@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | $data = $this->getActiveRecordData(); |
27 | 27 | |
28 | 28 | $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($options)); |
29 | - array_walk_recursive($options, function (&$value) use ($pdoStatement) { |
|
29 | + array_walk_recursive($options, function(&$value) use ($pdoStatement) { |
|
30 | 30 | static $index = 1; |
31 | 31 | |
32 | 32 | $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('Invalid option value.'); |
@@ -17,63 +17,63 @@ |
||
17 | 17 | */ |
18 | 18 | abstract class AbstractActiveRecordSearch extends AbstractActiveRecord |
19 | 19 | { |
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - public function search($options = []) |
|
24 | - { |
|
25 | - $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($options)); |
|
26 | - array_walk_recursive($options, function (&$value) use ($pdoStatement) { |
|
27 | - static $index = 1; |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + public function search($options = []) |
|
24 | + { |
|
25 | + $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($options)); |
|
26 | + array_walk_recursive($options, function (&$value) use ($pdoStatement) { |
|
27 | + static $index = 1; |
|
28 | 28 | |
29 | - $pdoStatement->bindParam($index++, $value); |
|
30 | - }); |
|
29 | + $pdoStatement->bindParam($index++, $value); |
|
30 | + }); |
|
31 | 31 | |
32 | - $pdoStatement->execute(); |
|
33 | - $result = []; |
|
32 | + $pdoStatement->execute(); |
|
33 | + $result = []; |
|
34 | 34 | |
35 | - while ($row = $pdoStatement->fetch()) { |
|
36 | - $new = new static($this->getPdo()); |
|
37 | - $new->setId(intval($row['id'])); |
|
38 | - $data = $new->getActiveRecordData(); |
|
35 | + while ($row = $pdoStatement->fetch()) { |
|
36 | + $new = new static($this->getPdo()); |
|
37 | + $new->setId(intval($row['id'])); |
|
38 | + $data = $new->getActiveRecordData(); |
|
39 | 39 | |
40 | - foreach ($data as $key => &$value) { |
|
41 | - $value = $row[$key]; |
|
42 | - } |
|
40 | + foreach ($data as $key => &$value) { |
|
41 | + $value = $row[$key]; |
|
42 | + } |
|
43 | 43 | |
44 | - $result[] = $new; |
|
45 | - } |
|
44 | + $result[] = $new; |
|
45 | + } |
|
46 | 46 | |
47 | - return $result; |
|
48 | - } |
|
47 | + return $result; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Returns the search query with the given options. |
|
52 | - * |
|
53 | - * @param array $options = [] |
|
54 | - * @return string the search query with the given options. |
|
55 | - */ |
|
56 | - private function getSearchQuery($options = []) |
|
57 | - { |
|
58 | - $columns = array_keys($this->getActiveRecordData()); |
|
59 | - $values = []; |
|
50 | + /** |
|
51 | + * Returns the search query with the given options. |
|
52 | + * |
|
53 | + * @param array $options = [] |
|
54 | + * @return string the search query with the given options. |
|
55 | + */ |
|
56 | + private function getSearchQuery($options = []) |
|
57 | + { |
|
58 | + $columns = array_keys($this->getActiveRecordData()); |
|
59 | + $values = []; |
|
60 | 60 | |
61 | - foreach ($options as $key => $value) { |
|
62 | - if (!in_array($key, $columns)) { |
|
63 | - throw new ActiveRecordException('Invalid option key.'); |
|
64 | - } |
|
61 | + foreach ($options as $key => $value) { |
|
62 | + if (!in_array($key, $columns)) { |
|
63 | + throw new ActiveRecordException('Invalid option key.'); |
|
64 | + } |
|
65 | 65 | |
66 | - if (is_int($value)) { |
|
67 | - $values[] = $key . ' = ?'; |
|
68 | - } elseif (is_string($value)) { |
|
69 | - $values[] = $key . ' LIKE ?'; |
|
70 | - } elseif(is_array($value) && !empty($value)) { |
|
71 | - $values[] = $key . ' IN(' . implode(',', array_fill(0, count($value), '?')) . ')'; |
|
72 | - } else { |
|
73 | - throw new ActiveRecordException('Invalid option value.'); |
|
74 | - } |
|
75 | - } |
|
66 | + if (is_int($value)) { |
|
67 | + $values[] = $key . ' = ?'; |
|
68 | + } elseif (is_string($value)) { |
|
69 | + $values[] = $key . ' LIKE ?'; |
|
70 | + } elseif(is_array($value) && !empty($value)) { |
|
71 | + $values[] = $key . ' IN(' . implode(',', array_fill(0, count($value), '?')) . ')'; |
|
72 | + } else { |
|
73 | + throw new ActiveRecordException('Invalid option value.'); |
|
74 | + } |
|
75 | + } |
|
76 | 76 | |
77 | - return sprintf('SELECT * FROM %s %s %s', $this->getActiveRecordName(), empty($values) ? '' : 'WHERE', implode(' AND ', $values)); |
|
78 | - } |
|
77 | + return sprintf('SELECT * FROM %s %s %s', $this->getActiveRecordName(), empty($values) ? '' : 'WHERE', implode(' AND ', $values)); |
|
78 | + } |
|
79 | 79 | } |
@@ -19,43 +19,43 @@ |
||
19 | 19 | */ |
20 | 20 | interface ActiveRecordInterface |
21 | 21 | { |
22 | - /** |
|
23 | - * Create the record. |
|
24 | - * |
|
25 | - * @return $this |
|
26 | - * @throws ActiveRecordException on failure. |
|
27 | - */ |
|
28 | - public function create(); |
|
22 | + /** |
|
23 | + * Create the record. |
|
24 | + * |
|
25 | + * @return $this |
|
26 | + * @throws ActiveRecordException on failure. |
|
27 | + */ |
|
28 | + public function create(); |
|
29 | 29 | |
30 | - /** |
|
31 | - * Read the record with the given ID. |
|
32 | - * |
|
33 | - * @param int $id |
|
34 | - * @return $this |
|
35 | - * @throws ActiveRecordException on failure. |
|
36 | - */ |
|
37 | - public function read($id); |
|
30 | + /** |
|
31 | + * Read the record with the given ID. |
|
32 | + * |
|
33 | + * @param int $id |
|
34 | + * @return $this |
|
35 | + * @throws ActiveRecordException on failure. |
|
36 | + */ |
|
37 | + public function read($id); |
|
38 | 38 | |
39 | - /** |
|
40 | - * Update the record. |
|
41 | - * |
|
42 | - * @return $this |
|
43 | - * @throws ActiveRecordException on failure. |
|
44 | - */ |
|
45 | - public function update(); |
|
39 | + /** |
|
40 | + * Update the record. |
|
41 | + * |
|
42 | + * @return $this |
|
43 | + * @throws ActiveRecordException on failure. |
|
44 | + */ |
|
45 | + public function update(); |
|
46 | 46 | |
47 | - /** |
|
48 | - * Delete the record. |
|
49 | - * |
|
50 | - * @return $this |
|
51 | - * @throws ActiveRecordException on failure. |
|
52 | - */ |
|
53 | - public function delete(); |
|
47 | + /** |
|
48 | + * Delete the record. |
|
49 | + * |
|
50 | + * @return $this |
|
51 | + * @throws ActiveRecordException on failure. |
|
52 | + */ |
|
53 | + public function delete(); |
|
54 | 54 | |
55 | - /** |
|
56 | - * Returns true if the record exists. |
|
57 | - * |
|
58 | - * @return bool true if the records exists. |
|
59 | - */ |
|
60 | - public function exists(); |
|
55 | + /** |
|
56 | + * Returns true if the record exists. |
|
57 | + * |
|
58 | + * @return bool true if the records exists. |
|
59 | + */ |
|
60 | + public function exists(); |
|
61 | 61 | } |
@@ -17,12 +17,12 @@ |
||
17 | 17 | */ |
18 | 18 | interface ActiveRecordSearchInterface extends ActiveRecordInterface |
19 | 19 | { |
20 | - /** |
|
21 | - * Returns the records with the given options. |
|
22 | - * |
|
23 | - * @param array $options = [] |
|
24 | - * @return static[] the records with the given options. |
|
25 | - * @throws ActiveRecordException on failure. |
|
26 | - */ |
|
27 | - public function search($options = []); |
|
20 | + /** |
|
21 | + * Returns the records with the given options. |
|
22 | + * |
|
23 | + * @param array $options = [] |
|
24 | + * @return static[] the records with the given options. |
|
25 | + * @throws ActiveRecordException on failure. |
|
26 | + */ |
|
27 | + public function search($options = []); |
|
28 | 28 | } |
@@ -17,192 +17,192 @@ |
||
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 | - $this->pdo = $pdo; |
|
34 | - $this->id = null; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * {@inheritdoc} |
|
39 | - */ |
|
40 | - public function create() |
|
41 | - { |
|
42 | - $pdoStatement = $this->pdo->prepare($this->getCreateQuery()); |
|
43 | - $pdoStatement->execute($this->getActiveRecordData()); |
|
44 | - |
|
45 | - $this->id = intval($this->pdo->lastInsertId()); |
|
46 | - return $this; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Returns the create query. |
|
51 | - * |
|
52 | - * @return string the create query. |
|
53 | - */ |
|
54 | - private function getCreateQuery() |
|
55 | - { |
|
56 | - $columns = array_keys($this->getActiveRecordData()); |
|
57 | - $values = []; |
|
58 | - |
|
59 | - foreach ($columns as $key => $value) { |
|
60 | - $values[] = ':' . $value; |
|
61 | - } |
|
62 | - |
|
63 | - return sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->getActiveRecordName(), implode(', ', $columns), implode(', ', $values)); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * {@inheritdoc} |
|
68 | - */ |
|
69 | - public function read($id) |
|
70 | - { |
|
71 | - $data = $this->getActiveRecordData(); |
|
72 | - |
|
73 | - $pdoStatement = $this->pdo->prepare($this->getReadQuery()); |
|
74 | - $pdoStatement->execute(['id' => $id]); |
|
75 | - $result = $pdoStatement->fetch(\PDO::FETCH_ASSOC); |
|
76 | - |
|
77 | - $this->id = $id; |
|
78 | - |
|
79 | - foreach ($data as $key => &$value) { |
|
80 | - $value = $result[$key]; |
|
81 | - } |
|
82 | - |
|
83 | - return $this; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Returns the read query. |
|
88 | - * |
|
89 | - * @return string the read query. |
|
90 | - */ |
|
91 | - private function getReadQuery() |
|
92 | - { |
|
93 | - return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName()); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * {@inheritdoc} |
|
98 | - */ |
|
99 | - public function update() |
|
100 | - { |
|
101 | - $pdoStatement = $this->pdo->prepare($this->getUpdateQuery()); |
|
102 | - $pdoStatement->execute(['id' => $this->id] + $this->getActiveRecordData()); |
|
103 | - |
|
104 | - return $this; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Returns the update query. |
|
109 | - * |
|
110 | - * @return string the update query. |
|
111 | - */ |
|
112 | - private function getUpdateQuery() |
|
113 | - { |
|
114 | - $values = []; |
|
115 | - |
|
116 | - foreach (array_keys($this->getActiveRecordData()) as $key => $value) { |
|
117 | - $values[] = $value . ' = :' . $value; |
|
118 | - } |
|
119 | - |
|
120 | - return sprintf('UPDATE %s SET %s WHERE `id` = :id', $this->getActiveRecordName(), implode(', ', $values)); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * {@inheritdoc} |
|
125 | - */ |
|
126 | - public function delete() |
|
127 | - { |
|
128 | - $pdoStatement = $this->pdo->prepare($this->getDeleteQuery()); |
|
129 | - $pdoStatement->execute(['id' => $this->id]); |
|
130 | - |
|
131 | - $this->id = null; |
|
132 | - return $this; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Returns the delete query. |
|
137 | - * |
|
138 | - * @return string the delete query. |
|
139 | - */ |
|
140 | - private function getDeleteQuery() |
|
141 | - { |
|
142 | - return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName()); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * {@inheritdoc} |
|
147 | - */ |
|
148 | - public function exists() |
|
149 | - { |
|
150 | - return $this->id !== null; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Returns the PDO. |
|
155 | - * |
|
156 | - * @return \PDO the PDO. |
|
157 | - */ |
|
158 | - public function getPdo() |
|
159 | - { |
|
160 | - return $this->pdo; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Set the PDO. |
|
165 | - * |
|
166 | - * @param \PDO $pdo |
|
167 | - * @return null |
|
168 | - */ |
|
169 | - protected function setPdo($pdo) |
|
170 | - { |
|
171 | - $this->pdo = $pdo; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Returns the ID. |
|
176 | - * |
|
177 | - * @return null|int The ID. |
|
178 | - */ |
|
179 | - public function getId() |
|
180 | - { |
|
181 | - return $this->id; |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Set the ID. |
|
186 | - * |
|
187 | - * @param int $id |
|
188 | - * @return null |
|
189 | - */ |
|
190 | - protected function setId($id) |
|
191 | - { |
|
192 | - $this->id = $id; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Returns the active record name. |
|
197 | - * |
|
198 | - * @return string the active record name. |
|
199 | - */ |
|
200 | - abstract protected function getActiveRecordName(); |
|
201 | - |
|
202 | - /** |
|
203 | - * Returns the active record data. |
|
204 | - * |
|
205 | - * @return array the active record data. |
|
206 | - */ |
|
207 | - abstract protected function getActiveRecordData(); |
|
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 | + $this->pdo = $pdo; |
|
34 | + $this->id = null; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * {@inheritdoc} |
|
39 | + */ |
|
40 | + public function create() |
|
41 | + { |
|
42 | + $pdoStatement = $this->pdo->prepare($this->getCreateQuery()); |
|
43 | + $pdoStatement->execute($this->getActiveRecordData()); |
|
44 | + |
|
45 | + $this->id = intval($this->pdo->lastInsertId()); |
|
46 | + return $this; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Returns the create query. |
|
51 | + * |
|
52 | + * @return string the create query. |
|
53 | + */ |
|
54 | + private function getCreateQuery() |
|
55 | + { |
|
56 | + $columns = array_keys($this->getActiveRecordData()); |
|
57 | + $values = []; |
|
58 | + |
|
59 | + foreach ($columns as $key => $value) { |
|
60 | + $values[] = ':' . $value; |
|
61 | + } |
|
62 | + |
|
63 | + return sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->getActiveRecordName(), implode(', ', $columns), implode(', ', $values)); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * {@inheritdoc} |
|
68 | + */ |
|
69 | + public function read($id) |
|
70 | + { |
|
71 | + $data = $this->getActiveRecordData(); |
|
72 | + |
|
73 | + $pdoStatement = $this->pdo->prepare($this->getReadQuery()); |
|
74 | + $pdoStatement->execute(['id' => $id]); |
|
75 | + $result = $pdoStatement->fetch(\PDO::FETCH_ASSOC); |
|
76 | + |
|
77 | + $this->id = $id; |
|
78 | + |
|
79 | + foreach ($data as $key => &$value) { |
|
80 | + $value = $result[$key]; |
|
81 | + } |
|
82 | + |
|
83 | + return $this; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Returns the read query. |
|
88 | + * |
|
89 | + * @return string the read query. |
|
90 | + */ |
|
91 | + private function getReadQuery() |
|
92 | + { |
|
93 | + return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName()); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * {@inheritdoc} |
|
98 | + */ |
|
99 | + public function update() |
|
100 | + { |
|
101 | + $pdoStatement = $this->pdo->prepare($this->getUpdateQuery()); |
|
102 | + $pdoStatement->execute(['id' => $this->id] + $this->getActiveRecordData()); |
|
103 | + |
|
104 | + return $this; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Returns the update query. |
|
109 | + * |
|
110 | + * @return string the update query. |
|
111 | + */ |
|
112 | + private function getUpdateQuery() |
|
113 | + { |
|
114 | + $values = []; |
|
115 | + |
|
116 | + foreach (array_keys($this->getActiveRecordData()) as $key => $value) { |
|
117 | + $values[] = $value . ' = :' . $value; |
|
118 | + } |
|
119 | + |
|
120 | + return sprintf('UPDATE %s SET %s WHERE `id` = :id', $this->getActiveRecordName(), implode(', ', $values)); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * {@inheritdoc} |
|
125 | + */ |
|
126 | + public function delete() |
|
127 | + { |
|
128 | + $pdoStatement = $this->pdo->prepare($this->getDeleteQuery()); |
|
129 | + $pdoStatement->execute(['id' => $this->id]); |
|
130 | + |
|
131 | + $this->id = null; |
|
132 | + return $this; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Returns the delete query. |
|
137 | + * |
|
138 | + * @return string the delete query. |
|
139 | + */ |
|
140 | + private function getDeleteQuery() |
|
141 | + { |
|
142 | + return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName()); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * {@inheritdoc} |
|
147 | + */ |
|
148 | + public function exists() |
|
149 | + { |
|
150 | + return $this->id !== null; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Returns the PDO. |
|
155 | + * |
|
156 | + * @return \PDO the PDO. |
|
157 | + */ |
|
158 | + public function getPdo() |
|
159 | + { |
|
160 | + return $this->pdo; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Set the PDO. |
|
165 | + * |
|
166 | + * @param \PDO $pdo |
|
167 | + * @return null |
|
168 | + */ |
|
169 | + protected function setPdo($pdo) |
|
170 | + { |
|
171 | + $this->pdo = $pdo; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Returns the ID. |
|
176 | + * |
|
177 | + * @return null|int The ID. |
|
178 | + */ |
|
179 | + public function getId() |
|
180 | + { |
|
181 | + return $this->id; |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Set the ID. |
|
186 | + * |
|
187 | + * @param int $id |
|
188 | + * @return null |
|
189 | + */ |
|
190 | + protected function setId($id) |
|
191 | + { |
|
192 | + $this->id = $id; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Returns the active record name. |
|
197 | + * |
|
198 | + * @return string the active record name. |
|
199 | + */ |
|
200 | + abstract protected function getActiveRecordName(); |
|
201 | + |
|
202 | + /** |
|
203 | + * Returns the active record data. |
|
204 | + * |
|
205 | + * @return array the active record data. |
|
206 | + */ |
|
207 | + abstract protected function getActiveRecordData(); |
|
208 | 208 | } |