Completed
Push — develop ( 4f411d...ee5a2d )
by Michael
04:34
created
src/AbstractActiveRecord.php 1 patch
Indentation   +314 added lines, -314 removed lines patch added patch discarded remove patch
@@ -19,318 +19,318 @@
 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
-			$result = (new Query($this->getPdo(), $this->getActiveRecordTable()))
66
-				->select()
67
-				->where('id', '=', $id)
68
-				->execute()
69
-				->fetch();
70
-
71
-			if ($result === 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($result);
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
-		if (!$this->exists()) {
89
-			throw new ActiveRecordException(sprintf('Can not update a non-existent active record entry to the `%s` table.', $this->getActiveRecordTable()));
90
-		}
91
-
92
-		try {
93
-			(new Query($this->getPdo(), $this->getActiveRecordTable()))
94
-				->update($this->getActiveRecordColumns())
95
-				->where('id', '=', $this->getId())
96
-				->execute();
97
-		} catch (\PDOException $e) {
98
-			throw new ActiveRecordException($e->getMessage(), 0, $e);
99
-		}
100
-
101
-		return $this;
102
-	}
103
-
104
-	/**
105
-	 * {@inheritdoc}
106
-	 */
107
-	public function delete()
108
-	{
109
-		if (!$this->exists()) {
110
-			throw new ActiveRecordException(sprintf('Can not delete a non-existent active record entry from the `%s` table.', $this->getActiveRecordTable()));
111
-		}
112
-
113
-		try {
114
-			(new Query($this->getPdo(), $this->getActiveRecordTable()))
115
-				->delete()
116
-				->where('id', '=', $this->getId())
117
-				->execute();
118
-
119
-			$this->setId(null);
120
-		} catch (\PDOException $e) {
121
-			throw new ActiveRecordException($e->getMessage(), 0, $e);
122
-		}
123
-
124
-		return $this;
125
-	}
126
-
127
-	/**
128
-	 * {@inheritdoc}
129
-	 */
130
-	public function sync()
131
-	{
132
-		if (!$this->exists()) {
133
-			return $this->create();
134
-		}
135
-
136
-		return $this->update();
137
-	}
138
-
139
-	/**
140
-	 * {@inheritdoc}
141
-	 */
142
-	public function exists()
143
-	{
144
-		return $this->getId() !== null;
145
-	}
146
-
147
-	/**
148
-	 * {@inheritdoc}
149
-	 */
150
-	public function fill(array $attributes)
151
-	{
152
-		if (isset($attributes['id'])) {
153
-			$this->setId($attributes['id']);
154
-		}
155
-
156
-		foreach ($this->getActiveRecordColumns() as $key => &$value) {
157
-			if (!array_key_exists($key, $attributes)) {
158
-				throw new ActiveRecordException(sprintf('Can not read the expected column `%s`. It\'s not returnd by the `%s` table', $key, $this->getActiveRecordTable()));
159
-			}
160
-
161
-			$value = $attributes[$key];
162
-		}
163
-
164
-		return $this;
165
-	}
166
-
167
-	/**
168
-	 * {@inheritdoc}
169
-	 */
170
-	public function searchOne(array $where = [], array $orderBy = [])
171
-	{
172
-		try {
173
-			$result = $this->getSearchQueryResult($where, $orderBy, 1)->fetch();
174
-
175
-			if ($result === false) {
176
-				throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->getActiveRecordTable()));
177
-			}
178
-
179
-			return $this->fill($result);
180
-		} catch (\PDOException $e) {
181
-			throw new ActiveRecordException($e->getMessage(), 0, $e);
182
-		}
183
-	}
184
-
185
-	/**
186
-	 * {@inheritdoc}
187
-	 */
188
-	public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0)
189
-	{
190
-		try {
191
-			$queryResult = $this->getSearchQueryResult($where, $orderBy, $limit, $offset);
192
-			$result = [];
193
-
194
-			foreach ($queryResult as $fetch) {
195
-				$new = clone $this;
196
-
197
-				$result[] = $new->fill($fetch);
198
-			}
199
-
200
-			return $result;
201
-		} catch (\PDOException $e) {
202
-			throw new ActiveRecordException($e->getMessage(), 0, $e);
203
-		}
204
-	}
205
-
206
-	/**
207
-	 * Returns the search query result with the given where, order by, limit and offset clauses.
208
-	 *
209
-	 * @param array $where = []
210
-	 * @param array $orderBy = []
211
-	 * @param int $limit = -1
212
-	 * @param int $offset = 0
213
-	 * @return \miBadger\Query\QueryResult the search query result with the given where, order by, limit and offset clauses.
214
-	 */
215
-	private function getSearchQueryResult(array $where = [], array $orderBy = [], $limit = -1, $offset = 0)
216
-	{
217
-		$query = (new Query($this->getPdo(), $this->getActiveRecordTable()))
218
-			->select();
219
-
220
-		$this->getSearchQueryWhere($query, $where);
221
-		$this->getSearchQueryOrderBy($query, $orderBy);
222
-		$this->getSearchQueryLimit($query, $limit, $offset);
223
-
224
-		return $query->execute();
225
-	}
226
-
227
-	/**
228
-	 * Returns the given query after adding the given where conditions.
229
-	 *
230
-	 * @param \miBadger\Query\Query $query
231
-	 * @param array $where
232
-	 * @return \miBadger\Query\Query the given query after adding the given where conditions.
233
-	 */
234
-	private function getSearchQueryWhere($query, $where)
235
-	{
236
-		foreach ($where as $key => $value) {
237
-			$query->where($value[0], $value[1], $value[2]);
238
-		}
239
-
240
-		return $query;
241
-	}
242
-
243
-	/**
244
-	 * Returns the given query after adding the given order by conditions.
245
-	 *
246
-	 * @param \miBadger\Query\Query $query
247
-	 * @param array $orderBy
248
-	 * @return \miBadger\Query\Query the given query after adding the given order by conditions.
249
-	 */
250
-	private function getSearchQueryOrderBy($query, $orderBy)
251
-	{
252
-		foreach ($orderBy as $key => $value) {
253
-			$query->orderBy($key, $value);
254
-		}
255
-
256
-		return $query;
257
-	}
258
-
259
-	/**
260
-	 * Returns the given query after adding the given limit and offset conditions.
261
-	 *
262
-	 * @param \miBadger\Query\Query $query
263
-	 * @param int $limit
264
-	 * @param int $offset
265
-	 * @return \miBadger\Query\Query the given query after adding the given limit and offset conditions.
266
-	 */
267
-	private function getSearchQueryLimit($query, $limit, $offset)
268
-	{
269
-		if ($limit > -1) {
270
-			$query->limit($limit);
271
-			$query->offset($offset);
272
-		}
273
-
274
-		return $query;
275
-	}
276
-
277
-	/**
278
-	 * Returns the PDO.
279
-	 *
280
-	 * @return \PDO the PDO.
281
-	 */
282
-	public function getPdo()
283
-	{
284
-		return $this->pdo;
285
-	}
286
-
287
-	/**
288
-	 * Set the PDO.
289
-	 *
290
-	 * @param \PDO $pdo
291
-	 * @return $this
292
-	 */
293
-	protected function setPdo($pdo)
294
-	{
295
-		$this->pdo = $pdo;
296
-
297
-		return $this;
298
-	}
299
-
300
-	/**
301
-	 * Returns the ID.
302
-	 *
303
-	 * @return null|int The ID.
304
-	 */
305
-	public function getId()
306
-	{
307
-		return $this->id;
308
-	}
309
-
310
-	/**
311
-	 * Set the ID.
312
-	 *
313
-	 * @param int $id
314
-	 * @return $this
315
-	 */
316
-	protected function setId($id)
317
-	{
318
-		$this->id = $id;
319
-
320
-		return $this;
321
-	}
322
-
323
-	/**
324
-	 * Returns the active record table.
325
-	 *
326
-	 * @return string the active record table.
327
-	 */
328
-	abstract protected function getActiveRecordTable();
329
-
330
-	/**
331
-	 * Returns the active record columns.
332
-	 *
333
-	 * @return array the active record columns.
334
-	 */
335
-	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
+            $result = (new Query($this->getPdo(), $this->getActiveRecordTable()))
66
+                ->select()
67
+                ->where('id', '=', $id)
68
+                ->execute()
69
+                ->fetch();
70
+
71
+            if ($result === 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($result);
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
+        if (!$this->exists()) {
89
+            throw new ActiveRecordException(sprintf('Can not update a non-existent active record entry to the `%s` table.', $this->getActiveRecordTable()));
90
+        }
91
+
92
+        try {
93
+            (new Query($this->getPdo(), $this->getActiveRecordTable()))
94
+                ->update($this->getActiveRecordColumns())
95
+                ->where('id', '=', $this->getId())
96
+                ->execute();
97
+        } catch (\PDOException $e) {
98
+            throw new ActiveRecordException($e->getMessage(), 0, $e);
99
+        }
100
+
101
+        return $this;
102
+    }
103
+
104
+    /**
105
+     * {@inheritdoc}
106
+     */
107
+    public function delete()
108
+    {
109
+        if (!$this->exists()) {
110
+            throw new ActiveRecordException(sprintf('Can not delete a non-existent active record entry from the `%s` table.', $this->getActiveRecordTable()));
111
+        }
112
+
113
+        try {
114
+            (new Query($this->getPdo(), $this->getActiveRecordTable()))
115
+                ->delete()
116
+                ->where('id', '=', $this->getId())
117
+                ->execute();
118
+
119
+            $this->setId(null);
120
+        } catch (\PDOException $e) {
121
+            throw new ActiveRecordException($e->getMessage(), 0, $e);
122
+        }
123
+
124
+        return $this;
125
+    }
126
+
127
+    /**
128
+     * {@inheritdoc}
129
+     */
130
+    public function sync()
131
+    {
132
+        if (!$this->exists()) {
133
+            return $this->create();
134
+        }
135
+
136
+        return $this->update();
137
+    }
138
+
139
+    /**
140
+     * {@inheritdoc}
141
+     */
142
+    public function exists()
143
+    {
144
+        return $this->getId() !== null;
145
+    }
146
+
147
+    /**
148
+     * {@inheritdoc}
149
+     */
150
+    public function fill(array $attributes)
151
+    {
152
+        if (isset($attributes['id'])) {
153
+            $this->setId($attributes['id']);
154
+        }
155
+
156
+        foreach ($this->getActiveRecordColumns() as $key => &$value) {
157
+            if (!array_key_exists($key, $attributes)) {
158
+                throw new ActiveRecordException(sprintf('Can not read the expected column `%s`. It\'s not returnd by the `%s` table', $key, $this->getActiveRecordTable()));
159
+            }
160
+
161
+            $value = $attributes[$key];
162
+        }
163
+
164
+        return $this;
165
+    }
166
+
167
+    /**
168
+     * {@inheritdoc}
169
+     */
170
+    public function searchOne(array $where = [], array $orderBy = [])
171
+    {
172
+        try {
173
+            $result = $this->getSearchQueryResult($where, $orderBy, 1)->fetch();
174
+
175
+            if ($result === false) {
176
+                throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->getActiveRecordTable()));
177
+            }
178
+
179
+            return $this->fill($result);
180
+        } catch (\PDOException $e) {
181
+            throw new ActiveRecordException($e->getMessage(), 0, $e);
182
+        }
183
+    }
184
+
185
+    /**
186
+     * {@inheritdoc}
187
+     */
188
+    public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0)
189
+    {
190
+        try {
191
+            $queryResult = $this->getSearchQueryResult($where, $orderBy, $limit, $offset);
192
+            $result = [];
193
+
194
+            foreach ($queryResult as $fetch) {
195
+                $new = clone $this;
196
+
197
+                $result[] = $new->fill($fetch);
198
+            }
199
+
200
+            return $result;
201
+        } catch (\PDOException $e) {
202
+            throw new ActiveRecordException($e->getMessage(), 0, $e);
203
+        }
204
+    }
205
+
206
+    /**
207
+     * Returns the search query result with the given where, order by, limit and offset clauses.
208
+     *
209
+     * @param array $where = []
210
+     * @param array $orderBy = []
211
+     * @param int $limit = -1
212
+     * @param int $offset = 0
213
+     * @return \miBadger\Query\QueryResult the search query result with the given where, order by, limit and offset clauses.
214
+     */
215
+    private function getSearchQueryResult(array $where = [], array $orderBy = [], $limit = -1, $offset = 0)
216
+    {
217
+        $query = (new Query($this->getPdo(), $this->getActiveRecordTable()))
218
+            ->select();
219
+
220
+        $this->getSearchQueryWhere($query, $where);
221
+        $this->getSearchQueryOrderBy($query, $orderBy);
222
+        $this->getSearchQueryLimit($query, $limit, $offset);
223
+
224
+        return $query->execute();
225
+    }
226
+
227
+    /**
228
+     * Returns the given query after adding the given where conditions.
229
+     *
230
+     * @param \miBadger\Query\Query $query
231
+     * @param array $where
232
+     * @return \miBadger\Query\Query the given query after adding the given where conditions.
233
+     */
234
+    private function getSearchQueryWhere($query, $where)
235
+    {
236
+        foreach ($where as $key => $value) {
237
+            $query->where($value[0], $value[1], $value[2]);
238
+        }
239
+
240
+        return $query;
241
+    }
242
+
243
+    /**
244
+     * Returns the given query after adding the given order by conditions.
245
+     *
246
+     * @param \miBadger\Query\Query $query
247
+     * @param array $orderBy
248
+     * @return \miBadger\Query\Query the given query after adding the given order by conditions.
249
+     */
250
+    private function getSearchQueryOrderBy($query, $orderBy)
251
+    {
252
+        foreach ($orderBy as $key => $value) {
253
+            $query->orderBy($key, $value);
254
+        }
255
+
256
+        return $query;
257
+    }
258
+
259
+    /**
260
+     * Returns the given query after adding the given limit and offset conditions.
261
+     *
262
+     * @param \miBadger\Query\Query $query
263
+     * @param int $limit
264
+     * @param int $offset
265
+     * @return \miBadger\Query\Query the given query after adding the given limit and offset conditions.
266
+     */
267
+    private function getSearchQueryLimit($query, $limit, $offset)
268
+    {
269
+        if ($limit > -1) {
270
+            $query->limit($limit);
271
+            $query->offset($offset);
272
+        }
273
+
274
+        return $query;
275
+    }
276
+
277
+    /**
278
+     * Returns the PDO.
279
+     *
280
+     * @return \PDO the PDO.
281
+     */
282
+    public function getPdo()
283
+    {
284
+        return $this->pdo;
285
+    }
286
+
287
+    /**
288
+     * Set the PDO.
289
+     *
290
+     * @param \PDO $pdo
291
+     * @return $this
292
+     */
293
+    protected function setPdo($pdo)
294
+    {
295
+        $this->pdo = $pdo;
296
+
297
+        return $this;
298
+    }
299
+
300
+    /**
301
+     * Returns the ID.
302
+     *
303
+     * @return null|int The ID.
304
+     */
305
+    public function getId()
306
+    {
307
+        return $this->id;
308
+    }
309
+
310
+    /**
311
+     * Set the ID.
312
+     *
313
+     * @param int $id
314
+     * @return $this
315
+     */
316
+    protected function setId($id)
317
+    {
318
+        $this->id = $id;
319
+
320
+        return $this;
321
+    }
322
+
323
+    /**
324
+     * Returns the active record table.
325
+     *
326
+     * @return string the active record table.
327
+     */
328
+    abstract protected function getActiveRecordTable();
329
+
330
+    /**
331
+     * Returns the active record columns.
332
+     *
333
+     * @return array the active record columns.
334
+     */
335
+    abstract protected function getActiveRecordColumns();
336 336
 }
Please login to merge, or discard this patch.