Completed
Push — master ( b05d4c...281aaa )
by Michael
03:36
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/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.
src/AbstractActiveRecord.php 1 patch
Indentation   +238 added lines, -238 removed lines patch added patch discarded remove patch
@@ -17,242 +17,242 @@
 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[] = sprintf(':%s', $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
-			$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->getActiveRecordName()));
85
-			}
86
-
87
-			$this->setActiveRecordData($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->getActiveRecordName()), 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->getActiveRecordName());
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->getActiveRecordName()));
113
-		}
114
-
115
-		try {
116
-			$pdoStatement = $this->getPdo()->prepare($this->getUpdateQuery());
117
-			$pdoStatement->execute(['id' => $this->getId()] + $this->getActiveRecordData());
118
-		} catch (\PDOException $e) {
119
-			throw new ActiveRecordException(sprintf('Can not update active record entry %d to the `%s` table.', $this->getId(), $this->getActiveRecordName()), 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->getActiveRecordData()) as $key => $value) {
135
-			$values[] = sprintf('`%s` = :%s', $value, $value);
136
-		}
137
-
138
-		return sprintf('UPDATE `%s` SET %s WHERE `id` = :id', $this->getActiveRecordName(), 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->getActiveRecordName()));
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->getActiveRecordName()), 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->getActiveRecordName());
170
-	}
171
-
172
-	/**
173
-	 * {@inheritdoc}
174
-	 */
175
-	public function exists()
176
-	{
177
-		return $this->getId() !== null;
178
-	}
179
-
180
-	/**
181
-	 * Returns the PDO.
182
-	 *
183
-	 * @return \PDO the PDO.
184
-	 */
185
-	public function getPdo()
186
-	{
187
-		return $this->pdo;
188
-	}
189
-
190
-	/**
191
-	 * Set the PDO.
192
-	 *
193
-	 * @param \PDO $pdo
194
-	 * @return $this
195
-	 */
196
-	protected function setPdo($pdo)
197
-	{
198
-		$this->pdo = $pdo;
199
-
200
-		return $this;
201
-	}
202
-
203
-	/**
204
-	 * Returns the ID.
205
-	 *
206
-	 * @return null|int The ID.
207
-	 */
208
-	public function getId()
209
-	{
210
-		return $this->id;
211
-	}
212
-
213
-	/**
214
-	 * Set the ID.
215
-	 *
216
-	 * @param int $id
217
-	 * @return $this
218
-	 */
219
-	protected function setId($id)
220
-	{
221
-		$this->id = $id;
222
-
223
-		return $this;
224
-	}
225
-
226
-	/**
227
-	 * Returns the active record name.
228
-	 *
229
-	 * @return string the active record name.
230
-	 */
231
-	abstract protected function getActiveRecordName();
232
-
233
-	/**
234
-	 * Returns the active record data.
235
-	 *
236
-	 * @return array the active record data.
237
-	 */
238
-	abstract protected function getActiveRecordData();
239
-
240
-	/**
241
-	 * Set the active record data.
242
-	 *
243
-	 * @param array $fetch
244
-	 * @return null
245
-	 */
246
-	protected function setActiveRecordData(array $fetch)
247
-	{
248
-		$data = $this->getActiveRecordData();
249
-
250
-		foreach ($data as $key => &$value) {
251
-			if (!array_key_exists($key, $fetch)) {
252
-				throw new ActiveRecordException(sprintf('Can not read the expected column `%s`. It\'s not returnd by the `%s` table', $key, $this->getActiveRecordName()));
253
-			}
254
-
255
-			$value = $fetch[$key];
256
-		}
257
-	}
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[] = sprintf(':%s', $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
+            $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->getActiveRecordName()));
85
+            }
86
+
87
+            $this->setActiveRecordData($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->getActiveRecordName()), 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->getActiveRecordName());
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->getActiveRecordName()));
113
+        }
114
+
115
+        try {
116
+            $pdoStatement = $this->getPdo()->prepare($this->getUpdateQuery());
117
+            $pdoStatement->execute(['id' => $this->getId()] + $this->getActiveRecordData());
118
+        } catch (\PDOException $e) {
119
+            throw new ActiveRecordException(sprintf('Can not update active record entry %d to the `%s` table.', $this->getId(), $this->getActiveRecordName()), 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->getActiveRecordData()) as $key => $value) {
135
+            $values[] = sprintf('`%s` = :%s', $value, $value);
136
+        }
137
+
138
+        return sprintf('UPDATE `%s` SET %s WHERE `id` = :id', $this->getActiveRecordName(), 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->getActiveRecordName()));
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->getActiveRecordName()), 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->getActiveRecordName());
170
+    }
171
+
172
+    /**
173
+     * {@inheritdoc}
174
+     */
175
+    public function exists()
176
+    {
177
+        return $this->getId() !== null;
178
+    }
179
+
180
+    /**
181
+     * Returns the PDO.
182
+     *
183
+     * @return \PDO the PDO.
184
+     */
185
+    public function getPdo()
186
+    {
187
+        return $this->pdo;
188
+    }
189
+
190
+    /**
191
+     * Set the PDO.
192
+     *
193
+     * @param \PDO $pdo
194
+     * @return $this
195
+     */
196
+    protected function setPdo($pdo)
197
+    {
198
+        $this->pdo = $pdo;
199
+
200
+        return $this;
201
+    }
202
+
203
+    /**
204
+     * Returns the ID.
205
+     *
206
+     * @return null|int The ID.
207
+     */
208
+    public function getId()
209
+    {
210
+        return $this->id;
211
+    }
212
+
213
+    /**
214
+     * Set the ID.
215
+     *
216
+     * @param int $id
217
+     * @return $this
218
+     */
219
+    protected function setId($id)
220
+    {
221
+        $this->id = $id;
222
+
223
+        return $this;
224
+    }
225
+
226
+    /**
227
+     * Returns the active record name.
228
+     *
229
+     * @return string the active record name.
230
+     */
231
+    abstract protected function getActiveRecordName();
232
+
233
+    /**
234
+     * Returns the active record data.
235
+     *
236
+     * @return array the active record data.
237
+     */
238
+    abstract protected function getActiveRecordData();
239
+
240
+    /**
241
+     * Set the active record data.
242
+     *
243
+     * @param array $fetch
244
+     * @return null
245
+     */
246
+    protected function setActiveRecordData(array $fetch)
247
+    {
248
+        $data = $this->getActiveRecordData();
249
+
250
+        foreach ($data as $key => &$value) {
251
+            if (!array_key_exists($key, $fetch)) {
252
+                throw new ActiveRecordException(sprintf('Can not read the expected column `%s`. It\'s not returnd by the `%s` table', $key, $this->getActiveRecordName()));
253
+            }
254
+
255
+            $value = $fetch[$key];
256
+        }
257
+    }
258 258
 }
Please login to merge, or discard this patch.