@@ -14,187 +14,187 @@ |
||
| 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(string $columnName, $definition); |
|
| 170 | + /** |
|
| 171 | + * @return void |
|
| 172 | + */ |
|
| 173 | + abstract protected function extendTableDefinition(string $columnName, $definition); |
|
| 174 | 174 | |
| 175 | - /** |
|
| 176 | - * @return void |
|
| 177 | - */ |
|
| 178 | - abstract protected function registerSearchHook(string $columnName, $fn); |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @return void |
|
| 182 | - */ |
|
| 183 | - abstract protected function registerDeleteHook(string $columnName, $fn); |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @return void |
|
| 187 | - */ |
|
| 188 | - abstract protected function registerUpdateHook(string $columnName, $fn); |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * @return void |
|
| 192 | - */ |
|
| 193 | - abstract protected function registerReadHook(string $columnName, $fn); |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * @return void |
|
| 197 | - */ |
|
| 198 | - abstract protected function registerCreateHook(string $columnName, $fn); |
|
| 175 | + /** |
|
| 176 | + * @return void |
|
| 177 | + */ |
|
| 178 | + abstract protected function registerSearchHook(string $columnName, $fn); |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @return void |
|
| 182 | + */ |
|
| 183 | + abstract protected function registerDeleteHook(string $columnName, $fn); |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @return void |
|
| 187 | + */ |
|
| 188 | + abstract protected function registerUpdateHook(string $columnName, $fn); |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * @return void |
|
| 192 | + */ |
|
| 193 | + abstract protected function registerReadHook(string $columnName, $fn); |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * @return void |
|
| 197 | + */ |
|
| 198 | + abstract protected function registerCreateHook(string $columnName, $fn); |
|
| 199 | 199 | |
| 200 | 200 | } |
| 201 | 201 | \ No newline at end of file |
@@ -9,110 +9,110 @@ |
||
| 9 | 9 | |
| 10 | 10 | Trait ManyToManyRelation |
| 11 | 11 | { |
| 12 | - // These variables are relevant for internal bookkeeping (constraint generation etc) |
|
| 13 | - |
|
| 14 | - /** @var string The name of the left column of the relation. */ |
|
| 15 | - private $_leftColumnName; |
|
| 16 | - |
|
| 17 | - /** @var string The name of the right column of the relation. */ |
|
| 18 | - private $_rightColumnName; |
|
| 19 | - |
|
| 20 | - /** @var string The name of the left table of the relation. */ |
|
| 21 | - private $_leftEntityTable; |
|
| 22 | - |
|
| 23 | - /** @var string The name of the right table of the relation. */ |
|
| 24 | - private $_rightEntityTable; |
|
| 25 | - |
|
| 26 | - /** @var \PDO The PDO object. */ |
|
| 27 | - protected $pdo; |
|
| 28 | - /** |
|
| 29 | - * Initializes the the ManyToManyRelation trait on the included object |
|
| 30 | - * |
|
| 31 | - * @param AbstractActiveRecord $leftEntity The left entity of the relation |
|
| 32 | - * @param int $leftVariable The reference to the variable where the id for the left entity will be stored |
|
| 33 | - * @param AbstractActiveRecord $rightEntity The left entity of the relation |
|
| 34 | - * @param int $leftVariable The reference to the variable where the id for the right entity will be stored |
|
| 35 | - * @return void |
|
| 36 | - */ |
|
| 37 | - protected function initManyToManyRelation(AbstractActiveRecord $leftEntity, &$leftVariable, AbstractActiveRecord $rightEntity, &$rightVariable) |
|
| 38 | - { |
|
| 39 | - $this->_leftEntityTable = $leftEntity->getTableName(); |
|
| 40 | - $this->_rightEntityTable = $rightEntity->getTableName(); |
|
| 41 | - |
|
| 42 | - if (get_class($leftEntity) === get_class($rightEntity)) { |
|
| 43 | - $this->_leftColumnName = sprintf("id_%s_left", $leftEntity->getTableName()); |
|
| 44 | - $this->_rightColumnName = sprintf("id_%s_right", $rightEntity->getTableName()); |
|
| 45 | - } else { |
|
| 46 | - $this->_leftColumnName = sprintf("id_%s", $leftEntity->getTableName()); |
|
| 47 | - $this->_rightColumnName = sprintf("id_%s", $rightEntity->getTableName()); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - $this->extendTableDefinition($this->_leftColumnName, [ |
|
| 51 | - 'value' => &$leftVariable, |
|
| 52 | - 'validate' => null, |
|
| 53 | - 'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
| 54 | - 'properties' => ColumnProperty::NOT_NULL |
|
| 55 | - ]); |
|
| 56 | - |
|
| 57 | - $this->extendTableDefinition($this->_rightColumnName, [ |
|
| 58 | - 'value' => &$rightVariable, |
|
| 59 | - 'validate' => null, |
|
| 60 | - 'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
| 61 | - 'properties' => ColumnProperty::NOT_NULL |
|
| 62 | - ]); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Build the constraints for the many-to-many relation table |
|
| 67 | - * @return void |
|
| 68 | - */ |
|
| 69 | - public function createTableConstraints() |
|
| 70 | - { |
|
| 71 | - $childTable = $this->getTableName(); |
|
| 72 | - |
|
| 73 | - $leftParentTable = $this->_leftEntityTable; |
|
| 74 | - $rightParentTable = $this->_rightEntityTable; |
|
| 75 | - |
|
| 76 | - $leftConstraint = SchemaBuilder::buildConstraintOnDeleteCascade($leftParentTable, 'id', $childTable, $this->_leftColumnName); |
|
| 77 | - $rightConstraint = SchemaBuilder::buildConstraintOnDeleteCascade($rightParentTable, 'id', $childTable, $this->_rightColumnName); |
|
| 78 | - |
|
| 79 | - $this->pdo->query($leftConstraint); |
|
| 80 | - $this->pdo->query($rightConstraint); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @return void |
|
| 85 | - */ |
|
| 86 | - abstract public function getTableName(); |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @return void |
|
| 90 | - */ |
|
| 91 | - abstract protected function extendTableDefinition(string $columnName, $definition); |
|
| 12 | + // These variables are relevant for internal bookkeeping (constraint generation etc) |
|
| 13 | + |
|
| 14 | + /** @var string The name of the left column of the relation. */ |
|
| 15 | + private $_leftColumnName; |
|
| 16 | + |
|
| 17 | + /** @var string The name of the right column of the relation. */ |
|
| 18 | + private $_rightColumnName; |
|
| 19 | + |
|
| 20 | + /** @var string The name of the left table of the relation. */ |
|
| 21 | + private $_leftEntityTable; |
|
| 22 | + |
|
| 23 | + /** @var string The name of the right table of the relation. */ |
|
| 24 | + private $_rightEntityTable; |
|
| 25 | + |
|
| 26 | + /** @var \PDO The PDO object. */ |
|
| 27 | + protected $pdo; |
|
| 28 | + /** |
|
| 29 | + * Initializes the the ManyToManyRelation trait on the included object |
|
| 30 | + * |
|
| 31 | + * @param AbstractActiveRecord $leftEntity The left entity of the relation |
|
| 32 | + * @param int $leftVariable The reference to the variable where the id for the left entity will be stored |
|
| 33 | + * @param AbstractActiveRecord $rightEntity The left entity of the relation |
|
| 34 | + * @param int $leftVariable The reference to the variable where the id for the right entity will be stored |
|
| 35 | + * @return void |
|
| 36 | + */ |
|
| 37 | + protected function initManyToManyRelation(AbstractActiveRecord $leftEntity, &$leftVariable, AbstractActiveRecord $rightEntity, &$rightVariable) |
|
| 38 | + { |
|
| 39 | + $this->_leftEntityTable = $leftEntity->getTableName(); |
|
| 40 | + $this->_rightEntityTable = $rightEntity->getTableName(); |
|
| 41 | + |
|
| 42 | + if (get_class($leftEntity) === get_class($rightEntity)) { |
|
| 43 | + $this->_leftColumnName = sprintf("id_%s_left", $leftEntity->getTableName()); |
|
| 44 | + $this->_rightColumnName = sprintf("id_%s_right", $rightEntity->getTableName()); |
|
| 45 | + } else { |
|
| 46 | + $this->_leftColumnName = sprintf("id_%s", $leftEntity->getTableName()); |
|
| 47 | + $this->_rightColumnName = sprintf("id_%s", $rightEntity->getTableName()); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + $this->extendTableDefinition($this->_leftColumnName, [ |
|
| 51 | + 'value' => &$leftVariable, |
|
| 52 | + 'validate' => null, |
|
| 53 | + 'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
| 54 | + 'properties' => ColumnProperty::NOT_NULL |
|
| 55 | + ]); |
|
| 56 | + |
|
| 57 | + $this->extendTableDefinition($this->_rightColumnName, [ |
|
| 58 | + 'value' => &$rightVariable, |
|
| 59 | + 'validate' => null, |
|
| 60 | + 'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
| 61 | + 'properties' => ColumnProperty::NOT_NULL |
|
| 62 | + ]); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Build the constraints for the many-to-many relation table |
|
| 67 | + * @return void |
|
| 68 | + */ |
|
| 69 | + public function createTableConstraints() |
|
| 70 | + { |
|
| 71 | + $childTable = $this->getTableName(); |
|
| 72 | + |
|
| 73 | + $leftParentTable = $this->_leftEntityTable; |
|
| 74 | + $rightParentTable = $this->_rightEntityTable; |
|
| 75 | + |
|
| 76 | + $leftConstraint = SchemaBuilder::buildConstraintOnDeleteCascade($leftParentTable, 'id', $childTable, $this->_leftColumnName); |
|
| 77 | + $rightConstraint = SchemaBuilder::buildConstraintOnDeleteCascade($rightParentTable, 'id', $childTable, $this->_rightColumnName); |
|
| 78 | + |
|
| 79 | + $this->pdo->query($leftConstraint); |
|
| 80 | + $this->pdo->query($rightConstraint); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @return void |
|
| 85 | + */ |
|
| 86 | + abstract public function getTableName(); |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @return void |
|
| 90 | + */ |
|
| 91 | + abstract protected function extendTableDefinition(string $columnName, $definition); |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * @return void |
|
| 95 | - */ |
|
| 96 | - abstract protected function registerSearchHook(string $columnName, $fn); |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @return void |
|
| 100 | - */ |
|
| 101 | - abstract protected function registerDeleteHook(string $columnName, $fn); |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @return void |
|
| 105 | - */ |
|
| 106 | - abstract protected function registerUpdateHook(string $columnName, $fn); |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @return void |
|
| 110 | - */ |
|
| 111 | - abstract protected function registerReadHook(string $columnName, $fn); |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @return void |
|
| 115 | - */ |
|
| 116 | - abstract protected function registerCreateHook(string $columnName, $fn); |
|
| 93 | + /** |
|
| 94 | + * @return void |
|
| 95 | + */ |
|
| 96 | + abstract protected function registerSearchHook(string $columnName, $fn); |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @return void |
|
| 100 | + */ |
|
| 101 | + abstract protected function registerDeleteHook(string $columnName, $fn); |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @return void |
|
| 105 | + */ |
|
| 106 | + abstract protected function registerUpdateHook(string $columnName, $fn); |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @return void |
|
| 110 | + */ |
|
| 111 | + abstract protected function registerReadHook(string $columnName, $fn); |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @return void |
|
| 115 | + */ |
|
| 116 | + abstract protected function registerCreateHook(string $columnName, $fn); |
|
| 117 | 117 | |
| 118 | 118 | } |
@@ -19,79 +19,79 @@ |
||
| 19 | 19 | interface ActiveRecordInterface |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - public function __construct(\PDO $pdo); |
|
| 22 | + public function __construct(\PDO $pdo); |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Returns the ID of the record. |
|
| 26 | - * |
|
| 27 | - * @return null|int The ID. |
|
| 28 | - */ |
|
| 29 | - public function getId(): ?int; |
|
| 24 | + /** |
|
| 25 | + * Returns the ID of the record. |
|
| 26 | + * |
|
| 27 | + * @return null|int The ID. |
|
| 28 | + */ |
|
| 29 | + public function getId(): ?int; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Returns this active record after creating an entry with the records attributes. |
|
| 33 | - * |
|
| 34 | - * @return $this |
|
| 35 | - * @throws ActiveRecordException on failure. |
|
| 36 | - */ |
|
| 37 | - public function create(); |
|
| 31 | + /** |
|
| 32 | + * Returns this active record after creating an entry with the records attributes. |
|
| 33 | + * |
|
| 34 | + * @return $this |
|
| 35 | + * @throws ActiveRecordException on failure. |
|
| 36 | + */ |
|
| 37 | + public function create(); |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Returns this active record after reading the attributes from the entry with the given identifier. |
|
| 41 | - * |
|
| 42 | - * @param mixed $id |
|
| 43 | - * @return $this |
|
| 44 | - * @throws ActiveRecordException on failure. |
|
| 45 | - */ |
|
| 46 | - public function read($id); |
|
| 39 | + /** |
|
| 40 | + * Returns this active record after reading the attributes from the entry with the given identifier. |
|
| 41 | + * |
|
| 42 | + * @param mixed $id |
|
| 43 | + * @return $this |
|
| 44 | + * @throws ActiveRecordException on failure. |
|
| 45 | + */ |
|
| 46 | + public function read($id); |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Returns this active record after updating the attributes to the corresponding entry. |
|
| 50 | - * |
|
| 51 | - * @return $this |
|
| 52 | - * @throws ActiveRecordException on failure. |
|
| 53 | - */ |
|
| 54 | - public function update(); |
|
| 48 | + /** |
|
| 49 | + * Returns this active record after updating the attributes to the corresponding entry. |
|
| 50 | + * |
|
| 51 | + * @return $this |
|
| 52 | + * @throws ActiveRecordException on failure. |
|
| 53 | + */ |
|
| 54 | + public function update(); |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Returns this record after deleting the corresponding entry. |
|
| 58 | - * |
|
| 59 | - * @return $this |
|
| 60 | - * @throws ActiveRecordException on failure. |
|
| 61 | - */ |
|
| 62 | - public function delete(); |
|
| 56 | + /** |
|
| 57 | + * Returns this record after deleting the corresponding entry. |
|
| 58 | + * |
|
| 59 | + * @return $this |
|
| 60 | + * @throws ActiveRecordException on failure. |
|
| 61 | + */ |
|
| 62 | + public function delete(); |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Returns this record after synchronizing it with the corresponding entry. |
|
| 66 | - * A new entry is created if this active record does not have a corresponding entry. |
|
| 67 | - * |
|
| 68 | - * @return $this |
|
| 69 | - * @throws ActiveRecordException on failure. |
|
| 70 | - */ |
|
| 71 | - public function sync(); |
|
| 64 | + /** |
|
| 65 | + * Returns this record after synchronizing it with the corresponding entry. |
|
| 66 | + * A new entry is created if this active record does not have a corresponding entry. |
|
| 67 | + * |
|
| 68 | + * @return $this |
|
| 69 | + * @throws ActiveRecordException on failure. |
|
| 70 | + */ |
|
| 71 | + public function sync(); |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Returns true if this active record has a corresponding entry. |
|
| 75 | - * |
|
| 76 | - * @return bool true if this active record has a corresponding entry. |
|
| 77 | - */ |
|
| 78 | - public function exists(); |
|
| 73 | + /** |
|
| 74 | + * Returns true if this active record has a corresponding entry. |
|
| 75 | + * |
|
| 76 | + * @return bool true if this active record has a corresponding entry. |
|
| 77 | + */ |
|
| 78 | + public function exists(); |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Returns this record after filling it with the given attributes. |
|
| 82 | - * |
|
| 83 | - * @param array $attributes = [] |
|
| 84 | - * @return $this |
|
| 85 | - * @throws ActiveRecordException on failure. |
|
| 86 | - */ |
|
| 87 | - public function fill(array $attributes); |
|
| 80 | + /** |
|
| 81 | + * Returns this record after filling it with the given attributes. |
|
| 82 | + * |
|
| 83 | + * @param array $attributes = [] |
|
| 84 | + * @return $this |
|
| 85 | + * @throws ActiveRecordException on failure. |
|
| 86 | + */ |
|
| 87 | + public function fill(array $attributes); |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Returns the records with the given where, order by, limit and offset clauses. |
|
| 91 | - * |
|
| 92 | - * @param array $excludedTraits |
|
| 93 | - * @return ActiveRecordQuery the query representing the current search. |
|
| 94 | - * @throws ActiveRecordException on failure. |
|
| 95 | - */ |
|
| 96 | - public function search(Array $excludedTraits); |
|
| 89 | + /** |
|
| 90 | + * Returns the records with the given where, order by, limit and offset clauses. |
|
| 91 | + * |
|
| 92 | + * @param array $excludedTraits |
|
| 93 | + * @return ActiveRecordQuery the query representing the current search. |
|
| 94 | + * @throws ActiveRecordException on failure. |
|
| 95 | + */ |
|
| 96 | + public function search(Array $excludedTraits); |
|
| 97 | 97 | } |