Completed
Push — v2 ( a375df...98ab20 )
by Berend
04:42
created
src/Traits/Password.php 1 patch
Indentation   +180 added lines, -180 removed lines patch added patch discarded remove patch
@@ -14,187 +14,187 @@
 block discarded – undo
14 14
 
15 15
 trait Password
16 16
 {
17
-	/** @var string The password hash. */
18
-	protected $password;
19
-
20
-	/** @var string|null The password reset token. */
21
-	protected $passwordResetToken;
22
-
23
-	/** @var string|null The password expiry date */
24
-	protected $passwordExpiryDate;
25
-
26
-	/**
27
-	 * this method is required to be called in the constructor for each class that uses this trait. 
28
-	 * It adds the fields necessary for the passwords struct to the table definition
29
-	 */
30
-	protected function initPassword()
31
-	{
32
-		$this->extendTableDefinition(TRAIT_PASSWORD_FIELD_PASSWORD, [
33
-			'value' => &$this->password,
34
-			'validate' => [$this, 'validatePassword'],
35
-			'setter' => [$this, 'setPassword'],
36
-			'type' => 'VARCHAR',
37
-			'length' => 1024,
38
-			'properties' => null
39
-		]);
40
-
41
-		$this->extendTableDefinition(TRAIT_PASSWORD_FIELD_RESET_TOKEN, [
42
-			'value' => &$this->passwordResetToken,
43
-			'validate' => null,
44
-			'default' => 0,
45
-			'type' => 'VARCHAR',
46
-			'length' => 1024
47
-		]);
48
-
49
-		$this->extendTableDefinition(TRAIT_PASSWORD_FIELD_RESET_TOKEN_EXPIRY, [
50
-			'value' => &$this->passwordExpiryDate,
51
-			'validate' => null,
52
-			'type' => 'DATETIME',
53
-		]);
54
-	}
55
-
56
-
57
-	/**
58
-	 * Returns whether the users password has been set
59
-	 * @return boolean true if the user has a password
60
-	 */
61
-	public function hasPasswordBeenSet()
62
-	{
63
-		return $this->password !== null;
64
-	}
65
-
66
-	/**
67
-	 * Returns true if the credentials are correct.
68
-	 *
69
-	 * @param string $password
70
-	 * @return boolean true if the credentials are correct
71
-	 */
72
-	public function isPassword($password)
73
-	{ 
74
-		if (!$this->hasPasswordBeenSet())
75
-		{
76
-			throw new ActiveRecordTraitException("Password field has not been set");
77
-		}
78
-
79
-		if (!password_verify($password, $this->password)) {
80
-			return false;
81
-		}
82
-
83
-		if (password_needs_rehash($this->password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH])) {
84
-			$this->setPassword($password)->sync();
85
-		}
86
-
87
-		return true;
88
-	}
89
-
90
-	public function validatePassword($password) {
91
-		if (strlen($password) < TRAIT_PASSWORD_MIN_LENGTH) {
92
-			$message = sprintf('\'Password\' must be atleast %s characters long. %s characters provided.', TRAIT_PASSWORD_MIN_LENGTH, strlen($password));
93
-			return [false, $message];
94
-		}
95
-		return [true, ''];
96
-	}
97
-
98
-	/**
99
-	 * Set the password.
100
-	 *
101
-	 * @param string $password
102
-	 * @return $this
103
-	 * @throws \Exception
104
-	 */
105
-	public function setPassword($password)
106
-	{
107
-		[$status, $error] = $this->validatePassword($password);
108
-		if (!$status) {
109
-			throw new ActiveRecordTraitException($error);
110
-		}
111
-
112
-		$passwordHash = \password_hash($password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH]);
113
-
114
-		if ($passwordHash === false) {
115
-			throw new ActiveRecordTraitException('\'Password\' hash failed.');
116
-		}
117
-
118
-		$this->password = $passwordHash;
119
-
120
-		return $this;
121
-	}
122
-
123
-	/**
124
-	 * @return string The Hash of the password
125
-	 */
126
-	public function getPasswordHash()
127
-	{
128
-		return $this->password;
129
-	}
130
-
131
-	/**
132
-	 * Returns the currently set password token for the entity, or null if not set
133
-	 * @return string|null The password reset token
134
-	 */
135
-	public function getPasswordResetToken()
136
-	{
137
-		return $this->passwordResetToken;
138
-	}
139
-
140
-	/**
141
-	 * Generates a new password reset token for the user
142
-	 */
143
-	public function generatePasswordResetToken()
144
-	{
145
-		$this->passwordResetToken = md5(uniqid(mt_rand(), true));
146
-
147
-		$validityDuration = new \DateInterval('PT24H');
148
-
149
-		$this->passwordExpiryDate = (new \DateTime('now'))->add($validityDuration)->format('Y-m-d H:i:s');
150
-		return $this;
151
-	}
152
-
153
-	/**
154
-	 * Clears the current password reset token
155
-	 */
156
-	public function clearPasswordResetToken()
157
-	{
158
-		$this->passwordResetToken = null;
159
-		$this->passwordExpiryDate = null;
160
-		return $this;
161
-	}
162
-
163
-	public function validatePasswordResetToken(string $token)
164
-	{
165
-		return $this->passwordResetToken !== null
166
-			&& $token === $this->passwordResetToken
167
-			&& (new \DateTime('now')) < (new \DateTime($this->passwordExpiryDate));
168
-	}
17
+    /** @var string The password hash. */
18
+    protected $password;
19
+
20
+    /** @var string|null The password reset token. */
21
+    protected $passwordResetToken;
22
+
23
+    /** @var string|null The password expiry date */
24
+    protected $passwordExpiryDate;
25
+
26
+    /**
27
+     * this method is required to be called in the constructor for each class that uses this trait. 
28
+     * It adds the fields necessary for the passwords struct to the table definition
29
+     */
30
+    protected function initPassword()
31
+    {
32
+        $this->extendTableDefinition(TRAIT_PASSWORD_FIELD_PASSWORD, [
33
+            'value' => &$this->password,
34
+            'validate' => [$this, 'validatePassword'],
35
+            'setter' => [$this, 'setPassword'],
36
+            'type' => 'VARCHAR',
37
+            'length' => 1024,
38
+            'properties' => null
39
+        ]);
40
+
41
+        $this->extendTableDefinition(TRAIT_PASSWORD_FIELD_RESET_TOKEN, [
42
+            'value' => &$this->passwordResetToken,
43
+            'validate' => null,
44
+            'default' => 0,
45
+            'type' => 'VARCHAR',
46
+            'length' => 1024
47
+        ]);
48
+
49
+        $this->extendTableDefinition(TRAIT_PASSWORD_FIELD_RESET_TOKEN_EXPIRY, [
50
+            'value' => &$this->passwordExpiryDate,
51
+            'validate' => null,
52
+            'type' => 'DATETIME',
53
+        ]);
54
+    }
55
+
56
+
57
+    /**
58
+     * Returns whether the users password has been set
59
+     * @return boolean true if the user has a password
60
+     */
61
+    public function hasPasswordBeenSet()
62
+    {
63
+        return $this->password !== null;
64
+    }
65
+
66
+    /**
67
+     * Returns true if the credentials are correct.
68
+     *
69
+     * @param string $password
70
+     * @return boolean true if the credentials are correct
71
+     */
72
+    public function isPassword($password)
73
+    { 
74
+        if (!$this->hasPasswordBeenSet())
75
+        {
76
+            throw new ActiveRecordTraitException("Password field has not been set");
77
+        }
78
+
79
+        if (!password_verify($password, $this->password)) {
80
+            return false;
81
+        }
82
+
83
+        if (password_needs_rehash($this->password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH])) {
84
+            $this->setPassword($password)->sync();
85
+        }
86
+
87
+        return true;
88
+    }
89
+
90
+    public function validatePassword($password) {
91
+        if (strlen($password) < TRAIT_PASSWORD_MIN_LENGTH) {
92
+            $message = sprintf('\'Password\' must be atleast %s characters long. %s characters provided.', TRAIT_PASSWORD_MIN_LENGTH, strlen($password));
93
+            return [false, $message];
94
+        }
95
+        return [true, ''];
96
+    }
97
+
98
+    /**
99
+     * Set the password.
100
+     *
101
+     * @param string $password
102
+     * @return $this
103
+     * @throws \Exception
104
+     */
105
+    public function setPassword($password)
106
+    {
107
+        [$status, $error] = $this->validatePassword($password);
108
+        if (!$status) {
109
+            throw new ActiveRecordTraitException($error);
110
+        }
111
+
112
+        $passwordHash = \password_hash($password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH]);
113
+
114
+        if ($passwordHash === false) {
115
+            throw new ActiveRecordTraitException('\'Password\' hash failed.');
116
+        }
117
+
118
+        $this->password = $passwordHash;
119
+
120
+        return $this;
121
+    }
122
+
123
+    /**
124
+     * @return string The Hash of the password
125
+     */
126
+    public function getPasswordHash()
127
+    {
128
+        return $this->password;
129
+    }
130
+
131
+    /**
132
+     * Returns the currently set password token for the entity, or null if not set
133
+     * @return string|null The password reset token
134
+     */
135
+    public function getPasswordResetToken()
136
+    {
137
+        return $this->passwordResetToken;
138
+    }
139
+
140
+    /**
141
+     * Generates a new password reset token for the user
142
+     */
143
+    public function generatePasswordResetToken()
144
+    {
145
+        $this->passwordResetToken = md5(uniqid(mt_rand(), true));
146
+
147
+        $validityDuration = new \DateInterval('PT24H');
148
+
149
+        $this->passwordExpiryDate = (new \DateTime('now'))->add($validityDuration)->format('Y-m-d H:i:s');
150
+        return $this;
151
+    }
152
+
153
+    /**
154
+     * Clears the current password reset token
155
+     */
156
+    public function clearPasswordResetToken()
157
+    {
158
+        $this->passwordResetToken = null;
159
+        $this->passwordExpiryDate = null;
160
+        return $this;
161
+    }
162
+
163
+    public function validatePasswordResetToken(string $token)
164
+    {
165
+        return $this->passwordResetToken !== null
166
+            && $token === $this->passwordResetToken
167
+            && (new \DateTime('now')) < (new \DateTime($this->passwordExpiryDate));
168
+    }
169 169
 	
170
-	/**
171
-	 * @return void
172
-	 */
173
-	abstract protected function extendTableDefinition($columnName, $definition);
170
+    /**
171
+     * @return void
172
+     */
173
+    abstract protected function extendTableDefinition($columnName, $definition);
174 174
 	
175
-	/**
176
-	 * @return void
177
-	 */
178
-	abstract protected function registerSearchHook($columnName, $fn);
179
-
180
-	/**
181
-	 * @return void
182
-	 */
183
-	abstract protected function registerDeleteHook($columnName, $fn);
184
-
185
-	/**
186
-	 * @return void
187
-	 */
188
-	abstract protected function registerUpdateHook($columnName, $fn);
189
-
190
-	/**
191
-	 * @return void
192
-	 */
193
-	abstract protected function registerReadHook($columnName, $fn);
194
-
195
-	/**
196
-	 * @return void
197
-	 */
198
-	abstract protected function registerCreateHook($columnName, $fn);
175
+    /**
176
+     * @return void
177
+     */
178
+    abstract protected function registerSearchHook($columnName, $fn);
179
+
180
+    /**
181
+     * @return void
182
+     */
183
+    abstract protected function registerDeleteHook($columnName, $fn);
184
+
185
+    /**
186
+     * @return void
187
+     */
188
+    abstract protected function registerUpdateHook($columnName, $fn);
189
+
190
+    /**
191
+     * @return void
192
+     */
193
+    abstract protected function registerReadHook($columnName, $fn);
194
+
195
+    /**
196
+     * @return void
197
+     */
198
+    abstract protected function registerCreateHook($columnName, $fn);
199 199
 
200 200
 }
201 201
\ No newline at end of file
Please login to merge, or discard this patch.
src/AbstractActiveRecord.php 1 patch
Indentation   +508 added lines, -508 removed lines patch added patch discarded remove patch
@@ -18,513 +18,513 @@
 block discarded – undo
18 18
  */
19 19
 abstract class AbstractActiveRecord implements ActiveRecordInterface
20 20
 {
21
-	const COLUMN_NAME_ID = 'id';
22
-	const COLUMN_TYPE_ID = 'INT UNSIGNED';
23
-
24
-	const CREATE = 'CREATE';
25
-	const READ = 'READ';
26
-	const UPDATE = 'UPDATE';
27
-	const DELETE = 'DELETE';
28
-	const SEARCH = 'SEARCH';
29
-
30
-	/** @var \PDO The PDO object. */
31
-	protected $pdo;
32
-
33
-	/** @var null|int The ID. */
34
-	private $id;
35
-
36
-	/** @var array A map of column name to functions that hook the insert function */
37
-	protected $createHooks;
38
-
39
-	/** @var array A map of column name to functions that hook the read function */
40
-	protected $readHooks;
41
-
42
-	/** @var array A map of column name to functions that hook the update function */
43
-	protected $updateHooks;
44
-
45
-	/** @var array A map of column name to functions that hook the update function */
46
-	protected $deleteHooks;	
47
-
48
-	/** @var array A map of column name to functions that hook the search function */
49
-	protected $searchHooks;
50
-
51
-	/** @var array A list of table column definitions */
52
-	protected $tableDefinition;
53
-
54
-	/**
55
-	 * Construct an abstract active record with the given PDO.
56
-	 *
57
-	 * @param \PDO $pdo
58
-	 */
59
-	public function __construct(\PDO $pdo)
60
-	{
61
-		$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
62
-		$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
63
-
64
-		$this->setPdo($pdo);
65
-
66
-		$this->createHooks = [];
67
-		$this->readHooks = [];
68
-		$this->updateHooks = [];
69
-		$this->deleteHooks = [];
70
-		$this->searchHooks = [];
71
-		$this->tableDefinition = $this->getTableDefinition();
72
-
73
-		// Extend table definition with default ID field, throw exception if field already exists
74
-		if (array_key_exists('id', $this->tableDefinition)) {
75
-			$message = "Table definition in record contains a field with name \"id\"";
76
-			$message .= ", which is a reserved name by ActiveRecord";
77
-			throw new ActiveRecordException($message, 0);
78
-		}
79
-
80
-		$this->tableDefinition[self::COLUMN_NAME_ID] =
81
-		[
82
-			'value' => &$this->id,
83
-			'validate' => null,
84
-			'type' => self::COLUMN_TYPE_ID,
85
-			'properties' =>
86
-				ColumnProperty::NOT_NULL
87
-				| ColumnProperty::IMMUTABLE
88
-				| ColumnProperty::AUTO_INCREMENT
89
-				| ColumnProperty::PRIMARY_KEY
90
-		];
91
-	}
92
-
93
-	private function checkHookConstraints($columnName, $hookMap)
94
-	{
95
-		// Check whether column exists
96
-		if (!array_key_exists($columnName, $this->tableDefinition)) 
97
-		{
98
-			throw new ActiveRecordException("Hook is trying to register on non-existing column \"$columnName\"", 0);
99
-		}
100
-
101
-		// Enforcing 1 hook per table column
102
-		if (array_key_exists($columnName, $hookMap)) {
103
-			$message = "Hook is trying to register on an already registered column \"$columnName\", ";
104
-			$message .= "do you have conflicting traits?";
105
-			throw new ActiveRecordException($message, 0);
106
-		}
107
-	}
108
-
109
-	public function registerHookOnAction($actionName, $columnName, $fn)
110
-	{
111
-		if (is_string($fn) && is_callable([$this, $fn])) {
112
-			$fn = [$this, $fn];
113
-		}
114
-
115
-		if (!is_callable($fn)) { 
116
-			throw new ActiveRecordException("Provided hook on column \"$columnName\" is not callable", 0);
117
-		}
118
-
119
-		switch ($actionName) {
120
-			case self::CREATE:
121
-				$this->checkHookConstraints($columnName, $this->createHooks);
122
-				$this->createHooks[$columnName] = $fn;
123
-				break;
124
-			case self::READ:
125
-				$this->checkHookConstraints($columnName, $this->readHooks);
126
-				$this->readHooks[$columnName] = $fn;
127
-				break;
128
-			case self::UPDATE:
129
-				$this->checkHookConstraints($columnName, $this->updateHooks);
130
-				$this->updateHooks[$columnName] = $fn;
131
-				break;
132
-			case self::DELETE:
133
-				$this->checkHookConstraints($columnName, $this->deleteHooks);
134
-				$this->deleteHooks[$columnName] = $fn;
135
-				break;
136
-			case self::SEARCH:
137
-				$this->checkHookConstraints($columnName, $this->searchHooks);
138
-				$this->searchHooks[$columnName] = $fn;
139
-				break;
140
-			default:
141
-				throw new ActiveRecordException("Invalid action: Can not register hook on non-existing action");
142
-		}
143
-	}
144
-
145
-	/**
146
-	 * Register a new hook for a specific column that gets called before execution of the create() method
147
-	 * Only one hook per column can be registered at a time
148
-	 * @param string $columnName The name of the column that is registered.
149
-	 * @param string|callable $fn Either a callable, or the name of a method on the inheriting object.
150
-	 */
151
-	public function registerCreateHook($columnName, $fn)
152
-	{
153
-		$this->registerHookOnAction(self::CREATE, $columnName, $fn);
154
-	}
155
-
156
-	/**
157
-	 * Register a new hook for a specific column that gets called before execution of the read() method
158
-	 * Only one hook per column can be registered at a time
159
-	 * @param string $columnName The name of the column that is registered.
160
-	 * @param string|callable $fn Either a callable, or the name of a method on the inheriting object.
161
-	 */
162
-	public function registerReadHook($columnName, $fn)
163
-	{
164
-		$this->registerHookOnAction(self::READ, $columnName, $fn);
165
-	}
166
-
167
-	/**
168
-	 * Register a new hook for a specific column that gets called before execution of the update() method
169
-	 * Only one hook per column can be registered at a time
170
-	 * @param string $columnName The name of the column that is registered.
171
-	 * @param string|callable $fn Either a callable, or the name of a method on the inheriting object.
172
-	 */
173
-	public function registerUpdateHook($columnName, $fn)
174
-	{
175
-		$this->registerHookOnAction(self::UPDATE, $columnName, $fn);
176
-	}
177
-
178
-	/**
179
-	 * Register a new hook for a specific column that gets called before execution of the delete() method
180
-	 * Only one hook per column can be registered at a time
181
-	 * @param string $columnName The name of the column that is registered.
182
-	 * @param string|callable $fn Either a callable, or the name of a method on the inheriting object.
183
-	 */
184
-	public function registerDeleteHook($columnName, $fn)
185
-	{
186
-		$this->registerHookOnAction(self::DELETE, $columnName, $fn);
187
-	}
188
-
189
-	/**
190
-	 * Register a new hook for a specific column that gets called before execution of the search() method
191
-	 * Only one hook per column can be registered at a time
192
-	 * @param string $columnName The name of the column that is registered.
193
-	 * @param string|callable $fn Either a callable, or the name of a method on the inheriting object. The callable is required to take one argument: an instance of miBadger\Query\Query; 
194
-	 */
195
-	public function registerSearchHook($columnName, $fn)
196
-	{
197
-		$this->registerHookOnAction(self::SEARCH, $columnName, $fn);
198
-	}
199
-
200
-	/**
201
-	 * Adds a new column definition to the table.
202
-	 * @param string $columnName The name of the column that is registered.
203
-	 * @param Array $definition The definition of that column.
204
-	 */
205
-	protected function extendTableDefinition($columnName, $definition)
206
-	{
207
-		if ($this->tableDefinition === null) {
208
-			throw new ActiveRecordException("tableDefinition is null, has parent been initialized in constructor?");
209
-		}
210
-
211
-		// Enforcing table can only be extended with new columns
212
-		if (array_key_exists($columnName, $this->tableDefinition)) {
213
-			$message = "Table is being extended with a column that already exists, ";
214
-			$message .= "\"$columnName\" conflicts with your table definition";
215
-			throw new ActiveRecordException($message, 0);
216
-		}
217
-
218
-		$this->tableDefinition[$columnName] = $definition;
219
-	}
220
-
221
-	/**
222
-	 * Creates the entity as a table in the database
223
-	 */
224
-	public function createTable()
225
-	{
226
-		$this->pdo->query(SchemaBuilder::buildCreateTableSQL($this->getTableName(), $this->tableDefinition));
227
-	}
228
-
229
-	/**
230
-	 * Iterates over the specified constraints in the table definition, 
231
-	 * 		and applies these to the database.
232
-	 */
233
-	public function createTableConstraints()
234
-	{
235
-		// Iterate over columns, check whether "relation" field exists, if so create constraint
236
-		foreach ($this->tableDefinition as $colName => $definition) {
237
-			if (isset($definition['relation']) && $definition['relation'] instanceof AbstractActiveRecord) {
238
-				// Forge new relation
239
-				$target = $definition['relation'];
240
-				$properties = $definition['properties'] ?? 0;
241
-
242
-				if ($properties & ColumnProperty::NOT_NULL) {
243
-					$constraintSql = SchemaBuilder::buildConstraintOnDeleteCascade($target->getTableName(), 'id', $this->getTableName(), $colName);
244
-				} else {
245
-					$constraintSql = SchemaBuilder::buildConstraintOnDeleteSetNull($target->getTableName(), 'id', $this->getTableName(), $colName);
246
-				}
247
-
248
-				$this->pdo->query($constraintSql);
249
-			} else if (isset($definition['relation'])) {
250
-				$msg = sprintf("Relation constraint on column \"%s\" of table \"%s\" does not contain a valid ActiveRecord instance", 
251
-					$colName,
252
-					$this->getTableName());
253
-				throw new ActiveRecordException($msg);
254
-			}
255
-		}
256
-	}
257
-
258
-	/**
259
-	 * Returns the name -> variable mapping for the table definition.
260
-	 * @return Array The mapping
261
-	 */
262
-	protected function getActiveRecordColumns()
263
-	{
264
-		$bindings = [];
265
-		foreach ($this->tableDefinition as $colName => $definition) {
266
-
267
-			// Ignore the id column (key) when inserting or updating
268
-			if ($colName == self::COLUMN_NAME_ID) {
269
-				continue;
270
-			}
271
-
272
-			$bindings[$colName] = &$definition['value'];
273
-		}
274
-		return $bindings;
275
-	}
276
-
277
-	protected function insertDefaults()
278
-	{
279
-		// Insert default values for not-null fields
280
-		foreach ($this->tableDefinition as $colName => $colDef) {
281
-			if ($colDef['value'] === null
282
-				&& ($colDef['properties'] ?? 0) & ColumnProperty::NOT_NULL
283
-				&& isset($colDef['default'])) {
284
-				$this->tableDefinition[$colName]['value'] = $colDef['default'];
285
-			}
286
-		}		
287
-	}
288
-
289
-	/**
290
-	 * {@inheritdoc}
291
-	 */
292
-	public function create()
293
-	{
294
-		foreach ($this->createHooks as $colName => $fn) {
295
-			$fn();
296
-		}
297
-
298
-		$this->insertDefaults();
299
-
300
-		try {
301
-			(new Query($this->getPdo(), $this->getTableName()))
302
-				->insert($this->getActiveRecordColumns())
303
-				->execute();
304
-
305
-			$this->setId(intval($this->getPdo()->lastInsertId()));
306
-		} catch (\PDOException $e) {
307
-			throw new ActiveRecordException($e->getMessage(), ActiveRecordException::DB_ERROR, $e);
308
-		}
309
-
310
-		return $this;
311
-	}
312
-
313
-	/**
314
-	 * {@inheritdoc}
315
-	 */
316
-	public function read($id)
317
-	{
318
-		$whereConditions = [
319
-			Query::Equal('id', $id)
320
-		];
321
-		foreach ($this->readHooks as $colName => $fn) {
322
-			$cond = $fn();
323
-			if ($cond !== null) {
324
-				$whereConditions[] = $cond;
325
-			}
326
-		}
327
-
328
-		try {
329
-			$row = (new Query($this->getPdo(), $this->getTableName()))
330
-				->select()
331
-				->where(Query::AndArray($whereConditions))
332
-				->execute()
333
-				->fetch();
21
+    const COLUMN_NAME_ID = 'id';
22
+    const COLUMN_TYPE_ID = 'INT UNSIGNED';
23
+
24
+    const CREATE = 'CREATE';
25
+    const READ = 'READ';
26
+    const UPDATE = 'UPDATE';
27
+    const DELETE = 'DELETE';
28
+    const SEARCH = 'SEARCH';
29
+
30
+    /** @var \PDO The PDO object. */
31
+    protected $pdo;
32
+
33
+    /** @var null|int The ID. */
34
+    private $id;
35
+
36
+    /** @var array A map of column name to functions that hook the insert function */
37
+    protected $createHooks;
38
+
39
+    /** @var array A map of column name to functions that hook the read function */
40
+    protected $readHooks;
41
+
42
+    /** @var array A map of column name to functions that hook the update function */
43
+    protected $updateHooks;
44
+
45
+    /** @var array A map of column name to functions that hook the update function */
46
+    protected $deleteHooks;	
47
+
48
+    /** @var array A map of column name to functions that hook the search function */
49
+    protected $searchHooks;
50
+
51
+    /** @var array A list of table column definitions */
52
+    protected $tableDefinition;
53
+
54
+    /**
55
+     * Construct an abstract active record with the given PDO.
56
+     *
57
+     * @param \PDO $pdo
58
+     */
59
+    public function __construct(\PDO $pdo)
60
+    {
61
+        $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
62
+        $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
63
+
64
+        $this->setPdo($pdo);
65
+
66
+        $this->createHooks = [];
67
+        $this->readHooks = [];
68
+        $this->updateHooks = [];
69
+        $this->deleteHooks = [];
70
+        $this->searchHooks = [];
71
+        $this->tableDefinition = $this->getTableDefinition();
72
+
73
+        // Extend table definition with default ID field, throw exception if field already exists
74
+        if (array_key_exists('id', $this->tableDefinition)) {
75
+            $message = "Table definition in record contains a field with name \"id\"";
76
+            $message .= ", which is a reserved name by ActiveRecord";
77
+            throw new ActiveRecordException($message, 0);
78
+        }
79
+
80
+        $this->tableDefinition[self::COLUMN_NAME_ID] =
81
+        [
82
+            'value' => &$this->id,
83
+            'validate' => null,
84
+            'type' => self::COLUMN_TYPE_ID,
85
+            'properties' =>
86
+                ColumnProperty::NOT_NULL
87
+                | ColumnProperty::IMMUTABLE
88
+                | ColumnProperty::AUTO_INCREMENT
89
+                | ColumnProperty::PRIMARY_KEY
90
+        ];
91
+    }
92
+
93
+    private function checkHookConstraints($columnName, $hookMap)
94
+    {
95
+        // Check whether column exists
96
+        if (!array_key_exists($columnName, $this->tableDefinition)) 
97
+        {
98
+            throw new ActiveRecordException("Hook is trying to register on non-existing column \"$columnName\"", 0);
99
+        }
100
+
101
+        // Enforcing 1 hook per table column
102
+        if (array_key_exists($columnName, $hookMap)) {
103
+            $message = "Hook is trying to register on an already registered column \"$columnName\", ";
104
+            $message .= "do you have conflicting traits?";
105
+            throw new ActiveRecordException($message, 0);
106
+        }
107
+    }
108
+
109
+    public function registerHookOnAction($actionName, $columnName, $fn)
110
+    {
111
+        if (is_string($fn) && is_callable([$this, $fn])) {
112
+            $fn = [$this, $fn];
113
+        }
114
+
115
+        if (!is_callable($fn)) { 
116
+            throw new ActiveRecordException("Provided hook on column \"$columnName\" is not callable", 0);
117
+        }
118
+
119
+        switch ($actionName) {
120
+            case self::CREATE:
121
+                $this->checkHookConstraints($columnName, $this->createHooks);
122
+                $this->createHooks[$columnName] = $fn;
123
+                break;
124
+            case self::READ:
125
+                $this->checkHookConstraints($columnName, $this->readHooks);
126
+                $this->readHooks[$columnName] = $fn;
127
+                break;
128
+            case self::UPDATE:
129
+                $this->checkHookConstraints($columnName, $this->updateHooks);
130
+                $this->updateHooks[$columnName] = $fn;
131
+                break;
132
+            case self::DELETE:
133
+                $this->checkHookConstraints($columnName, $this->deleteHooks);
134
+                $this->deleteHooks[$columnName] = $fn;
135
+                break;
136
+            case self::SEARCH:
137
+                $this->checkHookConstraints($columnName, $this->searchHooks);
138
+                $this->searchHooks[$columnName] = $fn;
139
+                break;
140
+            default:
141
+                throw new ActiveRecordException("Invalid action: Can not register hook on non-existing action");
142
+        }
143
+    }
144
+
145
+    /**
146
+     * Register a new hook for a specific column that gets called before execution of the create() method
147
+     * Only one hook per column can be registered at a time
148
+     * @param string $columnName The name of the column that is registered.
149
+     * @param string|callable $fn Either a callable, or the name of a method on the inheriting object.
150
+     */
151
+    public function registerCreateHook($columnName, $fn)
152
+    {
153
+        $this->registerHookOnAction(self::CREATE, $columnName, $fn);
154
+    }
155
+
156
+    /**
157
+     * Register a new hook for a specific column that gets called before execution of the read() method
158
+     * Only one hook per column can be registered at a time
159
+     * @param string $columnName The name of the column that is registered.
160
+     * @param string|callable $fn Either a callable, or the name of a method on the inheriting object.
161
+     */
162
+    public function registerReadHook($columnName, $fn)
163
+    {
164
+        $this->registerHookOnAction(self::READ, $columnName, $fn);
165
+    }
166
+
167
+    /**
168
+     * Register a new hook for a specific column that gets called before execution of the update() method
169
+     * Only one hook per column can be registered at a time
170
+     * @param string $columnName The name of the column that is registered.
171
+     * @param string|callable $fn Either a callable, or the name of a method on the inheriting object.
172
+     */
173
+    public function registerUpdateHook($columnName, $fn)
174
+    {
175
+        $this->registerHookOnAction(self::UPDATE, $columnName, $fn);
176
+    }
177
+
178
+    /**
179
+     * Register a new hook for a specific column that gets called before execution of the delete() method
180
+     * Only one hook per column can be registered at a time
181
+     * @param string $columnName The name of the column that is registered.
182
+     * @param string|callable $fn Either a callable, or the name of a method on the inheriting object.
183
+     */
184
+    public function registerDeleteHook($columnName, $fn)
185
+    {
186
+        $this->registerHookOnAction(self::DELETE, $columnName, $fn);
187
+    }
188
+
189
+    /**
190
+     * Register a new hook for a specific column that gets called before execution of the search() method
191
+     * Only one hook per column can be registered at a time
192
+     * @param string $columnName The name of the column that is registered.
193
+     * @param string|callable $fn Either a callable, or the name of a method on the inheriting object. The callable is required to take one argument: an instance of miBadger\Query\Query; 
194
+     */
195
+    public function registerSearchHook($columnName, $fn)
196
+    {
197
+        $this->registerHookOnAction(self::SEARCH, $columnName, $fn);
198
+    }
199
+
200
+    /**
201
+     * Adds a new column definition to the table.
202
+     * @param string $columnName The name of the column that is registered.
203
+     * @param Array $definition The definition of that column.
204
+     */
205
+    protected function extendTableDefinition($columnName, $definition)
206
+    {
207
+        if ($this->tableDefinition === null) {
208
+            throw new ActiveRecordException("tableDefinition is null, has parent been initialized in constructor?");
209
+        }
210
+
211
+        // Enforcing table can only be extended with new columns
212
+        if (array_key_exists($columnName, $this->tableDefinition)) {
213
+            $message = "Table is being extended with a column that already exists, ";
214
+            $message .= "\"$columnName\" conflicts with your table definition";
215
+            throw new ActiveRecordException($message, 0);
216
+        }
217
+
218
+        $this->tableDefinition[$columnName] = $definition;
219
+    }
220
+
221
+    /**
222
+     * Creates the entity as a table in the database
223
+     */
224
+    public function createTable()
225
+    {
226
+        $this->pdo->query(SchemaBuilder::buildCreateTableSQL($this->getTableName(), $this->tableDefinition));
227
+    }
228
+
229
+    /**
230
+     * Iterates over the specified constraints in the table definition, 
231
+     * 		and applies these to the database.
232
+     */
233
+    public function createTableConstraints()
234
+    {
235
+        // Iterate over columns, check whether "relation" field exists, if so create constraint
236
+        foreach ($this->tableDefinition as $colName => $definition) {
237
+            if (isset($definition['relation']) && $definition['relation'] instanceof AbstractActiveRecord) {
238
+                // Forge new relation
239
+                $target = $definition['relation'];
240
+                $properties = $definition['properties'] ?? 0;
241
+
242
+                if ($properties & ColumnProperty::NOT_NULL) {
243
+                    $constraintSql = SchemaBuilder::buildConstraintOnDeleteCascade($target->getTableName(), 'id', $this->getTableName(), $colName);
244
+                } else {
245
+                    $constraintSql = SchemaBuilder::buildConstraintOnDeleteSetNull($target->getTableName(), 'id', $this->getTableName(), $colName);
246
+                }
247
+
248
+                $this->pdo->query($constraintSql);
249
+            } else if (isset($definition['relation'])) {
250
+                $msg = sprintf("Relation constraint on column \"%s\" of table \"%s\" does not contain a valid ActiveRecord instance", 
251
+                    $colName,
252
+                    $this->getTableName());
253
+                throw new ActiveRecordException($msg);
254
+            }
255
+        }
256
+    }
257
+
258
+    /**
259
+     * Returns the name -> variable mapping for the table definition.
260
+     * @return Array The mapping
261
+     */
262
+    protected function getActiveRecordColumns()
263
+    {
264
+        $bindings = [];
265
+        foreach ($this->tableDefinition as $colName => $definition) {
266
+
267
+            // Ignore the id column (key) when inserting or updating
268
+            if ($colName == self::COLUMN_NAME_ID) {
269
+                continue;
270
+            }
271
+
272
+            $bindings[$colName] = &$definition['value'];
273
+        }
274
+        return $bindings;
275
+    }
276
+
277
+    protected function insertDefaults()
278
+    {
279
+        // Insert default values for not-null fields
280
+        foreach ($this->tableDefinition as $colName => $colDef) {
281
+            if ($colDef['value'] === null
282
+                && ($colDef['properties'] ?? 0) & ColumnProperty::NOT_NULL
283
+                && isset($colDef['default'])) {
284
+                $this->tableDefinition[$colName]['value'] = $colDef['default'];
285
+            }
286
+        }		
287
+    }
288
+
289
+    /**
290
+     * {@inheritdoc}
291
+     */
292
+    public function create()
293
+    {
294
+        foreach ($this->createHooks as $colName => $fn) {
295
+            $fn();
296
+        }
297
+
298
+        $this->insertDefaults();
299
+
300
+        try {
301
+            (new Query($this->getPdo(), $this->getTableName()))
302
+                ->insert($this->getActiveRecordColumns())
303
+                ->execute();
304
+
305
+            $this->setId(intval($this->getPdo()->lastInsertId()));
306
+        } catch (\PDOException $e) {
307
+            throw new ActiveRecordException($e->getMessage(), ActiveRecordException::DB_ERROR, $e);
308
+        }
309
+
310
+        return $this;
311
+    }
312
+
313
+    /**
314
+     * {@inheritdoc}
315
+     */
316
+    public function read($id)
317
+    {
318
+        $whereConditions = [
319
+            Query::Equal('id', $id)
320
+        ];
321
+        foreach ($this->readHooks as $colName => $fn) {
322
+            $cond = $fn();
323
+            if ($cond !== null) {
324
+                $whereConditions[] = $cond;
325
+            }
326
+        }
327
+
328
+        try {
329
+            $row = (new Query($this->getPdo(), $this->getTableName()))
330
+                ->select()
331
+                ->where(Query::AndArray($whereConditions))
332
+                ->execute()
333
+                ->fetch();
334 334
 			
335
-			if ($row === false) {
336
-				$msg = sprintf('Can not read the non-existent active record entry %d from the `%s` table.', $id, $this->getTableName());
337
-				throw new ActiveRecordException($msg, ActiveRecordException::NOT_FOUND);
338
-			}
339
-
340
-			$this->fill($row)->setId($id);
341
-		} catch (\PDOException $e) {
342
-			throw new ActiveRecordException($e->getMessage(), ActiveRecordException::DB_ERROR, $e);
343
-		}
344
-
345
-		return $this;
346
-	}
347
-
348
-	/**
349
-	 * {@inheritdoc}
350
-	 */
351
-	public function update()
352
-	{
353
-		foreach ($this->updateHooks as $colName => $fn) {
354
-			$fn();
355
-		}
356
-
357
-		try {
358
-			(new Query($this->getPdo(), $this->getTableName()))
359
-				->update($this->getActiveRecordColumns())
360
-				->where(Query::Equal('id', $this->getId()))
361
-				->execute();
362
-		} catch (\PDOException $e) {
363
-			throw new ActiveRecordException($e->getMessage(), ActiveRecordException::DB_ERROR, $e);
364
-		}
365
-
366
-		return $this;
367
-	}
368
-
369
-	/**
370
-	 * {@inheritdoc}
371
-	 */
372
-	public function delete()
373
-	{
374
-		foreach ($this->deleteHooks as $colName => $fn) {
375
-			$fn();
376
-		}
377
-
378
-		try {
379
-			(new Query($this->getPdo(), $this->getTableName()))
380
-				->delete()
381
-				->where(Query::Equal('id', $this->getId()))
382
-				->execute();
383
-
384
-			$this->setId(null);
385
-		} catch (\PDOException $e) {
386
-			throw new ActiveRecordException($e->getMessage(), ActiveRecordException::DB_ERROR, $e);
387
-		}
388
-
389
-		return $this;
390
-	}
391
-
392
-	/**
393
-	 * {@inheritdoc}
394
-	 */
395
-	public function sync()
396
-	{
397
-		if (!$this->exists()) {
398
-			return $this->create();
399
-		}
400
-
401
-		return $this->update();
402
-	}
403
-
404
-	/**
405
-	 * {@inheritdoc}
406
-	 */
407
-	public function exists()
408
-	{
409
-		return $this->getId() !== null;
410
-	}
411
-
412
-	/**
413
-	 * {@inheritdoc}
414
-	 */
415
-	public function fill(array $attributes)
416
-	{
417
-		$columns = $this->getActiveRecordColumns();
418
-		$columns['id'] = &$this->id;
419
-
420
-		foreach ($attributes as $key => $value) {
421
-			if (array_key_exists($key, $columns)) {
422
-				$columns[$key] = $value;
423
-			}
424
-		}
425
-
426
-		return $this;
427
-	}
428
-
429
-	/**
430
-	 * Returns the serialized form of the specified columns
431
-	 * 
432
-	 * @return Array
433
-	 */
434
-	public function toArray(Array $fieldWhitelist)
435
-	{
436
-		$output = [];
437
-		foreach ($this->tableDefinition as $colName => $definition) {
438
-			if (in_array($colName, $fieldWhitelist)) {
439
-				$output[$colName] = $definition['value'];
440
-			}
441
-		}
442
-
443
-		return $output;
444
-	}
445
-
446
-	/**
447
-	 * {@inheritdoc}
448
-	 */
449
-	public function search(array $ignoredTraits = [])
450
-	{
451
-		$clauses = [];
452
-		foreach ($this->searchHooks as $column => $fn) {
453
-			if (!in_array($column, $ignoredTraits)) {
454
-				$clauses[] = $fn();
455
-			}
456
-		}
457
-
458
-		return new ActiveRecordQuery($this, $clauses);
459
-	}
460
-
461
-	/**
462
-	 * Returns the PDO.
463
-	 *
464
-	 * @return \PDO the PDO.
465
-	 */
466
-	public function getPdo()
467
-	{
468
-		return $this->pdo;
469
-	}
470
-
471
-	/**
472
-	 * Set the PDO.
473
-	 *
474
-	 * @param \PDO $pdo
475
-	 * @return $this
476
-	 */
477
-	protected function setPdo($pdo)
478
-	{
479
-		$this->pdo = $pdo;
480
-
481
-		return $this;
482
-	}
483
-
484
-	/**
485
-	 * Returns the ID.
486
-	 *
487
-	 * @return null|int The ID.
488
-	 */
489
-	public function getId()
490
-	{
491
-		return $this->id;
492
-	}
493
-
494
-	/**
495
-	 * Set the ID.
496
-	 *
497
-	 * @param int $id
498
-	 * @return $this
499
-	 */
500
-	protected function setId($id)
501
-	{
502
-		$this->id = $id;
503
-
504
-		return $this;
505
-	}
506
-
507
-	public function getFinalTableDefinition()
508
-	{
509
-		return $this->tableDefinition;
510
-	}
511
-
512
-	public function newInstance()
513
-	{
514
-		return new static($this->pdo);
515
-	}
516
-
517
-	/**
518
-	 * Returns the active record table.
519
-	 *
520
-	 * @return string the active record table name.
521
-	 */
522
-	abstract public function getTableName(): string;
523
-
524
-	/**
525
-	 * Returns the active record columns.
526
-	 *
527
-	 * @return array the active record columns.
528
-	 */
529
-	abstract protected function getTableDefinition(): Array;
335
+            if ($row === false) {
336
+                $msg = sprintf('Can not read the non-existent active record entry %d from the `%s` table.', $id, $this->getTableName());
337
+                throw new ActiveRecordException($msg, ActiveRecordException::NOT_FOUND);
338
+            }
339
+
340
+            $this->fill($row)->setId($id);
341
+        } catch (\PDOException $e) {
342
+            throw new ActiveRecordException($e->getMessage(), ActiveRecordException::DB_ERROR, $e);
343
+        }
344
+
345
+        return $this;
346
+    }
347
+
348
+    /**
349
+     * {@inheritdoc}
350
+     */
351
+    public function update()
352
+    {
353
+        foreach ($this->updateHooks as $colName => $fn) {
354
+            $fn();
355
+        }
356
+
357
+        try {
358
+            (new Query($this->getPdo(), $this->getTableName()))
359
+                ->update($this->getActiveRecordColumns())
360
+                ->where(Query::Equal('id', $this->getId()))
361
+                ->execute();
362
+        } catch (\PDOException $e) {
363
+            throw new ActiveRecordException($e->getMessage(), ActiveRecordException::DB_ERROR, $e);
364
+        }
365
+
366
+        return $this;
367
+    }
368
+
369
+    /**
370
+     * {@inheritdoc}
371
+     */
372
+    public function delete()
373
+    {
374
+        foreach ($this->deleteHooks as $colName => $fn) {
375
+            $fn();
376
+        }
377
+
378
+        try {
379
+            (new Query($this->getPdo(), $this->getTableName()))
380
+                ->delete()
381
+                ->where(Query::Equal('id', $this->getId()))
382
+                ->execute();
383
+
384
+            $this->setId(null);
385
+        } catch (\PDOException $e) {
386
+            throw new ActiveRecordException($e->getMessage(), ActiveRecordException::DB_ERROR, $e);
387
+        }
388
+
389
+        return $this;
390
+    }
391
+
392
+    /**
393
+     * {@inheritdoc}
394
+     */
395
+    public function sync()
396
+    {
397
+        if (!$this->exists()) {
398
+            return $this->create();
399
+        }
400
+
401
+        return $this->update();
402
+    }
403
+
404
+    /**
405
+     * {@inheritdoc}
406
+     */
407
+    public function exists()
408
+    {
409
+        return $this->getId() !== null;
410
+    }
411
+
412
+    /**
413
+     * {@inheritdoc}
414
+     */
415
+    public function fill(array $attributes)
416
+    {
417
+        $columns = $this->getActiveRecordColumns();
418
+        $columns['id'] = &$this->id;
419
+
420
+        foreach ($attributes as $key => $value) {
421
+            if (array_key_exists($key, $columns)) {
422
+                $columns[$key] = $value;
423
+            }
424
+        }
425
+
426
+        return $this;
427
+    }
428
+
429
+    /**
430
+     * Returns the serialized form of the specified columns
431
+     * 
432
+     * @return Array
433
+     */
434
+    public function toArray(Array $fieldWhitelist)
435
+    {
436
+        $output = [];
437
+        foreach ($this->tableDefinition as $colName => $definition) {
438
+            if (in_array($colName, $fieldWhitelist)) {
439
+                $output[$colName] = $definition['value'];
440
+            }
441
+        }
442
+
443
+        return $output;
444
+    }
445
+
446
+    /**
447
+     * {@inheritdoc}
448
+     */
449
+    public function search(array $ignoredTraits = [])
450
+    {
451
+        $clauses = [];
452
+        foreach ($this->searchHooks as $column => $fn) {
453
+            if (!in_array($column, $ignoredTraits)) {
454
+                $clauses[] = $fn();
455
+            }
456
+        }
457
+
458
+        return new ActiveRecordQuery($this, $clauses);
459
+    }
460
+
461
+    /**
462
+     * Returns the PDO.
463
+     *
464
+     * @return \PDO the PDO.
465
+     */
466
+    public function getPdo()
467
+    {
468
+        return $this->pdo;
469
+    }
470
+
471
+    /**
472
+     * Set the PDO.
473
+     *
474
+     * @param \PDO $pdo
475
+     * @return $this
476
+     */
477
+    protected function setPdo($pdo)
478
+    {
479
+        $this->pdo = $pdo;
480
+
481
+        return $this;
482
+    }
483
+
484
+    /**
485
+     * Returns the ID.
486
+     *
487
+     * @return null|int The ID.
488
+     */
489
+    public function getId()
490
+    {
491
+        return $this->id;
492
+    }
493
+
494
+    /**
495
+     * Set the ID.
496
+     *
497
+     * @param int $id
498
+     * @return $this
499
+     */
500
+    protected function setId($id)
501
+    {
502
+        $this->id = $id;
503
+
504
+        return $this;
505
+    }
506
+
507
+    public function getFinalTableDefinition()
508
+    {
509
+        return $this->tableDefinition;
510
+    }
511
+
512
+    public function newInstance()
513
+    {
514
+        return new static($this->pdo);
515
+    }
516
+
517
+    /**
518
+     * Returns the active record table.
519
+     *
520
+     * @return string the active record table name.
521
+     */
522
+    abstract public function getTableName(): string;
523
+
524
+    /**
525
+     * Returns the active record columns.
526
+     *
527
+     * @return array the active record columns.
528
+     */
529
+    abstract protected function getTableDefinition(): Array;
530 530
 }
Please login to merge, or discard this patch.
src/ActiveRecordException.php 1 patch
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -16,6 +16,6 @@
 block discarded – undo
16 16
  */
17 17
 class ActiveRecordException extends \RuntimeException
18 18
 {
19
-	const NOT_FOUND = 1;
20
-	const DB_ERROR = 2;
19
+    const NOT_FOUND = 1;
20
+    const DB_ERROR = 2;
21 21
 }
Please login to merge, or discard this patch.