@@ -17,237 +17,237 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class AbstractActiveRecord implements ActiveRecordInterface |
19 | 19 | { |
20 | - /** @var \PDO The PDO object. */ |
|
21 | - private $pdo; |
|
22 | - |
|
23 | - /** @var null|int The ID. */ |
|
24 | - private $id; |
|
25 | - |
|
26 | - /** |
|
27 | - * Construct an abstract pdo active record with the given pdo. |
|
28 | - * |
|
29 | - * @param \PDO $pdo |
|
30 | - */ |
|
31 | - public function __construct(\PDO $pdo) |
|
32 | - { |
|
33 | - $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); |
|
34 | - $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
35 | - |
|
36 | - $this->setPdo($pdo); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * {@inheritdoc} |
|
41 | - */ |
|
42 | - public function create() |
|
43 | - { |
|
44 | - try { |
|
45 | - $pdoStatement = $this->getPdo()->prepare($this->getCreateQuery()); |
|
46 | - $pdoStatement->execute($this->getActiveRecordData()); |
|
47 | - |
|
48 | - $this->setId(intval($this->getPdo()->lastInsertId())); |
|
49 | - } catch(\PDOException $e) { |
|
50 | - throw new ActiveRecordException('Can\'t create the record.', 0, $e); |
|
51 | - } |
|
52 | - |
|
53 | - return $this; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Returns the create query. |
|
58 | - * |
|
59 | - * @return string the create query. |
|
60 | - */ |
|
61 | - private function getCreateQuery() |
|
62 | - { |
|
63 | - $columns = array_keys($this->getActiveRecordData()); |
|
64 | - $values = []; |
|
65 | - |
|
66 | - foreach ($columns as $key => $value) { |
|
67 | - $values[] = ':' . $value; |
|
68 | - } |
|
69 | - |
|
70 | - return sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->getActiveRecordName(), implode(', ', $columns), implode(', ', $values)); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * {@inheritdoc} |
|
75 | - */ |
|
76 | - public function read($id) |
|
77 | - { |
|
78 | - try { |
|
79 | - $pdoStatement = $this->getPdo()->prepare($this->getReadQuery()); |
|
80 | - $pdoStatement->execute(['id' => $id]); |
|
81 | - |
|
82 | - $this->setActiveRecordData($pdoStatement->fetch()); |
|
83 | - $this->setId($id); |
|
84 | - } catch (\PDOException $e) { |
|
85 | - throw new ActiveRecordException('Can\'t read the record.', 0, $e); |
|
86 | - } |
|
87 | - |
|
88 | - return $this; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Returns the read query. |
|
93 | - * |
|
94 | - * @return string the read query. |
|
95 | - */ |
|
96 | - private function getReadQuery() |
|
97 | - { |
|
98 | - return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName()); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * {@inheritdoc} |
|
103 | - */ |
|
104 | - public function update() |
|
105 | - { |
|
106 | - if (!$this->exists()) { |
|
107 | - throw new ActiveRecordException('Can\'t update a non-existent record.'); |
|
108 | - } |
|
109 | - |
|
110 | - try { |
|
111 | - $pdoStatement = $this->getPdo()->prepare($this->getUpdateQuery()); |
|
112 | - $pdoStatement->execute(['id' => $this->getId()] + $this->getActiveRecordData()); |
|
113 | - } catch (\PDOException $e) { |
|
114 | - throw new ActiveRecordException('Can\'t update the record.', 0, $e); |
|
115 | - } |
|
116 | - |
|
117 | - return $this; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Returns the update query. |
|
122 | - * |
|
123 | - * @return string the update query. |
|
124 | - */ |
|
125 | - private function getUpdateQuery() |
|
126 | - { |
|
127 | - $values = []; |
|
128 | - |
|
129 | - foreach (array_keys($this->getActiveRecordData()) as $key => $value) { |
|
130 | - $values[] = $value . ' = :' . $value; |
|
131 | - } |
|
132 | - |
|
133 | - return sprintf('UPDATE %s SET %s WHERE `id` = :id', $this->getActiveRecordName(), implode(', ', $values)); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * {@inheritdoc} |
|
138 | - */ |
|
139 | - public function delete() |
|
140 | - { |
|
141 | - if (!$this->exists()) { |
|
142 | - throw new ActiveRecordException('Can\'t delete a non-existent record.'); |
|
143 | - } |
|
144 | - |
|
145 | - try { |
|
146 | - $pdoStatement = $this->getPdo()->prepare($this->getDeleteQuery()); |
|
147 | - $pdoStatement->execute(['id' => $this->getId()]); |
|
148 | - |
|
149 | - $this->setId(null); |
|
150 | - } catch (\PDOException $e) { |
|
151 | - throw new ActiveRecordException('Can\'t delete the record.', 0, $e); |
|
152 | - } |
|
153 | - |
|
154 | - return $this; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Returns the delete query. |
|
159 | - * |
|
160 | - * @return string the delete query. |
|
161 | - */ |
|
162 | - private function getDeleteQuery() |
|
163 | - { |
|
164 | - return sprintf('DELETE FROM %s WHERE `id` = :id', $this->getActiveRecordName()); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * {@inheritdoc} |
|
169 | - */ |
|
170 | - public function exists() |
|
171 | - { |
|
172 | - return $this->getId() !== null; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Returns the PDO. |
|
177 | - * |
|
178 | - * @return \PDO the PDO. |
|
179 | - */ |
|
180 | - public function getPdo() |
|
181 | - { |
|
182 | - return $this->pdo; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Set the PDO. |
|
187 | - * |
|
188 | - * @param \PDO $pdo |
|
189 | - * @return $this |
|
190 | - */ |
|
191 | - protected function setPdo($pdo) |
|
192 | - { |
|
193 | - $this->pdo = $pdo; |
|
194 | - |
|
195 | - return $this; |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Returns the ID. |
|
200 | - * |
|
201 | - * @return null|int The ID. |
|
202 | - */ |
|
203 | - public function getId() |
|
204 | - { |
|
205 | - return $this->id; |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Set the ID. |
|
210 | - * |
|
211 | - * @param int $id |
|
212 | - * @return $this |
|
213 | - */ |
|
214 | - protected function setId($id) |
|
215 | - { |
|
216 | - $this->id = $id; |
|
217 | - |
|
218 | - return $this; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Returns the active record name. |
|
223 | - * |
|
224 | - * @return string the active record name. |
|
225 | - */ |
|
226 | - abstract protected function getActiveRecordName(); |
|
227 | - |
|
228 | - /** |
|
229 | - * Returns the active record data. |
|
230 | - * |
|
231 | - * @return array the active record data. |
|
232 | - */ |
|
233 | - abstract protected function getActiveRecordData(); |
|
234 | - |
|
235 | - /** |
|
236 | - * Set the active record data. |
|
237 | - * |
|
238 | - * @param array $fetch |
|
239 | - * @return null |
|
240 | - */ |
|
241 | - protected function setActiveRecordData(array $fetch) |
|
242 | - { |
|
243 | - $data = $this->getActiveRecordData(); |
|
244 | - |
|
245 | - foreach ($data as $key => &$value) { |
|
246 | - if (!isset($fetch[$key])) { |
|
247 | - throw new ActiveRecordException(sprintf('Can\'t read the expected column "%s". It\'s not returnd by the database', $key)); |
|
248 | - } |
|
249 | - |
|
250 | - $value = $fetch[$key]; |
|
251 | - } |
|
252 | - } |
|
20 | + /** @var \PDO The PDO object. */ |
|
21 | + private $pdo; |
|
22 | + |
|
23 | + /** @var null|int The ID. */ |
|
24 | + private $id; |
|
25 | + |
|
26 | + /** |
|
27 | + * Construct an abstract pdo active record with the given pdo. |
|
28 | + * |
|
29 | + * @param \PDO $pdo |
|
30 | + */ |
|
31 | + public function __construct(\PDO $pdo) |
|
32 | + { |
|
33 | + $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); |
|
34 | + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
35 | + |
|
36 | + $this->setPdo($pdo); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * {@inheritdoc} |
|
41 | + */ |
|
42 | + public function create() |
|
43 | + { |
|
44 | + try { |
|
45 | + $pdoStatement = $this->getPdo()->prepare($this->getCreateQuery()); |
|
46 | + $pdoStatement->execute($this->getActiveRecordData()); |
|
47 | + |
|
48 | + $this->setId(intval($this->getPdo()->lastInsertId())); |
|
49 | + } catch(\PDOException $e) { |
|
50 | + throw new ActiveRecordException('Can\'t create the record.', 0, $e); |
|
51 | + } |
|
52 | + |
|
53 | + return $this; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Returns the create query. |
|
58 | + * |
|
59 | + * @return string the create query. |
|
60 | + */ |
|
61 | + private function getCreateQuery() |
|
62 | + { |
|
63 | + $columns = array_keys($this->getActiveRecordData()); |
|
64 | + $values = []; |
|
65 | + |
|
66 | + foreach ($columns as $key => $value) { |
|
67 | + $values[] = ':' . $value; |
|
68 | + } |
|
69 | + |
|
70 | + return sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->getActiveRecordName(), implode(', ', $columns), implode(', ', $values)); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * {@inheritdoc} |
|
75 | + */ |
|
76 | + public function read($id) |
|
77 | + { |
|
78 | + try { |
|
79 | + $pdoStatement = $this->getPdo()->prepare($this->getReadQuery()); |
|
80 | + $pdoStatement->execute(['id' => $id]); |
|
81 | + |
|
82 | + $this->setActiveRecordData($pdoStatement->fetch()); |
|
83 | + $this->setId($id); |
|
84 | + } catch (\PDOException $e) { |
|
85 | + throw new ActiveRecordException('Can\'t read the record.', 0, $e); |
|
86 | + } |
|
87 | + |
|
88 | + return $this; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Returns the read query. |
|
93 | + * |
|
94 | + * @return string the read query. |
|
95 | + */ |
|
96 | + private function getReadQuery() |
|
97 | + { |
|
98 | + return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName()); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * {@inheritdoc} |
|
103 | + */ |
|
104 | + public function update() |
|
105 | + { |
|
106 | + if (!$this->exists()) { |
|
107 | + throw new ActiveRecordException('Can\'t update a non-existent record.'); |
|
108 | + } |
|
109 | + |
|
110 | + try { |
|
111 | + $pdoStatement = $this->getPdo()->prepare($this->getUpdateQuery()); |
|
112 | + $pdoStatement->execute(['id' => $this->getId()] + $this->getActiveRecordData()); |
|
113 | + } catch (\PDOException $e) { |
|
114 | + throw new ActiveRecordException('Can\'t update the record.', 0, $e); |
|
115 | + } |
|
116 | + |
|
117 | + return $this; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Returns the update query. |
|
122 | + * |
|
123 | + * @return string the update query. |
|
124 | + */ |
|
125 | + private function getUpdateQuery() |
|
126 | + { |
|
127 | + $values = []; |
|
128 | + |
|
129 | + foreach (array_keys($this->getActiveRecordData()) as $key => $value) { |
|
130 | + $values[] = $value . ' = :' . $value; |
|
131 | + } |
|
132 | + |
|
133 | + return sprintf('UPDATE %s SET %s WHERE `id` = :id', $this->getActiveRecordName(), implode(', ', $values)); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * {@inheritdoc} |
|
138 | + */ |
|
139 | + public function delete() |
|
140 | + { |
|
141 | + if (!$this->exists()) { |
|
142 | + throw new ActiveRecordException('Can\'t delete a non-existent record.'); |
|
143 | + } |
|
144 | + |
|
145 | + try { |
|
146 | + $pdoStatement = $this->getPdo()->prepare($this->getDeleteQuery()); |
|
147 | + $pdoStatement->execute(['id' => $this->getId()]); |
|
148 | + |
|
149 | + $this->setId(null); |
|
150 | + } catch (\PDOException $e) { |
|
151 | + throw new ActiveRecordException('Can\'t delete the record.', 0, $e); |
|
152 | + } |
|
153 | + |
|
154 | + return $this; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Returns the delete query. |
|
159 | + * |
|
160 | + * @return string the delete query. |
|
161 | + */ |
|
162 | + private function getDeleteQuery() |
|
163 | + { |
|
164 | + return sprintf('DELETE FROM %s WHERE `id` = :id', $this->getActiveRecordName()); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * {@inheritdoc} |
|
169 | + */ |
|
170 | + public function exists() |
|
171 | + { |
|
172 | + return $this->getId() !== null; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Returns the PDO. |
|
177 | + * |
|
178 | + * @return \PDO the PDO. |
|
179 | + */ |
|
180 | + public function getPdo() |
|
181 | + { |
|
182 | + return $this->pdo; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Set the PDO. |
|
187 | + * |
|
188 | + * @param \PDO $pdo |
|
189 | + * @return $this |
|
190 | + */ |
|
191 | + protected function setPdo($pdo) |
|
192 | + { |
|
193 | + $this->pdo = $pdo; |
|
194 | + |
|
195 | + return $this; |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Returns the ID. |
|
200 | + * |
|
201 | + * @return null|int The ID. |
|
202 | + */ |
|
203 | + public function getId() |
|
204 | + { |
|
205 | + return $this->id; |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Set the ID. |
|
210 | + * |
|
211 | + * @param int $id |
|
212 | + * @return $this |
|
213 | + */ |
|
214 | + protected function setId($id) |
|
215 | + { |
|
216 | + $this->id = $id; |
|
217 | + |
|
218 | + return $this; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Returns the active record name. |
|
223 | + * |
|
224 | + * @return string the active record name. |
|
225 | + */ |
|
226 | + abstract protected function getActiveRecordName(); |
|
227 | + |
|
228 | + /** |
|
229 | + * Returns the active record data. |
|
230 | + * |
|
231 | + * @return array the active record data. |
|
232 | + */ |
|
233 | + abstract protected function getActiveRecordData(); |
|
234 | + |
|
235 | + /** |
|
236 | + * Set the active record data. |
|
237 | + * |
|
238 | + * @param array $fetch |
|
239 | + * @return null |
|
240 | + */ |
|
241 | + protected function setActiveRecordData(array $fetch) |
|
242 | + { |
|
243 | + $data = $this->getActiveRecordData(); |
|
244 | + |
|
245 | + foreach ($data as $key => &$value) { |
|
246 | + if (!isset($fetch[$key])) { |
|
247 | + throw new ActiveRecordException(sprintf('Can\'t read the expected column "%s". It\'s not returnd by the database', $key)); |
|
248 | + } |
|
249 | + |
|
250 | + $value = $fetch[$key]; |
|
251 | + } |
|
252 | + } |
|
253 | 253 | } |
@@ -46,7 +46,7 @@ |
||
46 | 46 | $pdoStatement->execute($this->getActiveRecordData()); |
47 | 47 | |
48 | 48 | $this->setId(intval($this->getPdo()->lastInsertId())); |
49 | - } catch(\PDOException $e) { |
|
49 | + } catch (\PDOException $e) { |
|
50 | 50 | throw new ActiveRecordException('Can\'t create the record.', 0, $e); |
51 | 51 | } |
52 | 52 |
@@ -17,65 +17,65 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class AbstractActiveRecordSearch extends AbstractActiveRecord |
19 | 19 | { |
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - public function search($options = []) |
|
24 | - { |
|
25 | - try { |
|
26 | - $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($options)); |
|
27 | - array_walk_recursive($options, function (&$value) use ($pdoStatement) { |
|
28 | - static $index = 1; |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + public function search($options = []) |
|
24 | + { |
|
25 | + try { |
|
26 | + $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($options)); |
|
27 | + array_walk_recursive($options, function (&$value) use ($pdoStatement) { |
|
28 | + static $index = 1; |
|
29 | 29 | |
30 | - $pdoStatement->bindParam($index++, $value); |
|
31 | - }); |
|
30 | + $pdoStatement->bindParam($index++, $value); |
|
31 | + }); |
|
32 | 32 | |
33 | - $pdoStatement->execute(); |
|
34 | - $result = []; |
|
33 | + $pdoStatement->execute(); |
|
34 | + $result = []; |
|
35 | 35 | |
36 | - while ($fetch = $pdoStatement->fetch()) { |
|
37 | - $new = new static($this->getPdo()); |
|
36 | + while ($fetch = $pdoStatement->fetch()) { |
|
37 | + $new = new static($this->getPdo()); |
|
38 | 38 | |
39 | - $new->setId(intval($fetch['id'])); |
|
40 | - $new->setActiveRecordData($fetch); |
|
39 | + $new->setId(intval($fetch['id'])); |
|
40 | + $new->setActiveRecordData($fetch); |
|
41 | 41 | |
42 | - $result[] = $new; |
|
43 | - } |
|
42 | + $result[] = $new; |
|
43 | + } |
|
44 | 44 | |
45 | - return $result; |
|
46 | - } catch (\PDOException $e) { |
|
47 | - throw new ActiveRecordException('Can\'t search the record.', 0, $e); |
|
48 | - } |
|
49 | - } |
|
45 | + return $result; |
|
46 | + } catch (\PDOException $e) { |
|
47 | + throw new ActiveRecordException('Can\'t search the record.', 0, $e); |
|
48 | + } |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Returns the search query with the given options. |
|
53 | - * |
|
54 | - * @param array $options = [] |
|
55 | - * @return string the search query with the given options. |
|
56 | - */ |
|
57 | - private function getSearchQuery($options = []) |
|
58 | - { |
|
59 | - $columns = array_keys($this->getActiveRecordData()); |
|
60 | - $columns[] = 'id'; |
|
61 | - $values = []; |
|
51 | + /** |
|
52 | + * Returns the search query with the given options. |
|
53 | + * |
|
54 | + * @param array $options = [] |
|
55 | + * @return string the search query with the given options. |
|
56 | + */ |
|
57 | + private function getSearchQuery($options = []) |
|
58 | + { |
|
59 | + $columns = array_keys($this->getActiveRecordData()); |
|
60 | + $columns[] = 'id'; |
|
61 | + $values = []; |
|
62 | 62 | |
63 | - foreach ($options as $key => $value) { |
|
64 | - if (!in_array($key, $columns)) { |
|
65 | - throw new ActiveRecordException(sprintf('Option key "%s" doesn\'t exists.', $key)); |
|
66 | - } |
|
63 | + foreach ($options as $key => $value) { |
|
64 | + if (!in_array($key, $columns)) { |
|
65 | + throw new ActiveRecordException(sprintf('Option key "%s" doesn\'t exists.', $key)); |
|
66 | + } |
|
67 | 67 | |
68 | - if (is_numeric($value)) { |
|
69 | - $values[] = $key . ' = ?'; |
|
70 | - } elseif (is_string($value)) { |
|
71 | - $values[] = $key . ' LIKE ?'; |
|
72 | - } elseif(is_array($value) && !empty($value)) { |
|
73 | - $values[] = $key . ' IN(' . implode(',', array_fill(0, count($value), '?')) . ')'; |
|
74 | - } else { |
|
75 | - throw new ActiveRecordException('Option value not supported.'); |
|
76 | - } |
|
77 | - } |
|
68 | + if (is_numeric($value)) { |
|
69 | + $values[] = $key . ' = ?'; |
|
70 | + } elseif (is_string($value)) { |
|
71 | + $values[] = $key . ' LIKE ?'; |
|
72 | + } elseif(is_array($value) && !empty($value)) { |
|
73 | + $values[] = $key . ' IN(' . implode(',', array_fill(0, count($value), '?')) . ')'; |
|
74 | + } else { |
|
75 | + throw new ActiveRecordException('Option value not supported.'); |
|
76 | + } |
|
77 | + } |
|
78 | 78 | |
79 | - return sprintf('SELECT * FROM %s %s %s', $this->getActiveRecordName(), empty($values) ? '' : 'WHERE', implode(' AND ', $values)); |
|
80 | - } |
|
79 | + return sprintf('SELECT * FROM %s %s %s', $this->getActiveRecordName(), empty($values) ? '' : 'WHERE', implode(' AND ', $values)); |
|
80 | + } |
|
81 | 81 | } |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | { |
25 | 25 | try { |
26 | 26 | $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($options)); |
27 | - array_walk_recursive($options, function (&$value) use ($pdoStatement) { |
|
27 | + array_walk_recursive($options, function(&$value) use ($pdoStatement) { |
|
28 | 28 | static $index = 1; |
29 | 29 | |
30 | 30 | $pdoStatement->bindParam($index++, $value); |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | $values[] = $key . ' = ?'; |
70 | 70 | } elseif (is_string($value)) { |
71 | 71 | $values[] = $key . ' LIKE ?'; |
72 | - } elseif(is_array($value) && !empty($value)) { |
|
72 | + } elseif (is_array($value) && !empty($value)) { |
|
73 | 73 | $values[] = $key . ' IN(' . implode(',', array_fill(0, count($value), '?')) . ')'; |
74 | 74 | } else { |
75 | 75 | throw new ActiveRecordException('Option value not supported.'); |