Completed
Push — develop ( bcdff2...aedfc7 )
by Michael
02:19
created
src/AbstractActiveRecord.php 1 patch
Indentation   +188 added lines, -188 removed lines patch added patch discarded remove patch
@@ -17,192 +17,192 @@
 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
-		$this->pdo = $pdo;
34
-		$this->id = null;
35
-	}
36
-
37
-	/**
38
-	 * {@inheritdoc}
39
-	 */
40
-	public function create()
41
-	{
42
-		$pdoStatement = $this->pdo->prepare($this->getCreateQuery());
43
-		$pdoStatement->execute($this->getActiveRecordData());
44
-
45
-		$this->id = intval($this->pdo->lastInsertId());
46
-		return $this;
47
-	}
48
-
49
-	/**
50
-	 * Returns the create query.
51
-	 *
52
-	 * @return string the create query.
53
-	 */
54
-	private function getCreateQuery()
55
-	{
56
-		$columns = array_keys($this->getActiveRecordData());
57
-		$values = [];
58
-
59
-		foreach ($columns as $key => $value) {
60
-			$values[] = ':' . $value;
61
-		}
62
-
63
-		return sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->getActiveRecordName(), implode(', ', $columns), implode(', ', $values));
64
-	}
65
-
66
-	/**
67
-	 * {@inheritdoc}
68
-	 */
69
-	public function read($id)
70
-	{
71
-		$data = $this->getActiveRecordData();
72
-
73
-		$pdoStatement = $this->pdo->prepare($this->getReadQuery());
74
-		$pdoStatement->execute(['id' => $id]);
75
-		$result = $pdoStatement->fetch(\PDO::FETCH_ASSOC);
76
-
77
-		$this->id = $id;
78
-
79
-		foreach ($data as $key => &$value) {
80
-			$value = $result[$key];
81
-		}
82
-
83
-		return $this;
84
-	}
85
-
86
-	/**
87
-	 * Returns the read query.
88
-	 *
89
-	 * @return string the read query.
90
-	 */
91
-	private function getReadQuery()
92
-	{
93
-		return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName());
94
-	}
95
-
96
-	/**
97
-	 * {@inheritdoc}
98
-	 */
99
-	public function update()
100
-	{
101
-		$pdoStatement = $this->pdo->prepare($this->getUpdateQuery());
102
-		$pdoStatement->execute(['id' => $this->id] + $this->getActiveRecordData());
103
-
104
-		return $this;
105
-	}
106
-
107
-	/**
108
-	 * Returns the update query.
109
-	 *
110
-	 * @return string the update query.
111
-	 */
112
-	private function getUpdateQuery()
113
-	{
114
-		$values = [];
115
-
116
-		foreach (array_keys($this->getActiveRecordData()) as $key => $value) {
117
-			$values[] = $value . ' = :' . $value;
118
-		}
119
-
120
-		return sprintf('UPDATE %s SET %s WHERE `id` = :id', $this->getActiveRecordName(), implode(', ', $values));
121
-	}
122
-
123
-	/**
124
-	 * {@inheritdoc}
125
-	 */
126
-	public function delete()
127
-	{
128
-		$pdoStatement = $this->pdo->prepare($this->getDeleteQuery());
129
-		$pdoStatement->execute(['id' => $this->id]);
130
-
131
-		$this->id = null;
132
-		return $this;
133
-	}
134
-
135
-	/**
136
-	 * Returns the delete query.
137
-	 *
138
-	 * @return string the delete query.
139
-	 */
140
-	private function getDeleteQuery()
141
-	{
142
-		return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName());
143
-	}
144
-
145
-	/**
146
-	 * {@inheritdoc}
147
-	 */
148
-	public function exists()
149
-	{
150
-		return $this->id !== null;
151
-	}
152
-
153
-	/**
154
-	 * Returns the PDO.
155
-	 *
156
-	 * @return \PDO the PDO.
157
-	 */
158
-	public function getPdo()
159
-	{
160
-		return $this->pdo;
161
-	}
162
-
163
-	/**
164
-	 * Set the PDO.
165
-	 *
166
-	 * @param \PDO $pdo
167
-	 * @return null
168
-	 */
169
-	protected function setPdo($pdo)
170
-	{
171
-		$this->pdo = $pdo;
172
-	}
173
-
174
-	/**
175
-	 * Returns the ID.
176
-	 *
177
-	 * @return null|int The ID.
178
-	 */
179
-	public function getId()
180
-	{
181
-		return $this->id;
182
-	}
183
-
184
-	/**
185
-	 * Set the ID.
186
-	 *
187
-	 * @param int $id
188
-	 * @return null
189
-	 */
190
-	protected function setId($id)
191
-	{
192
-		$this->id = $id;
193
-	}
194
-
195
-	/**
196
-	 * Returns the active record name.
197
-	 *
198
-	 * @return string the active record name.
199
-	 */
200
-	abstract protected function getActiveRecordName();
201
-
202
-	/**
203
-	 * Returns the active record data.
204
-	 *
205
-	 * @return array the active record data.
206
-	 */
207
-	abstract protected function getActiveRecordData();
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
+        $this->pdo = $pdo;
34
+        $this->id = null;
35
+    }
36
+
37
+    /**
38
+     * {@inheritdoc}
39
+     */
40
+    public function create()
41
+    {
42
+        $pdoStatement = $this->pdo->prepare($this->getCreateQuery());
43
+        $pdoStatement->execute($this->getActiveRecordData());
44
+
45
+        $this->id = intval($this->pdo->lastInsertId());
46
+        return $this;
47
+    }
48
+
49
+    /**
50
+     * Returns the create query.
51
+     *
52
+     * @return string the create query.
53
+     */
54
+    private function getCreateQuery()
55
+    {
56
+        $columns = array_keys($this->getActiveRecordData());
57
+        $values = [];
58
+
59
+        foreach ($columns as $key => $value) {
60
+            $values[] = ':' . $value;
61
+        }
62
+
63
+        return sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->getActiveRecordName(), implode(', ', $columns), implode(', ', $values));
64
+    }
65
+
66
+    /**
67
+     * {@inheritdoc}
68
+     */
69
+    public function read($id)
70
+    {
71
+        $data = $this->getActiveRecordData();
72
+
73
+        $pdoStatement = $this->pdo->prepare($this->getReadQuery());
74
+        $pdoStatement->execute(['id' => $id]);
75
+        $result = $pdoStatement->fetch(\PDO::FETCH_ASSOC);
76
+
77
+        $this->id = $id;
78
+
79
+        foreach ($data as $key => &$value) {
80
+            $value = $result[$key];
81
+        }
82
+
83
+        return $this;
84
+    }
85
+
86
+    /**
87
+     * Returns the read query.
88
+     *
89
+     * @return string the read query.
90
+     */
91
+    private function getReadQuery()
92
+    {
93
+        return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName());
94
+    }
95
+
96
+    /**
97
+     * {@inheritdoc}
98
+     */
99
+    public function update()
100
+    {
101
+        $pdoStatement = $this->pdo->prepare($this->getUpdateQuery());
102
+        $pdoStatement->execute(['id' => $this->id] + $this->getActiveRecordData());
103
+
104
+        return $this;
105
+    }
106
+
107
+    /**
108
+     * Returns the update query.
109
+     *
110
+     * @return string the update query.
111
+     */
112
+    private function getUpdateQuery()
113
+    {
114
+        $values = [];
115
+
116
+        foreach (array_keys($this->getActiveRecordData()) as $key => $value) {
117
+            $values[] = $value . ' = :' . $value;
118
+        }
119
+
120
+        return sprintf('UPDATE %s SET %s WHERE `id` = :id', $this->getActiveRecordName(), implode(', ', $values));
121
+    }
122
+
123
+    /**
124
+     * {@inheritdoc}
125
+     */
126
+    public function delete()
127
+    {
128
+        $pdoStatement = $this->pdo->prepare($this->getDeleteQuery());
129
+        $pdoStatement->execute(['id' => $this->id]);
130
+
131
+        $this->id = null;
132
+        return $this;
133
+    }
134
+
135
+    /**
136
+     * Returns the delete query.
137
+     *
138
+     * @return string the delete query.
139
+     */
140
+    private function getDeleteQuery()
141
+    {
142
+        return sprintf('SELECT * FROM `%s` WHERE `id` = :id', $this->getActiveRecordName());
143
+    }
144
+
145
+    /**
146
+     * {@inheritdoc}
147
+     */
148
+    public function exists()
149
+    {
150
+        return $this->id !== null;
151
+    }
152
+
153
+    /**
154
+     * Returns the PDO.
155
+     *
156
+     * @return \PDO the PDO.
157
+     */
158
+    public function getPdo()
159
+    {
160
+        return $this->pdo;
161
+    }
162
+
163
+    /**
164
+     * Set the PDO.
165
+     *
166
+     * @param \PDO $pdo
167
+     * @return null
168
+     */
169
+    protected function setPdo($pdo)
170
+    {
171
+        $this->pdo = $pdo;
172
+    }
173
+
174
+    /**
175
+     * Returns the ID.
176
+     *
177
+     * @return null|int The ID.
178
+     */
179
+    public function getId()
180
+    {
181
+        return $this->id;
182
+    }
183
+
184
+    /**
185
+     * Set the ID.
186
+     *
187
+     * @param int $id
188
+     * @return null
189
+     */
190
+    protected function setId($id)
191
+    {
192
+        $this->id = $id;
193
+    }
194
+
195
+    /**
196
+     * Returns the active record name.
197
+     *
198
+     * @return string the active record name.
199
+     */
200
+    abstract protected function getActiveRecordName();
201
+
202
+    /**
203
+     * Returns the active record data.
204
+     *
205
+     * @return array the active record data.
206
+     */
207
+    abstract protected function getActiveRecordData();
208 208
 }
Please login to merge, or discard this patch.
src/AbstractActiveRecordSearch.php 1 patch
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -17,62 +17,62 @@
 block discarded – undo
17 17
  */
18 18
 abstract class AbstractActiveRecordSearch extends AbstractActiveRecord
19 19
 {
20
-	/**
21
-	 * {@inheritdoc}
22
-	 */
23
-	public function search($options = [])
24
-	{
25
-		$pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($options));
26
-		array_walk_recursive($options, function (&$value) use ($pdoStatement) {
27
-			static $index = 1;
20
+    /**
21
+     * {@inheritdoc}
22
+     */
23
+    public function search($options = [])
24
+    {
25
+        $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($options));
26
+        array_walk_recursive($options, function (&$value) use ($pdoStatement) {
27
+            static $index = 1;
28 28
 
29
-			$pdoStatement->bindParam($index++, $value);
30
-		});
29
+            $pdoStatement->bindParam($index++, $value);
30
+        });
31 31
 
32
-		$pdoStatement->execute();
33
-		$result = [];
32
+        $pdoStatement->execute();
33
+        $result = [];
34 34
 
35
-		while ($row = $pdoStatement->fetch()) {
36
-			$x = new static($this->getPdo());
37
-			$x->setId(intval($row['id']));
35
+        while ($row = $pdoStatement->fetch()) {
36
+            $x = new static($this->getPdo());
37
+            $x->setId(intval($row['id']));
38 38
 
39
-			foreach ($x->getActiveRecordData() as $key => &$value) {
40
-				$value = $row[$key];
41
-			}
39
+            foreach ($x->getActiveRecordData() as $key => &$value) {
40
+                $value = $row[$key];
41
+            }
42 42
 
43
-			$result[] = $x;
44
-		}
43
+            $result[] = $x;
44
+        }
45 45
 
46
-		return $result;
47
-	}
46
+        return $result;
47
+    }
48 48
 
49
-	/**
50
-	 * Returns the search query with the given options.
51
-	 *
52
-	 * @param arrray $options = []
53
-	 * @return string the search query with the given options.
54
-	 */
55
-	private function getSearchQuery($options = [])
56
-	{
57
-		$columns = array_keys($this->getActiveRecordData());
58
-		$values = [];
49
+    /**
50
+     * Returns the search query with the given options.
51
+     *
52
+     * @param arrray $options = []
53
+     * @return string the search query with the given options.
54
+     */
55
+    private function getSearchQuery($options = [])
56
+    {
57
+        $columns = array_keys($this->getActiveRecordData());
58
+        $values = [];
59 59
 
60
-		foreach ($options as $key => $value) {
61
-			if (!in_array($key, $columns)) {
62
-				throw new ActiveRecordException('Invalid option key.');
63
-			}
60
+        foreach ($options as $key => $value) {
61
+            if (!in_array($key, $columns)) {
62
+                throw new ActiveRecordException('Invalid option key.');
63
+            }
64 64
 
65
-			if (is_int($value)) {
66
-				$values[] = $key . ' = ?';
67
-			} elseif (is_string($value)) {
68
-				$values[] = $key . ' LIKE ?';
69
-			} elseif(is_array($value) && !empty($value)) {
70
-				$values[] = $key . ' IN(' . implode(',', array_fill(0, count($value), '?')) . ')';
71
-			} else {
72
-				throw new ActiveRecordException('Invalid option value.');
73
-			}
74
-		}
65
+            if (is_int($value)) {
66
+                $values[] = $key . ' = ?';
67
+            } elseif (is_string($value)) {
68
+                $values[] = $key . ' LIKE ?';
69
+            } elseif(is_array($value) && !empty($value)) {
70
+                $values[] = $key . ' IN(' . implode(',', array_fill(0, count($value), '?')) . ')';
71
+            } else {
72
+                throw new ActiveRecordException('Invalid option value.');
73
+            }
74
+        }
75 75
 
76
-		return sprintf('SELECT * FROM %s %s %s', $this->getActiveRecordName(), empty($values) ? '' : 'WHERE', implode(' AND ', $values));
77
-	}
76
+        return sprintf('SELECT * FROM %s %s %s', $this->getActiveRecordName(), empty($values) ? '' : 'WHERE', implode(' AND ', $values));
77
+    }
78 78
 }
Please login to merge, or discard this patch.