sergiodanilojr /
crud
| 1 | <?php |
||
| 2 | |||
| 3 | |||
| 4 | namespace ElePHPant; |
||
| 5 | |||
| 6 | |||
| 7 | /** |
||
| 8 | * Class CRUD |
||
| 9 | * @package ElePHPant |
||
| 10 | */ |
||
| 11 | class CRUD |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var |
||
| 15 | */ |
||
| 16 | private $fail; |
||
| 17 | /** |
||
| 18 | * @var |
||
| 19 | */ |
||
| 20 | private $query; |
||
| 21 | /** |
||
| 22 | * @var |
||
| 23 | */ |
||
| 24 | private $params; |
||
| 25 | /** |
||
| 26 | * @var |
||
| 27 | */ |
||
| 28 | private static $table; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $table |
||
| 32 | * @return static |
||
| 33 | */ |
||
| 34 | public static function setTable(string $table) |
||
| 35 | { |
||
| 36 | self::$table = $table; |
||
| 37 | return new static(); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @return string|null |
||
| 42 | */ |
||
| 43 | public function getQuery(): ?string |
||
| 44 | { |
||
| 45 | return $this->query; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @return mixed |
||
| 50 | */ |
||
| 51 | public function getFail() |
||
| 52 | { |
||
| 53 | return $this->fail; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param mixed $query |
||
| 58 | * @return CRUD |
||
| 59 | */ |
||
| 60 | public function setQuery($query) |
||
| 61 | { |
||
| 62 | $this->query = $query; |
||
| 63 | return $this; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param $params |
||
| 68 | */ |
||
| 69 | public function setParams($params) |
||
| 70 | { |
||
| 71 | $this->parseParams($params); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param array $data |
||
| 76 | * @return int|null |
||
| 77 | */ |
||
| 78 | public function create(array $data): ?int |
||
| 79 | { |
||
| 80 | try { |
||
| 81 | $columns = implode(", ", array_keys($data)); |
||
| 82 | $values = ":" . implode(", :", array_keys($data)); |
||
| 83 | |||
| 84 | $stmt = Connection::getInstance()->prepare('INSERT INTO ' . self::$table . " ({$columns}) VALUES ({$values})"); |
||
| 85 | $stmt->execute($this->filter($data)); |
||
| 86 | |||
| 87 | return Connection::getInstance()->lastInsertId(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 88 | } catch (\PDOException $exception) { |
||
| 89 | $this->fail = $exception; |
||
| 90 | return null; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $class |
||
| 96 | * @param bool $all |
||
| 97 | * @return array|mixed |
||
| 98 | */ |
||
| 99 | public function read(string $class = \stdClass::class, bool $all = false) |
||
| 100 | { |
||
| 101 | if ($all && $fetch = $this->fetch()) { |
||
| 102 | return $fetch->fetchAll(\PDO::FETCH_CLASS, $class); |
||
| 103 | } |
||
| 104 | |||
| 105 | $fetch = $this->fetch(); |
||
| 106 | |||
| 107 | return $fetch ? $fetch->fetchObject($class) : null; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param array $data |
||
| 112 | * @param string $terms |
||
| 113 | * @param string $params |
||
| 114 | * @return int|null |
||
| 115 | */ |
||
| 116 | public function update(array $data, string $terms): ?int |
||
| 117 | { |
||
| 118 | try { |
||
| 119 | $dataSet = []; |
||
| 120 | foreach ($data as $bind => $value) { |
||
| 121 | $dataSet[] = "{$bind} = :{$bind}"; |
||
| 122 | } |
||
| 123 | |||
| 124 | $dataSet = implode(", ", $dataSet); |
||
| 125 | |||
| 126 | $stmt = Connection::getInstance()->prepare('UPDATE ' . self::$table . " SET {$dataSet} WHERE {$terms}"); |
||
| 127 | $stmt->execute($this->filter(array_merge($data, $this->params))); |
||
| 128 | return ($stmt->rowCount() ?? 1); |
||
| 129 | } catch (\PDOException $exception) { |
||
| 130 | $this->fail = $exception; |
||
| 131 | return null; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $terms |
||
| 137 | * @param string|null $params |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | public function delete(string $terms, ?string $params = null): bool |
||
| 141 | { |
||
| 142 | try { |
||
| 143 | $stmt = Connection::getInstance() |
||
| 144 | ->prepare('DELETE FROM ' . self::$table . " WHERE {$terms}"); |
||
| 145 | |||
| 146 | if ($params) { |
||
| 147 | $this->parseParams($params); |
||
| 148 | $stmt->execute($this->params); |
||
| 149 | return true; |
||
| 150 | } |
||
| 151 | |||
| 152 | $stmt->execute(); |
||
| 153 | return true; |
||
| 154 | } catch (\PDOException $exception) { |
||
| 155 | $this->fail = $exception; |
||
| 156 | return false; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $key |
||
| 162 | * @return int |
||
| 163 | */ |
||
| 164 | public function count(string $key = "id"): int |
||
| 165 | { |
||
| 166 | $stmt = Connection::getInstance()->prepare($this->query); |
||
| 167 | $params = $this->params; |
||
| 168 | |||
| 169 | if (!$params) { |
||
| 170 | parse_str($params, $params); |
||
| 171 | } |
||
| 172 | |||
| 173 | if ($params && !is_array($params)) { |
||
| 174 | parse_str($params, $params); |
||
| 175 | } |
||
| 176 | |||
| 177 | $stmt->execute($params); |
||
| 178 | return $stmt->rowCount(); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return bool|\PDOStatement|null |
||
| 183 | */ |
||
| 184 | private function fetch() |
||
| 185 | { |
||
| 186 | try { |
||
| 187 | $stmt = Connection::getInstance()->prepare($this->query . $this->order . $this->limit . $this->offset); |
||
|
0 ignored issues
–
show
|
|||
| 188 | |||
| 189 | $stmt->execute($this->params); |
||
| 190 | |||
| 191 | if (!$stmt->rowCount()) { |
||
| 192 | return null; |
||
| 193 | } |
||
| 194 | |||
| 195 | return $stmt; |
||
| 196 | |||
| 197 | } catch (\PDOException $exception) { |
||
| 198 | $this->fail = $exception; |
||
| 199 | return null; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param $params |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | private function parseParams($params) |
||
| 208 | { |
||
| 209 | parse_str($params, $this->params); |
||
| 210 | return $this; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param array $data |
||
| 215 | * @return array|null |
||
| 216 | */ |
||
| 217 | private function filter(array $data): ?array |
||
| 218 | { |
||
| 219 | $filter = []; |
||
| 220 | foreach ($data as $key => $value) { |
||
| 221 | $filter[$key] = (is_null($value) ? null : filter_var($value, FILTER_DEFAULT)); |
||
| 222 | } |
||
| 223 | return $filter; |
||
| 224 | } |
||
| 225 | } |