1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| class Database {
protected static $_instance;
final protected function __construct () {
throw new RuntimeException("Illegal call of " . __METHOD__);
}
public static function instance () {
if (isset(self::$_instance))
return self::$_instance;
list($dsn, $username, $password, $options) = func_get_args() + array('', null, null, array());
return self::$_instance = new PDO($dsn, $username, $password, $options);
}
public static function factory ($model, $id = null) {
if (!class_exists($model, true))
throw new RuntimeException("Model calss not found: {$model}");
if (!in_array('Model', class_implements($model)))
throw new RuntimeException("{$model} class doesn't implements Model interface");
return new $model(self::$_instance, $id);
}
}
interface Model {
public function __construct (PDO $pdo, $id = null);
public function create (array $data);
public function retrieve ($id);
public function update (array $data = array());
public function delete ();
}
abstract class BaseModel implements Model {
protected $_pdo;
protected $_data;
public function __construct (PDO $pdo, $id = null) {
$this->_pdo = $pdo;
if ($id !== null && !$this->retrieve($id))
throw new RuntimeException('Cannot retrieve data');
}
public function getData () {
return $this->_data();
}
public function __get ($key) {
return $this->__isset($key) ? $this->_data[$key] : null;
}
public function __set ($key, $value) {
if ($this->__isset($key))
$this->_data[$key] = $value;
else
throw new RuntimeException("Cannot set {$key}: property doesn't exists");
}
public function __isset ($key) {
return isset($this->_data[$key]);
}
public function __unset ($key) {
unset($this->_data[$key]);
}
}
// Exemple de classe de modèle concrête
class User extends BaseModel {
public function create (array $data) {
$query = "INSERT IGNORE INTO `mydb`.`users` SET `login`=:login,`password`=:password,`mail`=:mail";
$data = filter_var_array($data, array(
'login' => FILTER_SANITIZE_STRING,
'password' => FILTER_UNSAFE_RAW,
'mail' => FILTER_SANITIZE_EMAIL
));
$stmt = $this->_pdo->prepare($query);
if (!$stmt->execute($data)) {
return false;
}
$inerted_id = $this->_pdo->lastInsertId();
return $this->retrieve($inerted_id);
}
public function retrieve ($id) {
$query = "SELECT `id`,`login`,`password`,`mail` FROM `mydb`.`users` WHERE `id`=:id";
$stmt = $this->_pdo->prepare($query);
if (!$stmt->execute(array('id' => $id))) {
return false;
}
$this->_data = $stmt->fetch(PDO::FETCH_ASSOC);
return $this;
}
public function update (array $data = array()) {
$query = "UPDATE `mydb`.`users` SET `login`=:login,`password`=:password,`mail`=:mail WHERE `id`=:id";
$data += $this->_data;
$data = filter_var_array($data, array(
'id' => FILTER_SANITIZE_NUMBER_INT,
'login' => FILTER_SANITIZE_STRING,
'password' => FILTER_UNSAFE_RAW,
'mail' => FILTER_SANITIZE_EMAIL
));
$stmt = $this->_pdo->prepare($query);
if (!$stmt->execute($data)) {
return false;
}
$this->_data = $data;
return $this;
}
public function delete () {
$query = "DELETE FROM `mydb`.`users` WHERE `id`=:id";
$stmt = $this->_pdo->prepare($query);
$data = filter_var_array($this->_data, array(
'id' => FILTER_SANITIZE_NUMBER_INT,
));
return $stmt->execute($data);
}
} |
Partager