Completed
Push — develop ( 9c27c4...b0b61d )
by Michael
03:21
created
src/AbstractActiveRecord.php 2 patches
Doc Comments   -1 removed lines patch added patch discarded remove patch
@@ -310,7 +310,6 @@
 block discarded – undo
310 310
 	/**
311 311
 	 * Returns the search query where clause.
312 312
 	 *
313
-	 * @param array $where
314 313
 	 * @return string the search query where clause.
315 314
 	 */
316 315
 	private function getSearchQueryWhereClause($key, $value)
Please login to merge, or discard this patch.
Indentation   +403 added lines, -403 removed lines patch added patch discarded remove patch
@@ -17,407 +17,407 @@
 block discarded – undo
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->getActiveRecordAttributes());
47
-
48
-			$this->setId(intval($this->getPdo()->lastInsertId()));
49
-		} catch (\PDOException $e) {
50
-			throw new ActiveRecordException(sprintf('Can not create a new active record entry in the `%s` table.', $this->getActiveRecordTable()), 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->getActiveRecordAttributes());
64
-		$values = [];
65
-
66
-		foreach ($columns as $key => $value) {
67
-			$values[] = sprintf(':%s', $value);
68
-		}
69
-
70
-		return sprintf('INSERT INTO `%s` (`%s`) VALUES (%s)', $this->getActiveRecordTable(), 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
-			$result = $pdoStatement->fetch();
82
-
83
-			if ($result === false) {
84
-				throw new ActiveRecordException(sprintf('Can not read the non-existent active record entry %d from the `%s` table.', $id, $this->getActiveRecordTable()));
85
-			}
86
-
87
-			$this->fill($result);
88
-			$this->setId($id);
89
-		} catch (\PDOException $e) {
90
-			throw new ActiveRecordException(sprintf('Can not read active record entry %d from the `%s` table.', $id, $this->getActiveRecordTable()), 0, $e);
91
-		}
92
-
93
-		return $this;
94
-	}
95
-
96
-	/**
97
-	 * Returns the read query.
98
-	 *
99
-	 * @return string the read query.
100
-	 */
101
-	private function getReadQuery()
102
-	{
103
-		return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordTable());
104
-	}
105
-
106
-	/**
107
-	 * {@inheritdoc}
108
-	 */
109
-	public function update()
110
-	{
111
-		if (!$this->exists()) {
112
-			throw new ActiveRecordException(sprintf('Can not update a non-existent active record entry to the `%s` table.', $this->getActiveRecordTable()));
113
-		}
114
-
115
-		try {
116
-			$pdoStatement = $this->getPdo()->prepare($this->getUpdateQuery());
117
-			$pdoStatement->execute(['id' => $this->getId()] + $this->getActiveRecordAttributes());
118
-		} catch (\PDOException $e) {
119
-			throw new ActiveRecordException(sprintf('Can not update active record entry %d to the `%s` table.', $this->getId(), $this->getActiveRecordTable()), 0, $e);
120
-		}
121
-
122
-		return $this;
123
-	}
124
-
125
-	/**
126
-	 * Returns the update query.
127
-	 *
128
-	 * @return string the update query.
129
-	 */
130
-	private function getUpdateQuery()
131
-	{
132
-		$values = [];
133
-
134
-		foreach (array_keys($this->getActiveRecordAttributes()) as $key => $value) {
135
-			$values[] = sprintf('`%s` = :%s', $value, $value);
136
-		}
137
-
138
-		return sprintf('UPDATE `%s` SET %s WHERE `id` = :id', $this->getActiveRecordTable(), implode(', ', $values));
139
-	}
140
-
141
-	/**
142
-	 * {@inheritdoc}
143
-	 */
144
-	public function delete()
145
-	{
146
-		if (!$this->exists()) {
147
-			throw new ActiveRecordException(sprintf('Can not delete a non-existent active record entry from the `%s` table.', $this->getActiveRecordTable()));
148
-		}
149
-
150
-		try {
151
-			$pdoStatement = $this->getPdo()->prepare($this->getDeleteQuery());
152
-			$pdoStatement->execute(['id' => $this->getId()]);
153
-
154
-			$this->setId(null);
155
-		} catch (\PDOException $e) {
156
-			throw new ActiveRecordException(sprintf('Can not delete active record entry %d from the `%s` table.', $this->getId(), $this->getActiveRecordTable()), 0, $e);
157
-		}
158
-
159
-		return $this;
160
-	}
161
-
162
-	/**
163
-	 * Returns the delete query.
164
-	 *
165
-	 * @return string the delete query.
166
-	 */
167
-	private function getDeleteQuery()
168
-	{
169
-		return sprintf('DELETE FROM `%s` WHERE `id` = :id', $this->getActiveRecordTable());
170
-	}
171
-
172
-	/**
173
-	 * {@inheritdoc}
174
-	 */
175
-	public function sync()
176
-	{
177
-		if (!$this->exists()) {
178
-			return $this->create();
179
-		}
180
-
181
-		return $this->update();
182
-	}
183
-
184
-	/**
185
-	 * {@inheritdoc}
186
-	 */
187
-	public function exists()
188
-	{
189
-		return $this->getId() !== null;
190
-	}
191
-
192
-	/**
193
-	 * Fill the active record
194
-	 *
195
-	 * @param array $fetch
196
-	 * @return null
197
-	 */
198
-	public function fill(array $fetch)
199
-	{
200
-		$data = $this->getActiveRecordAttributes();
201
-
202
-		foreach ($data as $key => &$value) {
203
-			if (!array_key_exists($key, $fetch)) {
204
-				throw new ActiveRecordException(sprintf('Can not read the expected column `%s`. It\'s not returnd by the `%s` table', $key, $this->getActiveRecordTable()));
205
-			}
206
-
207
-			$value = $fetch[$key];
208
-		}
209
-	}
210
-
211
-	/**
212
-	 * {@inheritdoc}
213
-	 */
214
-	public function searchFirst(array $where = [], array $orderBy = [])
215
-	{
216
-		try {
217
-			$pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($where, $orderBy, 1, 0));
218
-			array_walk_recursive($where, function(&$value) use ($pdoStatement) {
219
-				static $index = 1;
220
-
221
-				$pdoStatement->bindParam($index++, $value);
222
-			});
223
-
224
-			$pdoStatement->execute();
225
-			$fetch = $pdoStatement->fetch();
226
-
227
-			$this->setId(intval($fetch['id']));
228
-			$this->fill($fetch);
229
-
230
-			return $this;
231
-		} catch (\PDOException $e) {
232
-			throw new ActiveRecordException(sprintf('Can not search the record in the `%s` table.', $this->getActiveRecordTable()), 0, $e);
233
-		}
234
-	}
235
-
236
-	/**
237
-	 * {@inheritdoc}
238
-	 */
239
-	public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0)
240
-	{
241
-		try {
242
-			$pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($where, $orderBy, $limit, $offset));
243
-			array_walk_recursive($where, function(&$value) use ($pdoStatement) {
244
-				static $index = 1;
245
-
246
-				$pdoStatement->bindParam($index++, $value);
247
-			});
248
-
249
-			$pdoStatement->execute();
250
-			$result = [];
251
-
252
-			while ($fetch = $pdoStatement->fetch()) {
253
-				$new = new static($this->getPdo());
254
-
255
-				$new->setId(intval($fetch['id']));
256
-				$new->fill($fetch);
257
-
258
-				$result[] = $new;
259
-			}
260
-
261
-			return $result;
262
-		} catch (\PDOException $e) {
263
-			throw new ActiveRecordException(sprintf('Can not search the record in the `%s` table.', $this->getActiveRecordTable()), 0, $e);
264
-		}
265
-	}
266
-
267
-	/**
268
-	 * Returns the search query with the given where, order by, limit and offset clauses.
269
-	 *
270
-	 * @param array $where = []
271
-	 * @param array $orderBy = []
272
-	 * @param int $limit = -1
273
-	 * @param int $offset = 0
274
-	 * @return string the search query with the given where, order by, limit and offset clauses.
275
-	 */
276
-	private function getSearchQuery($where = [], $orderBy = [], $limit = -1, $offset = 0)
277
-	{
278
-		return sprintf(
279
-			'SELECT * FROM `%s` %s %s %s',
280
-			$this->getActiveRecordTable(),
281
-			$this->getSearchQueryWhereClauses($where),
282
-			$this->getSearchQueryOrderByClause($orderBy),
283
-			$this->getSearchQueryLimitClause($limit, $offset)
284
-		);
285
-	}
286
-
287
-	/**
288
-	 * Returns the search query where clauses.
289
-	 *
290
-	 * @param array $where
291
-	 * @return string the search query where clauses.
292
-	 */
293
-	private function getSearchQueryWhereClauses($where)
294
-	{
295
-		$columns = array_keys($this->getActiveRecordAttributes());
296
-		$columns[] = 'id';
297
-		$result = [];
298
-
299
-		foreach ($where as $key => $value) {
300
-			if (!in_array($key, $columns)) {
301
-				throw new ActiveRecordException(sprintf('Search attribute `%s` does not exists.', $key));
302
-			}
303
-
304
-			$result[] = $this->getSearchQueryWhereClause($key, $value);
305
-		}
306
-
307
-		return empty($result) ? '' : 'WHERE ' . implode(' AND ', $result);
308
-	}
309
-
310
-	/**
311
-	 * Returns the search query where clause.
312
-	 *
313
-	 * @param array $where
314
-	 * @return string the search query where clause.
315
-	 */
316
-	private function getSearchQueryWhereClause($key, $value)
317
-	{
318
-		if (is_numeric($value)) {
319
-			return sprintf('`%s` = ?', $key);
320
-		} elseif (is_string($value)) {
321
-			return sprintf('`%s` LIKE ?', $key);
322
-		} elseif (is_null($value)) {
323
-			return sprintf('`%s` IS ?', $key);
324
-		} elseif (is_array($value) && !empty($value)) {
325
-			return sprintf('`%s` IN (%s)', $key, implode(',', array_fill(0, count($value), '?')));
326
-		}
327
-
328
-		throw new ActiveRecordException(sprintf('Search attribute `%s` contains an unsupported type `%s`.', $key, gettype($value)));
329
-	}
330
-
331
-	/**
332
-	 * Returns the search query order by clause.
333
-	 *
334
-	 * @param array $orderBy
335
-	 * @return string the search query order by clause.
336
-	 */
337
-	private function getSearchQueryOrderByClause($orderBy)
338
-	{
339
-		$result = [];
340
-
341
-		foreach ($orderBy as $key => $value) {
342
-			$result[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC');
343
-		}
344
-
345
-		return empty($result) ? '' : 'ORDER BY ' . implode(', ', $result);
346
-	}
347
-
348
-	/**
349
-	 * Returns the search query limit and clause.
350
-	 *
351
-	 * @param int $limit = -1
352
-	 * @param int $offset = 0
353
-	 * @return string the search query limit and clause.
354
-	 */
355
-	private function getSearchQueryLimitClause($limit, $offset)
356
-	{
357
-		if ($limit == -1) {
358
-			return '';
359
-		}
360
-
361
-		return sprintf('LIMIT %d OFFSET %d', $limit, $offset);
362
-	}
363
-
364
-	/**
365
-	 * Returns the PDO.
366
-	 *
367
-	 * @return \PDO the PDO.
368
-	 */
369
-	public function getPdo()
370
-	{
371
-		return $this->pdo;
372
-	}
373
-
374
-	/**
375
-	 * Set the PDO.
376
-	 *
377
-	 * @param \PDO $pdo
378
-	 * @return $this
379
-	 */
380
-	protected function setPdo($pdo)
381
-	{
382
-		$this->pdo = $pdo;
383
-
384
-		return $this;
385
-	}
386
-
387
-	/**
388
-	 * Returns the ID.
389
-	 *
390
-	 * @return null|int The ID.
391
-	 */
392
-	public function getId()
393
-	{
394
-		return $this->id;
395
-	}
396
-
397
-	/**
398
-	 * Set the ID.
399
-	 *
400
-	 * @param int $id
401
-	 * @return $this
402
-	 */
403
-	protected function setId($id)
404
-	{
405
-		$this->id = $id;
406
-
407
-		return $this;
408
-	}
409
-
410
-	/**
411
-	 * Returns the active record table.
412
-	 *
413
-	 * @return string the active record table.
414
-	 */
415
-	abstract protected function getActiveRecordTable();
416
-
417
-	/**
418
-	 * Returns the active record attributes.
419
-	 *
420
-	 * @return array the active record attributes.
421
-	 */
422
-	abstract protected function getActiveRecordAttributes();
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->getActiveRecordAttributes());
47
+
48
+            $this->setId(intval($this->getPdo()->lastInsertId()));
49
+        } catch (\PDOException $e) {
50
+            throw new ActiveRecordException(sprintf('Can not create a new active record entry in the `%s` table.', $this->getActiveRecordTable()), 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->getActiveRecordAttributes());
64
+        $values = [];
65
+
66
+        foreach ($columns as $key => $value) {
67
+            $values[] = sprintf(':%s', $value);
68
+        }
69
+
70
+        return sprintf('INSERT INTO `%s` (`%s`) VALUES (%s)', $this->getActiveRecordTable(), 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
+            $result = $pdoStatement->fetch();
82
+
83
+            if ($result === false) {
84
+                throw new ActiveRecordException(sprintf('Can not read the non-existent active record entry %d from the `%s` table.', $id, $this->getActiveRecordTable()));
85
+            }
86
+
87
+            $this->fill($result);
88
+            $this->setId($id);
89
+        } catch (\PDOException $e) {
90
+            throw new ActiveRecordException(sprintf('Can not read active record entry %d from the `%s` table.', $id, $this->getActiveRecordTable()), 0, $e);
91
+        }
92
+
93
+        return $this;
94
+    }
95
+
96
+    /**
97
+     * Returns the read query.
98
+     *
99
+     * @return string the read query.
100
+     */
101
+    private function getReadQuery()
102
+    {
103
+        return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordTable());
104
+    }
105
+
106
+    /**
107
+     * {@inheritdoc}
108
+     */
109
+    public function update()
110
+    {
111
+        if (!$this->exists()) {
112
+            throw new ActiveRecordException(sprintf('Can not update a non-existent active record entry to the `%s` table.', $this->getActiveRecordTable()));
113
+        }
114
+
115
+        try {
116
+            $pdoStatement = $this->getPdo()->prepare($this->getUpdateQuery());
117
+            $pdoStatement->execute(['id' => $this->getId()] + $this->getActiveRecordAttributes());
118
+        } catch (\PDOException $e) {
119
+            throw new ActiveRecordException(sprintf('Can not update active record entry %d to the `%s` table.', $this->getId(), $this->getActiveRecordTable()), 0, $e);
120
+        }
121
+
122
+        return $this;
123
+    }
124
+
125
+    /**
126
+     * Returns the update query.
127
+     *
128
+     * @return string the update query.
129
+     */
130
+    private function getUpdateQuery()
131
+    {
132
+        $values = [];
133
+
134
+        foreach (array_keys($this->getActiveRecordAttributes()) as $key => $value) {
135
+            $values[] = sprintf('`%s` = :%s', $value, $value);
136
+        }
137
+
138
+        return sprintf('UPDATE `%s` SET %s WHERE `id` = :id', $this->getActiveRecordTable(), implode(', ', $values));
139
+    }
140
+
141
+    /**
142
+     * {@inheritdoc}
143
+     */
144
+    public function delete()
145
+    {
146
+        if (!$this->exists()) {
147
+            throw new ActiveRecordException(sprintf('Can not delete a non-existent active record entry from the `%s` table.', $this->getActiveRecordTable()));
148
+        }
149
+
150
+        try {
151
+            $pdoStatement = $this->getPdo()->prepare($this->getDeleteQuery());
152
+            $pdoStatement->execute(['id' => $this->getId()]);
153
+
154
+            $this->setId(null);
155
+        } catch (\PDOException $e) {
156
+            throw new ActiveRecordException(sprintf('Can not delete active record entry %d from the `%s` table.', $this->getId(), $this->getActiveRecordTable()), 0, $e);
157
+        }
158
+
159
+        return $this;
160
+    }
161
+
162
+    /**
163
+     * Returns the delete query.
164
+     *
165
+     * @return string the delete query.
166
+     */
167
+    private function getDeleteQuery()
168
+    {
169
+        return sprintf('DELETE FROM `%s` WHERE `id` = :id', $this->getActiveRecordTable());
170
+    }
171
+
172
+    /**
173
+     * {@inheritdoc}
174
+     */
175
+    public function sync()
176
+    {
177
+        if (!$this->exists()) {
178
+            return $this->create();
179
+        }
180
+
181
+        return $this->update();
182
+    }
183
+
184
+    /**
185
+     * {@inheritdoc}
186
+     */
187
+    public function exists()
188
+    {
189
+        return $this->getId() !== null;
190
+    }
191
+
192
+    /**
193
+     * Fill the active record
194
+     *
195
+     * @param array $fetch
196
+     * @return null
197
+     */
198
+    public function fill(array $fetch)
199
+    {
200
+        $data = $this->getActiveRecordAttributes();
201
+
202
+        foreach ($data as $key => &$value) {
203
+            if (!array_key_exists($key, $fetch)) {
204
+                throw new ActiveRecordException(sprintf('Can not read the expected column `%s`. It\'s not returnd by the `%s` table', $key, $this->getActiveRecordTable()));
205
+            }
206
+
207
+            $value = $fetch[$key];
208
+        }
209
+    }
210
+
211
+    /**
212
+     * {@inheritdoc}
213
+     */
214
+    public function searchFirst(array $where = [], array $orderBy = [])
215
+    {
216
+        try {
217
+            $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($where, $orderBy, 1, 0));
218
+            array_walk_recursive($where, function(&$value) use ($pdoStatement) {
219
+                static $index = 1;
220
+
221
+                $pdoStatement->bindParam($index++, $value);
222
+            });
223
+
224
+            $pdoStatement->execute();
225
+            $fetch = $pdoStatement->fetch();
226
+
227
+            $this->setId(intval($fetch['id']));
228
+            $this->fill($fetch);
229
+
230
+            return $this;
231
+        } catch (\PDOException $e) {
232
+            throw new ActiveRecordException(sprintf('Can not search the record in the `%s` table.', $this->getActiveRecordTable()), 0, $e);
233
+        }
234
+    }
235
+
236
+    /**
237
+     * {@inheritdoc}
238
+     */
239
+    public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0)
240
+    {
241
+        try {
242
+            $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($where, $orderBy, $limit, $offset));
243
+            array_walk_recursive($where, function(&$value) use ($pdoStatement) {
244
+                static $index = 1;
245
+
246
+                $pdoStatement->bindParam($index++, $value);
247
+            });
248
+
249
+            $pdoStatement->execute();
250
+            $result = [];
251
+
252
+            while ($fetch = $pdoStatement->fetch()) {
253
+                $new = new static($this->getPdo());
254
+
255
+                $new->setId(intval($fetch['id']));
256
+                $new->fill($fetch);
257
+
258
+                $result[] = $new;
259
+            }
260
+
261
+            return $result;
262
+        } catch (\PDOException $e) {
263
+            throw new ActiveRecordException(sprintf('Can not search the record in the `%s` table.', $this->getActiveRecordTable()), 0, $e);
264
+        }
265
+    }
266
+
267
+    /**
268
+     * Returns the search query with the given where, order by, limit and offset clauses.
269
+     *
270
+     * @param array $where = []
271
+     * @param array $orderBy = []
272
+     * @param int $limit = -1
273
+     * @param int $offset = 0
274
+     * @return string the search query with the given where, order by, limit and offset clauses.
275
+     */
276
+    private function getSearchQuery($where = [], $orderBy = [], $limit = -1, $offset = 0)
277
+    {
278
+        return sprintf(
279
+            'SELECT * FROM `%s` %s %s %s',
280
+            $this->getActiveRecordTable(),
281
+            $this->getSearchQueryWhereClauses($where),
282
+            $this->getSearchQueryOrderByClause($orderBy),
283
+            $this->getSearchQueryLimitClause($limit, $offset)
284
+        );
285
+    }
286
+
287
+    /**
288
+     * Returns the search query where clauses.
289
+     *
290
+     * @param array $where
291
+     * @return string the search query where clauses.
292
+     */
293
+    private function getSearchQueryWhereClauses($where)
294
+    {
295
+        $columns = array_keys($this->getActiveRecordAttributes());
296
+        $columns[] = 'id';
297
+        $result = [];
298
+
299
+        foreach ($where as $key => $value) {
300
+            if (!in_array($key, $columns)) {
301
+                throw new ActiveRecordException(sprintf('Search attribute `%s` does not exists.', $key));
302
+            }
303
+
304
+            $result[] = $this->getSearchQueryWhereClause($key, $value);
305
+        }
306
+
307
+        return empty($result) ? '' : 'WHERE ' . implode(' AND ', $result);
308
+    }
309
+
310
+    /**
311
+     * Returns the search query where clause.
312
+     *
313
+     * @param array $where
314
+     * @return string the search query where clause.
315
+     */
316
+    private function getSearchQueryWhereClause($key, $value)
317
+    {
318
+        if (is_numeric($value)) {
319
+            return sprintf('`%s` = ?', $key);
320
+        } elseif (is_string($value)) {
321
+            return sprintf('`%s` LIKE ?', $key);
322
+        } elseif (is_null($value)) {
323
+            return sprintf('`%s` IS ?', $key);
324
+        } elseif (is_array($value) && !empty($value)) {
325
+            return sprintf('`%s` IN (%s)', $key, implode(',', array_fill(0, count($value), '?')));
326
+        }
327
+
328
+        throw new ActiveRecordException(sprintf('Search attribute `%s` contains an unsupported type `%s`.', $key, gettype($value)));
329
+    }
330
+
331
+    /**
332
+     * Returns the search query order by clause.
333
+     *
334
+     * @param array $orderBy
335
+     * @return string the search query order by clause.
336
+     */
337
+    private function getSearchQueryOrderByClause($orderBy)
338
+    {
339
+        $result = [];
340
+
341
+        foreach ($orderBy as $key => $value) {
342
+            $result[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC');
343
+        }
344
+
345
+        return empty($result) ? '' : 'ORDER BY ' . implode(', ', $result);
346
+    }
347
+
348
+    /**
349
+     * Returns the search query limit and clause.
350
+     *
351
+     * @param int $limit = -1
352
+     * @param int $offset = 0
353
+     * @return string the search query limit and clause.
354
+     */
355
+    private function getSearchQueryLimitClause($limit, $offset)
356
+    {
357
+        if ($limit == -1) {
358
+            return '';
359
+        }
360
+
361
+        return sprintf('LIMIT %d OFFSET %d', $limit, $offset);
362
+    }
363
+
364
+    /**
365
+     * Returns the PDO.
366
+     *
367
+     * @return \PDO the PDO.
368
+     */
369
+    public function getPdo()
370
+    {
371
+        return $this->pdo;
372
+    }
373
+
374
+    /**
375
+     * Set the PDO.
376
+     *
377
+     * @param \PDO $pdo
378
+     * @return $this
379
+     */
380
+    protected function setPdo($pdo)
381
+    {
382
+        $this->pdo = $pdo;
383
+
384
+        return $this;
385
+    }
386
+
387
+    /**
388
+     * Returns the ID.
389
+     *
390
+     * @return null|int The ID.
391
+     */
392
+    public function getId()
393
+    {
394
+        return $this->id;
395
+    }
396
+
397
+    /**
398
+     * Set the ID.
399
+     *
400
+     * @param int $id
401
+     * @return $this
402
+     */
403
+    protected function setId($id)
404
+    {
405
+        $this->id = $id;
406
+
407
+        return $this;
408
+    }
409
+
410
+    /**
411
+     * Returns the active record table.
412
+     *
413
+     * @return string the active record table.
414
+     */
415
+    abstract protected function getActiveRecordTable();
416
+
417
+    /**
418
+     * Returns the active record attributes.
419
+     *
420
+     * @return array the active record attributes.
421
+     */
422
+    abstract protected function getActiveRecordAttributes();
423 423
 }
Please login to merge, or discard this patch.
src/ActiveRecordInterface.php 1 patch
Indentation   +71 added lines, -71 removed lines patch added patch discarded remove patch
@@ -19,83 +19,83 @@
 block discarded – undo
19 19
  */
20 20
 interface ActiveRecordInterface
21 21
 {
22
-	/**
23
-	 * Returns this active record after creating an entry with the records attributes.
24
-	 *
25
-	 * @return $this
26
-	 * @throws ActiveRecordException on failure.
27
-	 */
28
-	public function create();
22
+    /**
23
+     * Returns this active record after creating an entry with the records attributes.
24
+     *
25
+     * @return $this
26
+     * @throws ActiveRecordException on failure.
27
+     */
28
+    public function create();
29 29
 
30
-	/**
31
-	 * Returns this active record after reading the attributes from the entry with the given identifier.
32
-	 *
33
-	 * @param mixed $id
34
-	 * @return $this
35
-	 * @throws ActiveRecordException on failure.
36
-	 */
37
-	public function read($id);
30
+    /**
31
+     * Returns this active record after reading the attributes from the entry with the given identifier.
32
+     *
33
+     * @param mixed $id
34
+     * @return $this
35
+     * @throws ActiveRecordException on failure.
36
+     */
37
+    public function read($id);
38 38
 
39
-	/**
40
-	 * Returns this active record after updating the attributes to the corresponding entry.
41
-	 *
42
-	 * @return $this
43
-	 * @throws ActiveRecordException on failure.
44
-	 */
45
-	public function update();
39
+    /**
40
+     * Returns this active record after updating the attributes to the corresponding entry.
41
+     *
42
+     * @return $this
43
+     * @throws ActiveRecordException on failure.
44
+     */
45
+    public function update();
46 46
 
47
-	/**
48
-	 * Returns this record after deleting the corresponding entry.
49
-	 *
50
-	 * @return $this
51
-	 * @throws ActiveRecordException on failure.
52
-	 */
53
-	public function delete();
47
+    /**
48
+     * Returns this record after deleting the corresponding entry.
49
+     *
50
+     * @return $this
51
+     * @throws ActiveRecordException on failure.
52
+     */
53
+    public function delete();
54 54
 
55
-	/**
56
-	 * Returns this record after synchronizing it with the corresponding entry.
57
-	 * A new entry is created if this active record does not have a corresponding entry.
58
-	 *
59
-	 * @return $this
60
-	 * @throws ActiveRecordException on failure.
61
-	 */
62
-	public function sync();
55
+    /**
56
+     * Returns this record after synchronizing it with the corresponding entry.
57
+     * A new entry is created if this active record does not have a corresponding entry.
58
+     *
59
+     * @return $this
60
+     * @throws ActiveRecordException on failure.
61
+     */
62
+    public function sync();
63 63
 
64
-	/**
65
-	 * Returns true if this active record has a corresponding entry.
66
-	 *
67
-	 * @return bool true if this active record has a corresponding entry.
68
-	 */
69
-	public function exists();
64
+    /**
65
+     * Returns true if this active record has a corresponding entry.
66
+     *
67
+     * @return bool true if this active record has a corresponding entry.
68
+     */
69
+    public function exists();
70 70
 
71
-	/**
72
-	 * Returns this record after filling it with the given attributes.
73
-	 *
74
-	 * @param array $attributes = []
75
-	 * @return $this
76
-	 * @throws ActiveRecordException on failure.
77
-	 */
78
-	public function fill(array $attributes);
71
+    /**
72
+     * Returns this record after filling it with the given attributes.
73
+     *
74
+     * @param array $attributes = []
75
+     * @return $this
76
+     * @throws ActiveRecordException on failure.
77
+     */
78
+    public function fill(array $attributes);
79 79
 
80
-	/**
81
-	 * Returns this record after filling it with the attributes of the first entry with the given where and order by clauses.
82
-	 *
83
-	 * @param array $where = []
84
-	 * @param array $orderBy = []
85
-	 * @return $this
86
-	 * @throws ActiveRecordException on failure.
87
-	 */
88
-	public function searchFirst(array $where = [], array $orderBy = []);
80
+    /**
81
+     * Returns this record after filling it with the attributes of the first entry with the given where and order by clauses.
82
+     *
83
+     * @param array $where = []
84
+     * @param array $orderBy = []
85
+     * @return $this
86
+     * @throws ActiveRecordException on failure.
87
+     */
88
+    public function searchFirst(array $where = [], array $orderBy = []);
89 89
 
90
-	/**
91
-	 * Returns the records with the given where, order by, limit and offset clauses.
92
-	 *
93
-	 * @param array $where = []
94
-	 * @param array $orderBy = []
95
-	 * @param int $limit = -1
96
-	 * @param int $offset = 0
97
-	 * @return static[] the records with the given where, order by, limit and offset clauses.
98
-	 * @throws ActiveRecordException on failure.
99
-	 */
100
-	public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0);
90
+    /**
91
+     * Returns the records with the given where, order by, limit and offset clauses.
92
+     *
93
+     * @param array $where = []
94
+     * @param array $orderBy = []
95
+     * @param int $limit = -1
96
+     * @param int $offset = 0
97
+     * @return static[] the records with the given where, order by, limit and offset clauses.
98
+     * @throws ActiveRecordException on failure.
99
+     */
100
+    public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0);
101 101
 }
Please login to merge, or discard this patch.