Completed
Push — master ( c609bc...b05d4c )
by Michael
05:07 queued 02:06
created
src/ActiveRecordInterface.php 1 patch
Indentation   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -19,43 +19,43 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/AbstractActiveRecord.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -64,7 +64,7 @@  discard block
 block discarded – undo
64 64
 		$values = [];
65 65
 
66 66
 		foreach ($columns as $key => $value) {
67
-			$values[] = ':' . $value;
67
+			$values[] = ':'.$value;
68 68
 		}
69 69
 
70 70
 		return sprintf('INSERT INTO `%s` (`%s`) VALUES (%s)', $this->getActiveRecordName(), implode('`, `', $columns), implode(', ', $values));
@@ -127,7 +127,7 @@  discard block
 block discarded – undo
127 127
 		$values = [];
128 128
 
129 129
 		foreach (array_keys($this->getActiveRecordData()) as $key => $value) {
130
-			$values[] = '`' . $value . '` = :' . $value;
130
+			$values[] = '`'.$value.'` = :'.$value;
131 131
 		}
132 132
 
133 133
 		return sprintf('UPDATE `%s` SET %s WHERE `id` = :id', $this->getActiveRecordName(), implode(', ', $values));
Please login to merge, or discard this patch.
Indentation   +233 added lines, -233 removed lines patch added patch discarded remove patch
@@ -17,237 +17,237 @@
 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->getActiveRecordData());
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->getActiveRecordName()), 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(sprintf('Can not read active record entry %d from the `%s` table.', $id, $this->getActiveRecordName()), 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(sprintf('Can not update a non-existent active record entry to the `%s` table.', $this->getActiveRecordName()));
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(sprintf('Can not update active record entry %d to the `%s` table.', $this->getId(), $this->getActiveRecordName()), 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(sprintf('Can not delete a non-existent active record entry from the `%s` table.', $this->getActiveRecordName()));
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(sprintf('Can not delete active record entry %d from the `%s` table.', $this->getId(), $this->getActiveRecordName()), 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 (!array_key_exists($key, $fetch)) {
247
-				throw new ActiveRecordException(sprintf('Can not read the expected column `%s`. It\'s not returnd by the `%s` table', $key, $this->getActiveRecordName()));
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(sprintf('Can not create a new active record entry in the `%s` table.', $this->getActiveRecordName()), 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(sprintf('Can not read active record entry %d from the `%s` table.', $id, $this->getActiveRecordName()), 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(sprintf('Can not update a non-existent active record entry to the `%s` table.', $this->getActiveRecordName()));
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(sprintf('Can not update active record entry %d to the `%s` table.', $this->getId(), $this->getActiveRecordName()), 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(sprintf('Can not delete a non-existent active record entry from the `%s` table.', $this->getActiveRecordName()));
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(sprintf('Can not delete active record entry %d from the `%s` table.', $this->getId(), $this->getActiveRecordName()), 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 (!array_key_exists($key, $fetch)) {
247
+                throw new ActiveRecordException(sprintf('Can not read the expected column `%s`. It\'s not returnd by the `%s` table', $key, $this->getActiveRecordName()));
248
+            }
249
+
250
+            $value = $fetch[$key];
251
+        }
252
+    }
253 253
 }
Please login to merge, or discard this patch.
src/AbstractActiveRecordSearch.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -99,7 +99,7 @@  discard block
 block discarded – undo
99 99
 			}
100 100
 		}
101 101
 
102
-		return empty($result) ? '' : 'WHERE ' . implode(' AND ', $result);
102
+		return empty($result) ? '' : 'WHERE '.implode(' AND ', $result);
103 103
 	}
104 104
 
105 105
 	/**
@@ -116,6 +116,6 @@  discard block
 block discarded – undo
116 116
 			$result[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC');
117 117
 		}
118 118
 
119
-		return empty($result) ? '' : 'ORDER BY ' . implode(', ', $result);
119
+		return empty($result) ? '' : 'ORDER BY '.implode(', ', $result);
120 120
 	}
121 121
 }
Please login to merge, or discard this patch.
Indentation   +116 added lines, -116 removed lines patch added patch discarded remove patch
@@ -17,120 +17,120 @@
 block discarded – undo
17 17
  */
18 18
 abstract class AbstractActiveRecordSearch extends AbstractActiveRecord
19 19
 {
20
-	/**
21
-	 * {@inheritdoc}
22
-	 */
23
-	public function search($where = [], $orderBy = [], $limit = -1, $offset = 0)
24
-	{
25
-		try {
26
-			$pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($where, $orderBy, $limit, $offset));
27
-			array_walk_recursive($where, function(&$value) use ($pdoStatement) {
28
-				static $index = 1;
29
-
30
-				$pdoStatement->bindParam($index++, $value);
31
-			});
32
-
33
-			$pdoStatement->execute();
34
-			$result = [];
35
-
36
-			while ($fetch = $pdoStatement->fetch()) {
37
-				$new = new static($this->getPdo());
38
-
39
-				$new->setId(intval($fetch['id']));
40
-				$new->setActiveRecordData($fetch);
41
-
42
-				$result[] = $new;
43
-			}
44
-
45
-			return $result;
46
-		} catch (\PDOException $e) {
47
-			throw new ActiveRecordException(sprintf('Can not search the record in the `%s` table.', $this->getActiveRecordName()), 0, $e);
48
-		}
49
-	}
50
-
51
-	/**
52
-	 * Returns the search query with the given where, order by, limit and offset clauses.
53
-	 *
54
-	 * @param array $where = []
55
-	 * @param array $orderBy = []
56
-	 * @param int $limit = -1
57
-	 * @param int $offset = 0
58
-	 * @return string the search query with the given where, order by, limit and offset clauses.
59
-	 */
60
-	private function getSearchQuery($where = [], $orderBy = [], $limit = -1, $offset = 0)
61
-	{
62
-		return sprintf(
63
-			'SELECT * FROM `%s` %s %s %s',
64
-			$this->getActiveRecordName(),
65
-			$this->getSearchQueryWhereClause($where),
66
-			$this->getSearchQueryOrderByClause($orderBy),
67
-			$this->getSearchQueryLimitClause($limit, $offset)
68
-		);
69
-	}
70
-
71
-	/**
72
-	 * Returns the search query where clause.
73
-	 *
74
-	 * @param array $where
75
-	 * @return string the search query where clause.
76
-	 */
77
-	private function getSearchQueryWhereClause($where)
78
-	{
79
-		$columns = array_keys($this->getActiveRecordData());
80
-		$columns[] = 'id';
81
-		$result = [];
82
-
83
-		foreach ($where as $key => $value) {
84
-			if (!in_array($key, $columns)) {
85
-				throw new ActiveRecordException(sprintf('Search option key `%s` does not exists.', $key));
86
-			}
87
-
88
-			if (is_numeric($value)) {
89
-				$result[] = sprintf('`%s` = ?', $key);
90
-			} elseif (is_string($value)) {
91
-				$result[] = sprintf('`%s` LIKE ?', $key);
92
-			} elseif (is_null($value)) {
93
-				$result[] = sprintf('`%s` IS ?', $key);
94
-			} elseif (is_array($value) && !empty($value)) {
95
-				$result[] = sprintf('`%s` IN (%s)', $key, implode(',', array_fill(0, count($value), '?')));
96
-			} else {
97
-				throw new ActiveRecordException(sprintf('Search option value of key `%s` is not supported.', $key));
98
-			}
99
-		}
100
-
101
-		return empty($result) ? '' : 'WHERE ' . implode(' AND ', $result);
102
-	}
103
-
104
-	/**
105
-	 * Returns the search query order by clause.
106
-	 *
107
-	 * @param array $orderBy
108
-	 * @return string the search query order by clause.
109
-	 */
110
-	private function getSearchQueryOrderByClause($orderBy)
111
-	{
112
-		$result = [];
113
-
114
-		foreach ($orderBy as $key => $value) {
115
-			$result[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC');
116
-		}
117
-
118
-		return empty($result) ? '' : 'ORDER BY ' . implode(', ', $result);
119
-	}
120
-
121
-	/**
122
-	 * Returns the search query limit and clause.
123
-	 *
124
-	 * @param int $limit = -1
125
-	 * @param int $offset = 0
126
-	 * @return string the search query limit and clause.
127
-	 */
128
-	private function getSearchQueryLimitClause($limit, $offset)
129
-	{
130
-		if ($limit == -1) {
131
-			return '';
132
-		}
133
-
134
-		return sprintf('LIMIT %d OFFSET %d', $limit, $offset);
135
-	}
20
+    /**
21
+     * {@inheritdoc}
22
+     */
23
+    public function search($where = [], $orderBy = [], $limit = -1, $offset = 0)
24
+    {
25
+        try {
26
+            $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($where, $orderBy, $limit, $offset));
27
+            array_walk_recursive($where, function(&$value) use ($pdoStatement) {
28
+                static $index = 1;
29
+
30
+                $pdoStatement->bindParam($index++, $value);
31
+            });
32
+
33
+            $pdoStatement->execute();
34
+            $result = [];
35
+
36
+            while ($fetch = $pdoStatement->fetch()) {
37
+                $new = new static($this->getPdo());
38
+
39
+                $new->setId(intval($fetch['id']));
40
+                $new->setActiveRecordData($fetch);
41
+
42
+                $result[] = $new;
43
+            }
44
+
45
+            return $result;
46
+        } catch (\PDOException $e) {
47
+            throw new ActiveRecordException(sprintf('Can not search the record in the `%s` table.', $this->getActiveRecordName()), 0, $e);
48
+        }
49
+    }
50
+
51
+    /**
52
+     * Returns the search query with the given where, order by, limit and offset clauses.
53
+     *
54
+     * @param array $where = []
55
+     * @param array $orderBy = []
56
+     * @param int $limit = -1
57
+     * @param int $offset = 0
58
+     * @return string the search query with the given where, order by, limit and offset clauses.
59
+     */
60
+    private function getSearchQuery($where = [], $orderBy = [], $limit = -1, $offset = 0)
61
+    {
62
+        return sprintf(
63
+            'SELECT * FROM `%s` %s %s %s',
64
+            $this->getActiveRecordName(),
65
+            $this->getSearchQueryWhereClause($where),
66
+            $this->getSearchQueryOrderByClause($orderBy),
67
+            $this->getSearchQueryLimitClause($limit, $offset)
68
+        );
69
+    }
70
+
71
+    /**
72
+     * Returns the search query where clause.
73
+     *
74
+     * @param array $where
75
+     * @return string the search query where clause.
76
+     */
77
+    private function getSearchQueryWhereClause($where)
78
+    {
79
+        $columns = array_keys($this->getActiveRecordData());
80
+        $columns[] = 'id';
81
+        $result = [];
82
+
83
+        foreach ($where as $key => $value) {
84
+            if (!in_array($key, $columns)) {
85
+                throw new ActiveRecordException(sprintf('Search option key `%s` does not exists.', $key));
86
+            }
87
+
88
+            if (is_numeric($value)) {
89
+                $result[] = sprintf('`%s` = ?', $key);
90
+            } elseif (is_string($value)) {
91
+                $result[] = sprintf('`%s` LIKE ?', $key);
92
+            } elseif (is_null($value)) {
93
+                $result[] = sprintf('`%s` IS ?', $key);
94
+            } elseif (is_array($value) && !empty($value)) {
95
+                $result[] = sprintf('`%s` IN (%s)', $key, implode(',', array_fill(0, count($value), '?')));
96
+            } else {
97
+                throw new ActiveRecordException(sprintf('Search option value of key `%s` is not supported.', $key));
98
+            }
99
+        }
100
+
101
+        return empty($result) ? '' : 'WHERE ' . implode(' AND ', $result);
102
+    }
103
+
104
+    /**
105
+     * Returns the search query order by clause.
106
+     *
107
+     * @param array $orderBy
108
+     * @return string the search query order by clause.
109
+     */
110
+    private function getSearchQueryOrderByClause($orderBy)
111
+    {
112
+        $result = [];
113
+
114
+        foreach ($orderBy as $key => $value) {
115
+            $result[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC');
116
+        }
117
+
118
+        return empty($result) ? '' : 'ORDER BY ' . implode(', ', $result);
119
+    }
120
+
121
+    /**
122
+     * Returns the search query limit and clause.
123
+     *
124
+     * @param int $limit = -1
125
+     * @param int $offset = 0
126
+     * @return string the search query limit and clause.
127
+     */
128
+    private function getSearchQueryLimitClause($limit, $offset)
129
+    {
130
+        if ($limit == -1) {
131
+            return '';
132
+        }
133
+
134
+        return sprintf('LIMIT %d OFFSET %d', $limit, $offset);
135
+    }
136 136
 }
Please login to merge, or discard this patch.
src/ActiveRecordSearchInterface.php 1 patch
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -17,15 +17,15 @@
 block discarded – undo
17 17
  */
18 18
 interface ActiveRecordSearchInterface extends ActiveRecordInterface
19 19
 {
20
-	/**
21
-	 * Returns the records with the given where, order by, limit and offset clauses.
22
-	 *
23
-	 * @param array $where = []
24
-	 * @param array $orderBy = []
25
-	 * @param int $limit = -1
26
-	 * @param int $offset = 0
27
-	 * @return static[] the records with the given where, order by, limit and offset clauses.
28
-	 * @throws ActiveRecordException on failure.
29
-	 */
30
-	public function search($where = [], $orderBy = [], $limit = -1, $offset = 0);
20
+    /**
21
+     * Returns the records with the given where, order by, limit and offset clauses.
22
+     *
23
+     * @param array $where = []
24
+     * @param array $orderBy = []
25
+     * @param int $limit = -1
26
+     * @param int $offset = 0
27
+     * @return static[] the records with the given where, order by, limit and offset clauses.
28
+     * @throws ActiveRecordException on failure.
29
+     */
30
+    public function search($where = [], $orderBy = [], $limit = -1, $offset = 0);
31 31
 }
Please login to merge, or discard this patch.