Completed
Push — develop ( 81cd8e...6ff7cd )
by Michael
12:02 queued 08:26
created
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 searchOne(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 searchOne(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.
src/AbstractActiveRecord.php 1 patch
Indentation   +303 added lines, -303 removed lines patch added patch discarded remove patch
@@ -19,307 +19,307 @@
 block discarded – undo
19 19
  */
20 20
 abstract class AbstractActiveRecord implements ActiveRecordInterface
21 21
 {
22
-	/** @var \PDO The PDO object. */
23
-	private $pdo;
24
-
25
-	/** @var null|int The ID. */
26
-	private $id;
27
-
28
-	/**
29
-	 * Construct an abstract active record with the given PDO.
30
-	 *
31
-	 * @param \PDO $pdo
32
-	 */
33
-	public function __construct(\PDO $pdo)
34
-	{
35
-		$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
36
-		$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
37
-
38
-		$this->setPdo($pdo);
39
-	}
40
-
41
-	/**
42
-	 * {@inheritdoc}
43
-	 */
44
-	public function create()
45
-	{
46
-		try {
47
-			(new Query($this->getPdo(), $this->getActiveRecordTable()))
48
-				->insert($this->getActiveRecordColumns())
49
-				->execute();
50
-
51
-			$this->setId(intval($this->getPdo()->lastInsertId()));
52
-		} catch (\PDOException $e) {
53
-			throw new ActiveRecordException($e->getMessage(), 0, $e);
54
-		}
55
-
56
-		return $this;
57
-	}
58
-
59
-	/**
60
-	 * {@inheritdoc}
61
-	 */
62
-	public function read($id)
63
-	{
64
-		try {
65
-			$row = (new Query($this->getPdo(), $this->getActiveRecordTable()))
66
-				->select()
67
-				->where('id', '=', $id)
68
-				->execute()
69
-				->fetch();
70
-
71
-			if ($row === false) {
72
-				throw new ActiveRecordException(sprintf('Can not read the non-existent active record entry %d from the `%s` table.', $id, $this->getActiveRecordTable()));
73
-			}
74
-
75
-			$this->fill($row)->setId($id);
76
-		} catch (\PDOException $e) {
77
-			throw new ActiveRecordException($e->getMessage(), 0, $e);
78
-		}
79
-
80
-		return $this;
81
-	}
82
-
83
-	/**
84
-	 * {@inheritdoc}
85
-	 */
86
-	public function update()
87
-	{
88
-		try {
89
-			(new Query($this->getPdo(), $this->getActiveRecordTable()))
90
-				->update($this->getActiveRecordColumns())
91
-				->where('id', '=', $this->getId())
92
-				->execute();
93
-		} catch (\PDOException $e) {
94
-			throw new ActiveRecordException($e->getMessage(), 0, $e);
95
-		}
96
-
97
-		return $this;
98
-	}
99
-
100
-	/**
101
-	 * {@inheritdoc}
102
-	 */
103
-	public function delete()
104
-	{
105
-		try {
106
-			(new Query($this->getPdo(), $this->getActiveRecordTable()))
107
-				->delete()
108
-				->where('id', '=', $this->getId())
109
-				->execute();
110
-
111
-			$this->setId(null);
112
-		} catch (\PDOException $e) {
113
-			throw new ActiveRecordException($e->getMessage(), 0, $e);
114
-		}
115
-
116
-		return $this;
117
-	}
118
-
119
-	/**
120
-	 * {@inheritdoc}
121
-	 */
122
-	public function sync()
123
-	{
124
-		if (!$this->exists()) {
125
-			return $this->create();
126
-		}
127
-
128
-		return $this->update();
129
-	}
130
-
131
-	/**
132
-	 * {@inheritdoc}
133
-	 */
134
-	public function exists()
135
-	{
136
-		return $this->getId() !== null;
137
-	}
138
-
139
-	/**
140
-	 * {@inheritdoc}
141
-	 */
142
-	public function fill(array $attributes)
143
-	{
144
-		$columns = $this->getActiveRecordColumns();
145
-		$columns['id'] = &$this->id;
146
-
147
-		foreach ($attributes as $key => $value) {
148
-			if (array_key_exists($key, $columns)) {
149
-				$columns[$key] = $value;
150
-			}
151
-		}
152
-
153
-		return $this;
154
-	}
155
-
156
-	/**
157
-	 * {@inheritdoc}
158
-	 */
159
-	public function searchOne(array $where = [], array $orderBy = [])
160
-	{
161
-		try {
162
-			$row = $this->getSearchQueryResult($where, $orderBy, 1)->fetch();
163
-
164
-			if ($row === false) {
165
-				throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->getActiveRecordTable()));
166
-			}
167
-
168
-			return $this->fill($row)->setId($row['id']);
169
-		} catch (\PDOException $e) {
170
-			throw new ActiveRecordException($e->getMessage(), 0, $e);
171
-		}
172
-	}
173
-
174
-	/**
175
-	 * {@inheritdoc}
176
-	 */
177
-	public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0)
178
-	{
179
-		try {
180
-			$queryResult = $this->getSearchQueryResult($where, $orderBy, $limit, $offset);
181
-			$result = [];
182
-
183
-			foreach ($queryResult as $row) {
184
-				$new = clone $this;
185
-
186
-				$result[] = $new->fill($row)->setId($row['id']);
187
-			}
188
-
189
-			return $result;
190
-		} catch (\PDOException $e) {
191
-			throw new ActiveRecordException($e->getMessage(), 0, $e);
192
-		}
193
-	}
194
-
195
-	/**
196
-	 * Returns the search query result with the given where, order by, limit and offset clauses.
197
-	 *
198
-	 * @param array $where = []
199
-	 * @param array $orderBy = []
200
-	 * @param int $limit = -1
201
-	 * @param int $offset = 0
202
-	 * @return \miBadger\Query\QueryResult the search query result with the given where, order by, limit and offset clauses.
203
-	 */
204
-	private function getSearchQueryResult(array $where = [], array $orderBy = [], $limit = -1, $offset = 0)
205
-	{
206
-		$query = (new Query($this->getPdo(), $this->getActiveRecordTable()))
207
-			->select();
208
-
209
-		$this->getSearchQueryWhere($query, $where);
210
-		$this->getSearchQueryOrderBy($query, $orderBy);
211
-		$this->getSearchQueryLimit($query, $limit, $offset);
212
-
213
-		return $query->execute();
214
-	}
215
-
216
-	/**
217
-	 * Returns the given query after adding the given where conditions.
218
-	 *
219
-	 * @param \miBadger\Query\Query $query
220
-	 * @param array $where
221
-	 * @return \miBadger\Query\Query the given query after adding the given where conditions.
222
-	 */
223
-	private function getSearchQueryWhere($query, $where)
224
-	{
225
-		foreach ($where as $key => $value) {
226
-			$query->where($value[0], $value[1], $value[2]);
227
-		}
228
-
229
-		return $query;
230
-	}
231
-
232
-	/**
233
-	 * Returns the given query after adding the given order by conditions.
234
-	 *
235
-	 * @param \miBadger\Query\Query $query
236
-	 * @param array $orderBy
237
-	 * @return \miBadger\Query\Query the given query after adding the given order by conditions.
238
-	 */
239
-	private function getSearchQueryOrderBy($query, $orderBy)
240
-	{
241
-		foreach ($orderBy as $key => $value) {
242
-			$query->orderBy($key, $value);
243
-		}
244
-
245
-		return $query;
246
-	}
247
-
248
-	/**
249
-	 * Returns the given query after adding the given limit and offset conditions.
250
-	 *
251
-	 * @param \miBadger\Query\Query $query
252
-	 * @param int $limit
253
-	 * @param int $offset
254
-	 * @return \miBadger\Query\Query the given query after adding the given limit and offset conditions.
255
-	 */
256
-	private function getSearchQueryLimit($query, $limit, $offset)
257
-	{
258
-		if ($limit > -1) {
259
-			$query->limit($limit);
260
-			$query->offset($offset);
261
-		}
262
-
263
-		return $query;
264
-	}
265
-
266
-	/**
267
-	 * Returns the PDO.
268
-	 *
269
-	 * @return \PDO the PDO.
270
-	 */
271
-	public function getPdo()
272
-	{
273
-		return $this->pdo;
274
-	}
275
-
276
-	/**
277
-	 * Set the PDO.
278
-	 *
279
-	 * @param \PDO $pdo
280
-	 * @return $this
281
-	 */
282
-	protected function setPdo($pdo)
283
-	{
284
-		$this->pdo = $pdo;
285
-
286
-		return $this;
287
-	}
288
-
289
-	/**
290
-	 * Returns the ID.
291
-	 *
292
-	 * @return null|int The ID.
293
-	 */
294
-	public function getId()
295
-	{
296
-		return $this->id;
297
-	}
298
-
299
-	/**
300
-	 * Set the ID.
301
-	 *
302
-	 * @param int $id
303
-	 * @return $this
304
-	 */
305
-	protected function setId($id)
306
-	{
307
-		$this->id = $id;
308
-
309
-		return $this;
310
-	}
311
-
312
-	/**
313
-	 * Returns the active record table.
314
-	 *
315
-	 * @return string the active record table.
316
-	 */
317
-	abstract protected function getActiveRecordTable();
318
-
319
-	/**
320
-	 * Returns the active record columns.
321
-	 *
322
-	 * @return array the active record columns.
323
-	 */
324
-	abstract protected function getActiveRecordColumns();
22
+    /** @var \PDO The PDO object. */
23
+    private $pdo;
24
+
25
+    /** @var null|int The ID. */
26
+    private $id;
27
+
28
+    /**
29
+     * Construct an abstract active record with the given PDO.
30
+     *
31
+     * @param \PDO $pdo
32
+     */
33
+    public function __construct(\PDO $pdo)
34
+    {
35
+        $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
36
+        $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
37
+
38
+        $this->setPdo($pdo);
39
+    }
40
+
41
+    /**
42
+     * {@inheritdoc}
43
+     */
44
+    public function create()
45
+    {
46
+        try {
47
+            (new Query($this->getPdo(), $this->getActiveRecordTable()))
48
+                ->insert($this->getActiveRecordColumns())
49
+                ->execute();
50
+
51
+            $this->setId(intval($this->getPdo()->lastInsertId()));
52
+        } catch (\PDOException $e) {
53
+            throw new ActiveRecordException($e->getMessage(), 0, $e);
54
+        }
55
+
56
+        return $this;
57
+    }
58
+
59
+    /**
60
+     * {@inheritdoc}
61
+     */
62
+    public function read($id)
63
+    {
64
+        try {
65
+            $row = (new Query($this->getPdo(), $this->getActiveRecordTable()))
66
+                ->select()
67
+                ->where('id', '=', $id)
68
+                ->execute()
69
+                ->fetch();
70
+
71
+            if ($row === false) {
72
+                throw new ActiveRecordException(sprintf('Can not read the non-existent active record entry %d from the `%s` table.', $id, $this->getActiveRecordTable()));
73
+            }
74
+
75
+            $this->fill($row)->setId($id);
76
+        } catch (\PDOException $e) {
77
+            throw new ActiveRecordException($e->getMessage(), 0, $e);
78
+        }
79
+
80
+        return $this;
81
+    }
82
+
83
+    /**
84
+     * {@inheritdoc}
85
+     */
86
+    public function update()
87
+    {
88
+        try {
89
+            (new Query($this->getPdo(), $this->getActiveRecordTable()))
90
+                ->update($this->getActiveRecordColumns())
91
+                ->where('id', '=', $this->getId())
92
+                ->execute();
93
+        } catch (\PDOException $e) {
94
+            throw new ActiveRecordException($e->getMessage(), 0, $e);
95
+        }
96
+
97
+        return $this;
98
+    }
99
+
100
+    /**
101
+     * {@inheritdoc}
102
+     */
103
+    public function delete()
104
+    {
105
+        try {
106
+            (new Query($this->getPdo(), $this->getActiveRecordTable()))
107
+                ->delete()
108
+                ->where('id', '=', $this->getId())
109
+                ->execute();
110
+
111
+            $this->setId(null);
112
+        } catch (\PDOException $e) {
113
+            throw new ActiveRecordException($e->getMessage(), 0, $e);
114
+        }
115
+
116
+        return $this;
117
+    }
118
+
119
+    /**
120
+     * {@inheritdoc}
121
+     */
122
+    public function sync()
123
+    {
124
+        if (!$this->exists()) {
125
+            return $this->create();
126
+        }
127
+
128
+        return $this->update();
129
+    }
130
+
131
+    /**
132
+     * {@inheritdoc}
133
+     */
134
+    public function exists()
135
+    {
136
+        return $this->getId() !== null;
137
+    }
138
+
139
+    /**
140
+     * {@inheritdoc}
141
+     */
142
+    public function fill(array $attributes)
143
+    {
144
+        $columns = $this->getActiveRecordColumns();
145
+        $columns['id'] = &$this->id;
146
+
147
+        foreach ($attributes as $key => $value) {
148
+            if (array_key_exists($key, $columns)) {
149
+                $columns[$key] = $value;
150
+            }
151
+        }
152
+
153
+        return $this;
154
+    }
155
+
156
+    /**
157
+     * {@inheritdoc}
158
+     */
159
+    public function searchOne(array $where = [], array $orderBy = [])
160
+    {
161
+        try {
162
+            $row = $this->getSearchQueryResult($where, $orderBy, 1)->fetch();
163
+
164
+            if ($row === false) {
165
+                throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->getActiveRecordTable()));
166
+            }
167
+
168
+            return $this->fill($row)->setId($row['id']);
169
+        } catch (\PDOException $e) {
170
+            throw new ActiveRecordException($e->getMessage(), 0, $e);
171
+        }
172
+    }
173
+
174
+    /**
175
+     * {@inheritdoc}
176
+     */
177
+    public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0)
178
+    {
179
+        try {
180
+            $queryResult = $this->getSearchQueryResult($where, $orderBy, $limit, $offset);
181
+            $result = [];
182
+
183
+            foreach ($queryResult as $row) {
184
+                $new = clone $this;
185
+
186
+                $result[] = $new->fill($row)->setId($row['id']);
187
+            }
188
+
189
+            return $result;
190
+        } catch (\PDOException $e) {
191
+            throw new ActiveRecordException($e->getMessage(), 0, $e);
192
+        }
193
+    }
194
+
195
+    /**
196
+     * Returns the search query result with the given where, order by, limit and offset clauses.
197
+     *
198
+     * @param array $where = []
199
+     * @param array $orderBy = []
200
+     * @param int $limit = -1
201
+     * @param int $offset = 0
202
+     * @return \miBadger\Query\QueryResult the search query result with the given where, order by, limit and offset clauses.
203
+     */
204
+    private function getSearchQueryResult(array $where = [], array $orderBy = [], $limit = -1, $offset = 0)
205
+    {
206
+        $query = (new Query($this->getPdo(), $this->getActiveRecordTable()))
207
+            ->select();
208
+
209
+        $this->getSearchQueryWhere($query, $where);
210
+        $this->getSearchQueryOrderBy($query, $orderBy);
211
+        $this->getSearchQueryLimit($query, $limit, $offset);
212
+
213
+        return $query->execute();
214
+    }
215
+
216
+    /**
217
+     * Returns the given query after adding the given where conditions.
218
+     *
219
+     * @param \miBadger\Query\Query $query
220
+     * @param array $where
221
+     * @return \miBadger\Query\Query the given query after adding the given where conditions.
222
+     */
223
+    private function getSearchQueryWhere($query, $where)
224
+    {
225
+        foreach ($where as $key => $value) {
226
+            $query->where($value[0], $value[1], $value[2]);
227
+        }
228
+
229
+        return $query;
230
+    }
231
+
232
+    /**
233
+     * Returns the given query after adding the given order by conditions.
234
+     *
235
+     * @param \miBadger\Query\Query $query
236
+     * @param array $orderBy
237
+     * @return \miBadger\Query\Query the given query after adding the given order by conditions.
238
+     */
239
+    private function getSearchQueryOrderBy($query, $orderBy)
240
+    {
241
+        foreach ($orderBy as $key => $value) {
242
+            $query->orderBy($key, $value);
243
+        }
244
+
245
+        return $query;
246
+    }
247
+
248
+    /**
249
+     * Returns the given query after adding the given limit and offset conditions.
250
+     *
251
+     * @param \miBadger\Query\Query $query
252
+     * @param int $limit
253
+     * @param int $offset
254
+     * @return \miBadger\Query\Query the given query after adding the given limit and offset conditions.
255
+     */
256
+    private function getSearchQueryLimit($query, $limit, $offset)
257
+    {
258
+        if ($limit > -1) {
259
+            $query->limit($limit);
260
+            $query->offset($offset);
261
+        }
262
+
263
+        return $query;
264
+    }
265
+
266
+    /**
267
+     * Returns the PDO.
268
+     *
269
+     * @return \PDO the PDO.
270
+     */
271
+    public function getPdo()
272
+    {
273
+        return $this->pdo;
274
+    }
275
+
276
+    /**
277
+     * Set the PDO.
278
+     *
279
+     * @param \PDO $pdo
280
+     * @return $this
281
+     */
282
+    protected function setPdo($pdo)
283
+    {
284
+        $this->pdo = $pdo;
285
+
286
+        return $this;
287
+    }
288
+
289
+    /**
290
+     * Returns the ID.
291
+     *
292
+     * @return null|int The ID.
293
+     */
294
+    public function getId()
295
+    {
296
+        return $this->id;
297
+    }
298
+
299
+    /**
300
+     * Set the ID.
301
+     *
302
+     * @param int $id
303
+     * @return $this
304
+     */
305
+    protected function setId($id)
306
+    {
307
+        $this->id = $id;
308
+
309
+        return $this;
310
+    }
311
+
312
+    /**
313
+     * Returns the active record table.
314
+     *
315
+     * @return string the active record table.
316
+     */
317
+    abstract protected function getActiveRecordTable();
318
+
319
+    /**
320
+     * Returns the active record columns.
321
+     *
322
+     * @return array the active record columns.
323
+     */
324
+    abstract protected function getActiveRecordColumns();
325 325
 }
Please login to merge, or discard this patch.