Completed
Push — master ( b05d4c...281aaa )
by Michael
03:36
created
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.