@@ -19,83 +19,83 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | interface ActiveRecordInterface |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * Returns this active record after creating an entry with the records attributes. |
|
| 24 | - * |
|
| 25 | - * @return $this |
|
| 26 | - * @throws ActiveRecordException on failure. |
|
| 27 | - */ |
|
| 28 | - public function create(); |
|
| 22 | + /** |
|
| 23 | + * Returns this active record after creating an entry with the records attributes. |
|
| 24 | + * |
|
| 25 | + * @return $this |
|
| 26 | + * @throws ActiveRecordException on failure. |
|
| 27 | + */ |
|
| 28 | + public function create(); |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Returns this active record after reading the attributes from the entry with the given identifier. |
|
| 32 | - * |
|
| 33 | - * @param mixed $id |
|
| 34 | - * @return $this |
|
| 35 | - * @throws ActiveRecordException on failure. |
|
| 36 | - */ |
|
| 37 | - public function read($id); |
|
| 30 | + /** |
|
| 31 | + * Returns this active record after reading the attributes from the entry with the given identifier. |
|
| 32 | + * |
|
| 33 | + * @param mixed $id |
|
| 34 | + * @return $this |
|
| 35 | + * @throws ActiveRecordException on failure. |
|
| 36 | + */ |
|
| 37 | + public function read($id); |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Returns this active record after updating the attributes to the corresponding entry. |
|
| 41 | - * |
|
| 42 | - * @return $this |
|
| 43 | - * @throws ActiveRecordException on failure. |
|
| 44 | - */ |
|
| 45 | - public function update(); |
|
| 39 | + /** |
|
| 40 | + * Returns this active record after updating the attributes to the corresponding entry. |
|
| 41 | + * |
|
| 42 | + * @return $this |
|
| 43 | + * @throws ActiveRecordException on failure. |
|
| 44 | + */ |
|
| 45 | + public function update(); |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Returns this record after deleting the corresponding entry. |
|
| 49 | - * |
|
| 50 | - * @return $this |
|
| 51 | - * @throws ActiveRecordException on failure. |
|
| 52 | - */ |
|
| 53 | - public function delete(); |
|
| 47 | + /** |
|
| 48 | + * Returns this record after deleting the corresponding entry. |
|
| 49 | + * |
|
| 50 | + * @return $this |
|
| 51 | + * @throws ActiveRecordException on failure. |
|
| 52 | + */ |
|
| 53 | + public function delete(); |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Returns this record after synchronizing it with the corresponding entry. |
|
| 57 | - * A new entry is created if this active record does not have a corresponding entry. |
|
| 58 | - * |
|
| 59 | - * @return $this |
|
| 60 | - * @throws ActiveRecordException on failure. |
|
| 61 | - */ |
|
| 62 | - public function sync(); |
|
| 55 | + /** |
|
| 56 | + * Returns this record after synchronizing it with the corresponding entry. |
|
| 57 | + * A new entry is created if this active record does not have a corresponding entry. |
|
| 58 | + * |
|
| 59 | + * @return $this |
|
| 60 | + * @throws ActiveRecordException on failure. |
|
| 61 | + */ |
|
| 62 | + public function sync(); |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Returns true if this active record has a corresponding entry. |
|
| 66 | - * |
|
| 67 | - * @return bool true if this active record has a corresponding entry. |
|
| 68 | - */ |
|
| 69 | - public function exists(); |
|
| 64 | + /** |
|
| 65 | + * Returns true if this active record has a corresponding entry. |
|
| 66 | + * |
|
| 67 | + * @return bool true if this active record has a corresponding entry. |
|
| 68 | + */ |
|
| 69 | + public function exists(); |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Returns this record after filling it with the given attributes. |
|
| 73 | - * |
|
| 74 | - * @param array $attributes = [] |
|
| 75 | - * @return $this |
|
| 76 | - * @throws ActiveRecordException on failure. |
|
| 77 | - */ |
|
| 78 | - public function fill(array $attributes); |
|
| 71 | + /** |
|
| 72 | + * Returns this record after filling it with the given attributes. |
|
| 73 | + * |
|
| 74 | + * @param array $attributes = [] |
|
| 75 | + * @return $this |
|
| 76 | + * @throws ActiveRecordException on failure. |
|
| 77 | + */ |
|
| 78 | + public function fill(array $attributes); |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Returns this record after filling it with the attributes of the first entry with the given where and order by clauses. |
|
| 82 | - * |
|
| 83 | - * @param array $where = [] |
|
| 84 | - * @param array $orderBy = [] |
|
| 85 | - * @return $this |
|
| 86 | - * @throws ActiveRecordException on failure. |
|
| 87 | - */ |
|
| 88 | - public function searchOne(array $where = [], array $orderBy = []); |
|
| 80 | + /** |
|
| 81 | + * Returns this record after filling it with the attributes of the first entry with the given where and order by clauses. |
|
| 82 | + * |
|
| 83 | + * @param array $where = [] |
|
| 84 | + * @param array $orderBy = [] |
|
| 85 | + * @return $this |
|
| 86 | + * @throws ActiveRecordException on failure. |
|
| 87 | + */ |
|
| 88 | + public function searchOne(array $where = [], array $orderBy = []); |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Returns the records with the given where, order by, limit and offset clauses. |
|
| 92 | - * |
|
| 93 | - * @param array $where = [] |
|
| 94 | - * @param array $orderBy = [] |
|
| 95 | - * @param int $limit = -1 |
|
| 96 | - * @param int $offset = 0 |
|
| 97 | - * @return static[] the records with the given where, order by, limit and offset clauses. |
|
| 98 | - * @throws ActiveRecordException on failure. |
|
| 99 | - */ |
|
| 100 | - public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0); |
|
| 90 | + /** |
|
| 91 | + * Returns the records with the given where, order by, limit and offset clauses. |
|
| 92 | + * |
|
| 93 | + * @param array $where = [] |
|
| 94 | + * @param array $orderBy = [] |
|
| 95 | + * @param int $limit = -1 |
|
| 96 | + * @param int $offset = 0 |
|
| 97 | + * @return static[] the records with the given where, order by, limit and offset clauses. |
|
| 98 | + * @throws ActiveRecordException on failure. |
|
| 99 | + */ |
|
| 100 | + public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0); |
|
| 101 | 101 | } |
@@ -2,8 +2,6 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace miBadger\ActiveRecord\Traits; |
| 4 | 4 | |
| 5 | -use miBadger\ActiveRecord\ColumnProperty; |
|
| 6 | - |
|
| 7 | 5 | const TRAIT_ADDRESS_FIELD_ADDRESS = "address_address"; |
| 8 | 6 | const TRAIT_ADDRESS_FIELD_ZIPCODE = "address_zipcode"; |
| 9 | 7 | const TRAIT_ADDRESS_FIELD_CITY = "address_city"; |
@@ -11,104 +11,104 @@ |
||
| 11 | 11 | |
| 12 | 12 | trait Address |
| 13 | 13 | { |
| 14 | - protected $address; |
|
| 15 | - |
|
| 16 | - protected $zipcode; |
|
| 17 | - |
|
| 18 | - protected $city; |
|
| 19 | - |
|
| 20 | - protected $country; |
|
| 21 | - |
|
| 22 | - protected function initAddress() |
|
| 23 | - { |
|
| 24 | - $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ADDRESS, [ |
|
| 25 | - 'value' => &$this->address, |
|
| 26 | - 'validate' => null, |
|
| 27 | - 'type' => 'VARCHAR', |
|
| 28 | - 'length' => 1024, |
|
| 29 | - 'properties' => null |
|
| 30 | - ]); |
|
| 31 | - |
|
| 32 | - $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ZIPCODE, [ |
|
| 33 | - 'value' => &$this->zipcode, |
|
| 34 | - 'validate' => null, |
|
| 35 | - 'type' => 'VARCHAR', |
|
| 36 | - 'length' => 1024, |
|
| 37 | - 'properties' => null |
|
| 38 | - ]); |
|
| 39 | - |
|
| 40 | - $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_CITY, [ |
|
| 41 | - 'value' => &$this->city, |
|
| 42 | - 'validate' => null, |
|
| 43 | - 'type' => 'VARCHAR', |
|
| 44 | - 'length' => 1024, |
|
| 45 | - 'properties' => null |
|
| 46 | - ]); |
|
| 47 | - |
|
| 48 | - $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_COUNTRY, [ |
|
| 49 | - 'value' => &$this->country, |
|
| 50 | - 'validate' => null, |
|
| 51 | - 'type' => 'VARCHAR', |
|
| 52 | - 'length' => 1024, |
|
| 53 | - 'properties' => null |
|
| 54 | - ]); |
|
| 55 | - |
|
| 56 | - $this->address = null; |
|
| 57 | - $this->zipcode = null; |
|
| 58 | - $this->city = null; |
|
| 59 | - $this->country = null; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function getAddress() |
|
| 63 | - { |
|
| 64 | - return $this->address; |
|
| 65 | - } |
|
| 14 | + protected $address; |
|
| 15 | + |
|
| 16 | + protected $zipcode; |
|
| 17 | + |
|
| 18 | + protected $city; |
|
| 19 | + |
|
| 20 | + protected $country; |
|
| 21 | + |
|
| 22 | + protected function initAddress() |
|
| 23 | + { |
|
| 24 | + $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ADDRESS, [ |
|
| 25 | + 'value' => &$this->address, |
|
| 26 | + 'validate' => null, |
|
| 27 | + 'type' => 'VARCHAR', |
|
| 28 | + 'length' => 1024, |
|
| 29 | + 'properties' => null |
|
| 30 | + ]); |
|
| 31 | + |
|
| 32 | + $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ZIPCODE, [ |
|
| 33 | + 'value' => &$this->zipcode, |
|
| 34 | + 'validate' => null, |
|
| 35 | + 'type' => 'VARCHAR', |
|
| 36 | + 'length' => 1024, |
|
| 37 | + 'properties' => null |
|
| 38 | + ]); |
|
| 39 | + |
|
| 40 | + $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_CITY, [ |
|
| 41 | + 'value' => &$this->city, |
|
| 42 | + 'validate' => null, |
|
| 43 | + 'type' => 'VARCHAR', |
|
| 44 | + 'length' => 1024, |
|
| 45 | + 'properties' => null |
|
| 46 | + ]); |
|
| 47 | + |
|
| 48 | + $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_COUNTRY, [ |
|
| 49 | + 'value' => &$this->country, |
|
| 50 | + 'validate' => null, |
|
| 51 | + 'type' => 'VARCHAR', |
|
| 52 | + 'length' => 1024, |
|
| 53 | + 'properties' => null |
|
| 54 | + ]); |
|
| 55 | + |
|
| 56 | + $this->address = null; |
|
| 57 | + $this->zipcode = null; |
|
| 58 | + $this->city = null; |
|
| 59 | + $this->country = null; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function getAddress() |
|
| 63 | + { |
|
| 64 | + return $this->address; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - public function setAddress($address) |
|
| 68 | - { |
|
| 69 | - $this->address = $address; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - public function getZipcode() |
|
| 73 | - { |
|
| 74 | - return $this->zipcode; |
|
| 75 | - } |
|
| 67 | + public function setAddress($address) |
|
| 68 | + { |
|
| 69 | + $this->address = $address; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + public function getZipcode() |
|
| 73 | + { |
|
| 74 | + return $this->zipcode; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - public function setZipcode($zipcode) |
|
| 78 | - { |
|
| 79 | - $this->zipcode = $zipcode; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function getCity() |
|
| 83 | - { |
|
| 84 | - return $this->city; |
|
| 85 | - } |
|
| 77 | + public function setZipcode($zipcode) |
|
| 78 | + { |
|
| 79 | + $this->zipcode = $zipcode; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function getCity() |
|
| 83 | + { |
|
| 84 | + return $this->city; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - public function setCity($city) |
|
| 88 | - { |
|
| 89 | - $this->city = $city; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function getCountry() |
|
| 93 | - { |
|
| 94 | - return $this->country; |
|
| 95 | - } |
|
| 87 | + public function setCity($city) |
|
| 88 | + { |
|
| 89 | + $this->city = $city; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function getCountry() |
|
| 93 | + { |
|
| 94 | + return $this->country; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - public function setCountry($country) |
|
| 98 | - { |
|
| 99 | - $this->country = $country; |
|
| 100 | - } |
|
| 97 | + public function setCountry($country) |
|
| 98 | + { |
|
| 99 | + $this->country = $country; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - abstract function extendTableDefinition($columnName, $definition); |
|
| 102 | + abstract function extendTableDefinition($columnName, $definition); |
|
| 103 | 103 | |
| 104 | - abstract function registerSearchHook($columnName, $fn); |
|
| 104 | + abstract function registerSearchHook($columnName, $fn); |
|
| 105 | 105 | |
| 106 | - abstract function registerDeleteHook($columnName, $fn); |
|
| 106 | + abstract function registerDeleteHook($columnName, $fn); |
|
| 107 | 107 | |
| 108 | - abstract function registerUpdateHook($columnName, $fn); |
|
| 108 | + abstract function registerUpdateHook($columnName, $fn); |
|
| 109 | 109 | |
| 110 | - abstract function registerReadHook($columnName, $fn); |
|
| 110 | + abstract function registerReadHook($columnName, $fn); |
|
| 111 | 111 | |
| 112 | - abstract function registerCreateHook($columnName, $fn); |
|
| 112 | + abstract function registerCreateHook($columnName, $fn); |
|
| 113 | 113 | |
| 114 | 114 | } |
| 115 | 115 | \ No newline at end of file |
@@ -2,7 +2,6 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace miBadger\ActiveRecord\Traits; |
| 4 | 4 | |
| 5 | -use miBadger\Query\Query; |
|
| 6 | 5 | use miBadger\ActiveRecord\ColumnProperty; |
| 7 | 6 | |
| 8 | 7 | const TRAIT_DATEFIELDS_LAST_MODIFIED = "last_modified"; |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | 67 | * Returns the timestamp of last update for this record |
| 68 | - * @return DateTime |
|
| 68 | + * @return \DateTime |
|
| 69 | 69 | */ |
| 70 | 70 | public function getLastModifiedDate() |
| 71 | 71 | { |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | 76 | * Returns the timestamp of when this record was created |
| 77 | - * @return DateTime |
|
| 77 | + * @return \DateTime |
|
| 78 | 78 | */ |
| 79 | 79 | public function getCreationDate() |
| 80 | 80 | { |
@@ -88,10 +88,16 @@ discard block |
||
| 88 | 88 | |
| 89 | 89 | abstract function registerDeleteHook($columnName, $fn); |
| 90 | 90 | |
| 91 | + /** |
|
| 92 | + * @param string $fn |
|
| 93 | + */ |
|
| 91 | 94 | abstract function registerUpdateHook($columnName, $fn); |
| 92 | 95 | |
| 93 | 96 | abstract function registerReadHook($columnName, $fn); |
| 94 | 97 | |
| 98 | + /** |
|
| 99 | + * @param string $fn |
|
| 100 | + */ |
|
| 95 | 101 | abstract function registerCreateHook($columnName, $fn); |
| 96 | 102 | } |
| 97 | 103 | |
@@ -10,89 +10,89 @@ |
||
| 10 | 10 | |
| 11 | 11 | trait Datefields |
| 12 | 12 | { |
| 13 | - /** @var string The timestamp representing the moment this record was created */ |
|
| 14 | - protected $created; |
|
| 15 | - |
|
| 16 | - /** @var string The timestamp representing the moment this record was last updated */ |
|
| 17 | - protected $lastModified; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * this method is required to be called in the constructor for each class that uses this trait. |
|
| 21 | - * It adds the datefields to the table definition and registers the callback hooks |
|
| 22 | - */ |
|
| 23 | - protected function initDatefields() |
|
| 24 | - { |
|
| 25 | - $this->extendTableDefinition(TRAIT_DATEFIELDS_CREATED, [ |
|
| 26 | - 'value' => &$this->created, |
|
| 27 | - 'validate' => null, |
|
| 28 | - 'type' => 'DATETIME', |
|
| 29 | - 'default' => 'CURRENT_TIMESTAMP', |
|
| 30 | - 'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE |
|
| 31 | - ]); |
|
| 32 | - |
|
| 33 | - $this->extendTableDefinition(TRAIT_DATEFIELDS_LAST_MODIFIED, [ |
|
| 34 | - 'value' => &$this->lastModified, |
|
| 35 | - 'validate' => null, |
|
| 36 | - 'type' => 'DATETIME', |
|
| 37 | - 'default' => 'CURRENT_TIMESTAMP', |
|
| 38 | - 'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE |
|
| 39 | - ]); |
|
| 13 | + /** @var string The timestamp representing the moment this record was created */ |
|
| 14 | + protected $created; |
|
| 15 | + |
|
| 16 | + /** @var string The timestamp representing the moment this record was last updated */ |
|
| 17 | + protected $lastModified; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * this method is required to be called in the constructor for each class that uses this trait. |
|
| 21 | + * It adds the datefields to the table definition and registers the callback hooks |
|
| 22 | + */ |
|
| 23 | + protected function initDatefields() |
|
| 24 | + { |
|
| 25 | + $this->extendTableDefinition(TRAIT_DATEFIELDS_CREATED, [ |
|
| 26 | + 'value' => &$this->created, |
|
| 27 | + 'validate' => null, |
|
| 28 | + 'type' => 'DATETIME', |
|
| 29 | + 'default' => 'CURRENT_TIMESTAMP', |
|
| 30 | + 'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE |
|
| 31 | + ]); |
|
| 32 | + |
|
| 33 | + $this->extendTableDefinition(TRAIT_DATEFIELDS_LAST_MODIFIED, [ |
|
| 34 | + 'value' => &$this->lastModified, |
|
| 35 | + 'validate' => null, |
|
| 36 | + 'type' => 'DATETIME', |
|
| 37 | + 'default' => 'CURRENT_TIMESTAMP', |
|
| 38 | + 'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE |
|
| 39 | + ]); |
|
| 40 | 40 | |
| 41 | - $this->registerUpdateHook(TRAIT_DATEFIELDS_LAST_MODIFIED, 'DatefieldsUpdateHook'); |
|
| 42 | - $this->registerCreateHook(TRAIT_DATEFIELDS_LAST_MODIFIED, 'DatefieldsCreateHook'); |
|
| 43 | - |
|
| 44 | - $this->created = null; |
|
| 45 | - $this->lastModified = null; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The hook that gets called to set the timestamp whenever a new record is created |
|
| 50 | - */ |
|
| 51 | - protected function DatefieldsCreateHook() |
|
| 52 | - { |
|
| 53 | - // @TODO: Should this be split up to seperate hooks for "last_modified" and "created" for consistency? |
|
| 54 | - $this->created = (new \DateTime('now'))->format('Y-m-d H:i:s'); |
|
| 55 | - $this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s'); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * The hook that gets called to set the timestamp whenever a record gets updated |
|
| 60 | - */ |
|
| 61 | - protected function DatefieldsUpdateHook() |
|
| 62 | - { |
|
| 63 | - $this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s'); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Returns the timestamp of last update for this record |
|
| 68 | - * @return DateTime |
|
| 69 | - */ |
|
| 70 | - public function getLastModifiedDate() |
|
| 71 | - { |
|
| 72 | - return new \DateTime($this->lastModified); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Returns the timestamp of when this record was created |
|
| 77 | - * @return DateTime |
|
| 78 | - */ |
|
| 79 | - public function getCreationDate() |
|
| 80 | - { |
|
| 81 | - return new \DateTime($this->created); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - |
|
| 85 | - abstract function extendTableDefinition($columnName, $definition); |
|
| 86 | - |
|
| 87 | - abstract function registerSearchHook($columnName, $fn); |
|
| 88 | - |
|
| 89 | - abstract function registerDeleteHook($columnName, $fn); |
|
| 90 | - |
|
| 91 | - abstract function registerUpdateHook($columnName, $fn); |
|
| 92 | - |
|
| 93 | - abstract function registerReadHook($columnName, $fn); |
|
| 94 | - |
|
| 95 | - abstract function registerCreateHook($columnName, $fn); |
|
| 41 | + $this->registerUpdateHook(TRAIT_DATEFIELDS_LAST_MODIFIED, 'DatefieldsUpdateHook'); |
|
| 42 | + $this->registerCreateHook(TRAIT_DATEFIELDS_LAST_MODIFIED, 'DatefieldsCreateHook'); |
|
| 43 | + |
|
| 44 | + $this->created = null; |
|
| 45 | + $this->lastModified = null; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The hook that gets called to set the timestamp whenever a new record is created |
|
| 50 | + */ |
|
| 51 | + protected function DatefieldsCreateHook() |
|
| 52 | + { |
|
| 53 | + // @TODO: Should this be split up to seperate hooks for "last_modified" and "created" for consistency? |
|
| 54 | + $this->created = (new \DateTime('now'))->format('Y-m-d H:i:s'); |
|
| 55 | + $this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s'); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * The hook that gets called to set the timestamp whenever a record gets updated |
|
| 60 | + */ |
|
| 61 | + protected function DatefieldsUpdateHook() |
|
| 62 | + { |
|
| 63 | + $this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s'); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Returns the timestamp of last update for this record |
|
| 68 | + * @return DateTime |
|
| 69 | + */ |
|
| 70 | + public function getLastModifiedDate() |
|
| 71 | + { |
|
| 72 | + return new \DateTime($this->lastModified); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Returns the timestamp of when this record was created |
|
| 77 | + * @return DateTime |
|
| 78 | + */ |
|
| 79 | + public function getCreationDate() |
|
| 80 | + { |
|
| 81 | + return new \DateTime($this->created); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + |
|
| 85 | + abstract function extendTableDefinition($columnName, $definition); |
|
| 86 | + |
|
| 87 | + abstract function registerSearchHook($columnName, $fn); |
|
| 88 | + |
|
| 89 | + abstract function registerDeleteHook($columnName, $fn); |
|
| 90 | + |
|
| 91 | + abstract function registerUpdateHook($columnName, $fn); |
|
| 92 | + |
|
| 93 | + abstract function registerReadHook($columnName, $fn); |
|
| 94 | + |
|
| 95 | + abstract function registerCreateHook($columnName, $fn); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - |
|
| 99 | 98 | \ No newline at end of file |
| 99 | + |
|
| 100 | 100 | \ No newline at end of file |
@@ -2,9 +2,8 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace miBadger\ActiveRecord\Traits; |
| 4 | 4 | |
| 5 | -use miBadger\Query\Query; |
|
| 6 | -use miBadger\ActiveRecord\ColumnProperty; |
|
| 7 | 5 | use miBadger\ActiveRecord\AbstractActiveRecord; |
| 6 | +use miBadger\ActiveRecord\ColumnProperty; |
|
| 8 | 7 | |
| 9 | 8 | Trait ManyToManyRelation |
| 10 | 9 | { |
@@ -74,6 +74,9 @@ |
||
| 74 | 74 | $this->pdo->query($rightConstraint); |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | + /** |
|
| 78 | + * @param string $columnName |
|
| 79 | + */ |
|
| 77 | 80 | abstract function extendTableDefinition($columnName, $definition); |
| 78 | 81 | |
| 79 | 82 | abstract function registerSearchHook($columnName, $fn); |
@@ -8,82 +8,82 @@ |
||
| 8 | 8 | |
| 9 | 9 | Trait ManyToManyRelation |
| 10 | 10 | { |
| 11 | - // These variables are relevant for internal bookkeeping (constraint generation etc) |
|
| 12 | - /** @var string The name of the left column of the relation. */ |
|
| 13 | - private $_leftColumnName; |
|
| 14 | - |
|
| 15 | - /** @var string The name of the right column of the relation. */ |
|
| 16 | - private $_rightColumnName; |
|
| 17 | - |
|
| 18 | - /** @var string The name of the left table of the relation. */ |
|
| 19 | - private $_leftEntityTable; |
|
| 20 | - |
|
| 21 | - /** @var string The name of the right table of the relation. */ |
|
| 22 | - private $_rightEntityTable; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Initializes the the ManyToManyRelation trait on the included object |
|
| 26 | - * |
|
| 27 | - * @param AbstractActiveRecord $leftEntity The left entity of the relation |
|
| 28 | - * @param &variable $leftVariable The variable where the id for the left entity will be stored |
|
| 29 | - * @param AbstractActiveRecord $rightEntity The left entity of the relation |
|
| 30 | - * @param &variable $leftVariable The variable where the id for the right entity will be stored |
|
| 31 | - */ |
|
| 32 | - protected function initManyToManyRelation(AbstractActiveRecord $leftEntity, &$leftVariable, AbstractActiveRecord $rightEntity, &$rightVariable) |
|
| 33 | - { |
|
| 34 | - $this->_leftEntityTable = $leftEntity->getActiveRecordTable(); |
|
| 35 | - $this->_rightEntityTable = $rightEntity->getActiveRecordTable(); |
|
| 36 | - |
|
| 37 | - if (get_class($leftEntity) === get_class($rightEntity)) { |
|
| 38 | - $this->_leftColumnName = sprintf("id_%s_left", $leftEntity->getActiveRecordTable()); |
|
| 39 | - $this->_rightColumnName = sprintf("id_%s_right", $rightEntity->getActiveRecordTable()); |
|
| 40 | - } else { |
|
| 41 | - $this->_leftColumnName = sprintf("id_%s", $leftEntity->getActiveRecordTable()); |
|
| 42 | - $this->_rightColumnName = sprintf("id_%s", $rightEntity->getActiveRecordTable()); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - $this->extendTableDefinition($this->_leftColumnName, [ |
|
| 46 | - 'value' => &$leftVariable, |
|
| 47 | - 'validate' => null, |
|
| 48 | - 'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
| 49 | - 'properties' => ColumnProperty::NOT_NULL |
|
| 50 | - ]); |
|
| 51 | - |
|
| 52 | - $this->extendTableDefinition($this->_rightColumnName, [ |
|
| 53 | - 'value' => &$rightVariable, |
|
| 54 | - 'validate' => null, |
|
| 55 | - 'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
| 56 | - 'properties' => ColumnProperty::NOT_NULL |
|
| 57 | - ]); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Build the constraints for the many-to-many relation table |
|
| 62 | - */ |
|
| 63 | - public function createTableConstraints() |
|
| 64 | - { |
|
| 65 | - $childTable = $this->getActiveRecordTable(); |
|
| 66 | - |
|
| 67 | - $leftParentTable = $this->_leftEntityTable; |
|
| 68 | - $rightParentTable = $this->_rightEntityTable; |
|
| 69 | - |
|
| 70 | - $leftConstraint = $this->buildConstraint($leftParentTable, 'id', $childTable, $this->_leftColumnName); |
|
| 71 | - $rightConstraint = $this->buildConstraint($rightParentTable, 'id', $childTable, $this->_rightColumnName); |
|
| 72 | - |
|
| 73 | - $this->pdo->query($leftConstraint); |
|
| 74 | - $this->pdo->query($rightConstraint); |
|
| 75 | - } |
|
| 11 | + // These variables are relevant for internal bookkeeping (constraint generation etc) |
|
| 12 | + /** @var string The name of the left column of the relation. */ |
|
| 13 | + private $_leftColumnName; |
|
| 14 | + |
|
| 15 | + /** @var string The name of the right column of the relation. */ |
|
| 16 | + private $_rightColumnName; |
|
| 17 | + |
|
| 18 | + /** @var string The name of the left table of the relation. */ |
|
| 19 | + private $_leftEntityTable; |
|
| 20 | + |
|
| 21 | + /** @var string The name of the right table of the relation. */ |
|
| 22 | + private $_rightEntityTable; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Initializes the the ManyToManyRelation trait on the included object |
|
| 26 | + * |
|
| 27 | + * @param AbstractActiveRecord $leftEntity The left entity of the relation |
|
| 28 | + * @param &variable $leftVariable The variable where the id for the left entity will be stored |
|
| 29 | + * @param AbstractActiveRecord $rightEntity The left entity of the relation |
|
| 30 | + * @param &variable $leftVariable The variable where the id for the right entity will be stored |
|
| 31 | + */ |
|
| 32 | + protected function initManyToManyRelation(AbstractActiveRecord $leftEntity, &$leftVariable, AbstractActiveRecord $rightEntity, &$rightVariable) |
|
| 33 | + { |
|
| 34 | + $this->_leftEntityTable = $leftEntity->getActiveRecordTable(); |
|
| 35 | + $this->_rightEntityTable = $rightEntity->getActiveRecordTable(); |
|
| 36 | + |
|
| 37 | + if (get_class($leftEntity) === get_class($rightEntity)) { |
|
| 38 | + $this->_leftColumnName = sprintf("id_%s_left", $leftEntity->getActiveRecordTable()); |
|
| 39 | + $this->_rightColumnName = sprintf("id_%s_right", $rightEntity->getActiveRecordTable()); |
|
| 40 | + } else { |
|
| 41 | + $this->_leftColumnName = sprintf("id_%s", $leftEntity->getActiveRecordTable()); |
|
| 42 | + $this->_rightColumnName = sprintf("id_%s", $rightEntity->getActiveRecordTable()); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + $this->extendTableDefinition($this->_leftColumnName, [ |
|
| 46 | + 'value' => &$leftVariable, |
|
| 47 | + 'validate' => null, |
|
| 48 | + 'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
| 49 | + 'properties' => ColumnProperty::NOT_NULL |
|
| 50 | + ]); |
|
| 51 | + |
|
| 52 | + $this->extendTableDefinition($this->_rightColumnName, [ |
|
| 53 | + 'value' => &$rightVariable, |
|
| 54 | + 'validate' => null, |
|
| 55 | + 'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
| 56 | + 'properties' => ColumnProperty::NOT_NULL |
|
| 57 | + ]); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Build the constraints for the many-to-many relation table |
|
| 62 | + */ |
|
| 63 | + public function createTableConstraints() |
|
| 64 | + { |
|
| 65 | + $childTable = $this->getActiveRecordTable(); |
|
| 66 | + |
|
| 67 | + $leftParentTable = $this->_leftEntityTable; |
|
| 68 | + $rightParentTable = $this->_rightEntityTable; |
|
| 69 | + |
|
| 70 | + $leftConstraint = $this->buildConstraint($leftParentTable, 'id', $childTable, $this->_leftColumnName); |
|
| 71 | + $rightConstraint = $this->buildConstraint($rightParentTable, 'id', $childTable, $this->_rightColumnName); |
|
| 72 | + |
|
| 73 | + $this->pdo->query($leftConstraint); |
|
| 74 | + $this->pdo->query($rightConstraint); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - abstract function extendTableDefinition($columnName, $definition); |
|
| 77 | + abstract function extendTableDefinition($columnName, $definition); |
|
| 78 | 78 | |
| 79 | - abstract function registerSearchHook($columnName, $fn); |
|
| 79 | + abstract function registerSearchHook($columnName, $fn); |
|
| 80 | 80 | |
| 81 | - abstract function registerDeleteHook($columnName, $fn); |
|
| 81 | + abstract function registerDeleteHook($columnName, $fn); |
|
| 82 | 82 | |
| 83 | - abstract function registerUpdateHook($columnName, $fn); |
|
| 83 | + abstract function registerUpdateHook($columnName, $fn); |
|
| 84 | 84 | |
| 85 | - abstract function registerReadHook($columnName, $fn); |
|
| 85 | + abstract function registerReadHook($columnName, $fn); |
|
| 86 | 86 | |
| 87 | - abstract function registerCreateHook($columnName, $fn); |
|
| 87 | + abstract function registerCreateHook($columnName, $fn); |
|
| 88 | 88 | |
| 89 | 89 | } |
@@ -2,8 +2,8 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace miBadger\ActiveRecord\Traits; |
| 4 | 4 | |
| 5 | -use miBadger\Query\Query; |
|
| 6 | 5 | use miBadger\ActiveRecord\ColumnProperty; |
| 6 | +use miBadger\Query\Query; |
|
| 7 | 7 | |
| 8 | 8 | const TRAIT_SOFT_DELETE_FIELD_KEY = "soft_delete"; |
| 9 | 9 | |
@@ -80,6 +80,9 @@ |
||
| 80 | 80 | |
| 81 | 81 | abstract function extendTableDefinition($columnName, $definition); |
| 82 | 82 | |
| 83 | + /** |
|
| 84 | + * @param string $fn |
|
| 85 | + */ |
|
| 83 | 86 | abstract function registerSearchHook($columnName, $fn); |
| 84 | 87 | |
| 85 | 88 | abstract function registerDeleteHook($columnName, $fn); |
@@ -9,84 +9,84 @@ |
||
| 9 | 9 | |
| 10 | 10 | trait SoftDelete |
| 11 | 11 | { |
| 12 | - /** @var boolean the soft delete status for the entity this trait is embedded into. */ |
|
| 13 | - protected $softDelete; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * this method is required to be called in the constructor for each class that uses this trait. |
|
| 17 | - * It adds the required fields to the table definition and registers hooks |
|
| 18 | - */ |
|
| 19 | - protected function initSoftDelete() |
|
| 20 | - { |
|
| 21 | - $this->softDelete = false; |
|
| 22 | - |
|
| 23 | - $this->extendTableDefinition(TRAIT_SOFT_DELETE_FIELD_KEY, [ |
|
| 24 | - 'value' => &$this->softDelete, |
|
| 25 | - 'validate' => null, |
|
| 26 | - 'default' => 0, |
|
| 27 | - 'type' => 'INT', |
|
| 28 | - 'length' => 1, |
|
| 29 | - 'properties' => ColumnProperty::NOT_NULL |
|
| 30 | - ]); |
|
| 31 | - |
|
| 32 | - $this->registerSearchHook(TRAIT_SOFT_DELETE_FIELD_KEY, 'softDeleteSearchHook'); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The hook that gets called whenever a query is made |
|
| 37 | - */ |
|
| 38 | - protected function softDeleteSearchHook(Query $query) |
|
| 39 | - { |
|
| 40 | - $query->where(TRAIT_SOFT_DELETE_FIELD_KEY, '=', 0); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * returns the name for the soft delete field in the database |
|
| 45 | - * @return string |
|
| 46 | - */ |
|
| 47 | - public function getSoftDeleteFieldName() |
|
| 48 | - { |
|
| 49 | - return TRAIT_SOFT_DELETE_FIELD_KEY; |
|
| 50 | - } |
|
| 12 | + /** @var boolean the soft delete status for the entity this trait is embedded into. */ |
|
| 13 | + protected $softDelete; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * this method is required to be called in the constructor for each class that uses this trait. |
|
| 17 | + * It adds the required fields to the table definition and registers hooks |
|
| 18 | + */ |
|
| 19 | + protected function initSoftDelete() |
|
| 20 | + { |
|
| 21 | + $this->softDelete = false; |
|
| 22 | + |
|
| 23 | + $this->extendTableDefinition(TRAIT_SOFT_DELETE_FIELD_KEY, [ |
|
| 24 | + 'value' => &$this->softDelete, |
|
| 25 | + 'validate' => null, |
|
| 26 | + 'default' => 0, |
|
| 27 | + 'type' => 'INT', |
|
| 28 | + 'length' => 1, |
|
| 29 | + 'properties' => ColumnProperty::NOT_NULL |
|
| 30 | + ]); |
|
| 31 | + |
|
| 32 | + $this->registerSearchHook(TRAIT_SOFT_DELETE_FIELD_KEY, 'softDeleteSearchHook'); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The hook that gets called whenever a query is made |
|
| 37 | + */ |
|
| 38 | + protected function softDeleteSearchHook(Query $query) |
|
| 39 | + { |
|
| 40 | + $query->where(TRAIT_SOFT_DELETE_FIELD_KEY, '=', 0); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * returns the name for the soft delete field in the database |
|
| 45 | + * @return string |
|
| 46 | + */ |
|
| 47 | + public function getSoftDeleteFieldName() |
|
| 48 | + { |
|
| 49 | + return TRAIT_SOFT_DELETE_FIELD_KEY; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Mark the current record as soft deleted |
|
| 54 | - * @return $this |
|
| 55 | - */ |
|
| 56 | - public function softDelete() |
|
| 57 | - { |
|
| 58 | - $this->softDelete = true; |
|
| 59 | - return $this; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Undo the current soft deletion status (mark it as non-soft deleted) |
|
| 64 | - * @return $this |
|
| 65 | - */ |
|
| 66 | - public function softRestore() |
|
| 67 | - { |
|
| 68 | - $this->softDelete = false; |
|
| 69 | - return $this; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * returns the current soft deletion status |
|
| 74 | - * @return $this |
|
| 75 | - */ |
|
| 76 | - public function getDeletionStatus() |
|
| 77 | - { |
|
| 78 | - return $this->softDelete; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - abstract function extendTableDefinition($columnName, $definition); |
|
| 82 | - |
|
| 83 | - abstract function registerSearchHook($columnName, $fn); |
|
| 84 | - |
|
| 85 | - abstract function registerDeleteHook($columnName, $fn); |
|
| 86 | - |
|
| 87 | - abstract function registerUpdateHook($columnName, $fn); |
|
| 88 | - |
|
| 89 | - abstract function registerReadHook($columnName, $fn); |
|
| 90 | - |
|
| 91 | - abstract function registerCreateHook($columnName, $fn); |
|
| 52 | + /** |
|
| 53 | + * Mark the current record as soft deleted |
|
| 54 | + * @return $this |
|
| 55 | + */ |
|
| 56 | + public function softDelete() |
|
| 57 | + { |
|
| 58 | + $this->softDelete = true; |
|
| 59 | + return $this; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Undo the current soft deletion status (mark it as non-soft deleted) |
|
| 64 | + * @return $this |
|
| 65 | + */ |
|
| 66 | + public function softRestore() |
|
| 67 | + { |
|
| 68 | + $this->softDelete = false; |
|
| 69 | + return $this; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * returns the current soft deletion status |
|
| 74 | + * @return $this |
|
| 75 | + */ |
|
| 76 | + public function getDeletionStatus() |
|
| 77 | + { |
|
| 78 | + return $this->softDelete; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + abstract function extendTableDefinition($columnName, $definition); |
|
| 82 | + |
|
| 83 | + abstract function registerSearchHook($columnName, $fn); |
|
| 84 | + |
|
| 85 | + abstract function registerDeleteHook($columnName, $fn); |
|
| 86 | + |
|
| 87 | + abstract function registerUpdateHook($columnName, $fn); |
|
| 88 | + |
|
| 89 | + abstract function registerReadHook($columnName, $fn); |
|
| 90 | + |
|
| 91 | + abstract function registerCreateHook($columnName, $fn); |
|
| 92 | 92 | } |
| 93 | 93 | \ No newline at end of file |
@@ -5,10 +5,10 @@ |
||
| 5 | 5 | |
| 6 | 6 | class ColumnProperty |
| 7 | 7 | { |
| 8 | - const NONE = 0; |
|
| 9 | - const UNIQUE = 1; |
|
| 10 | - const NOT_NULL = 2; |
|
| 11 | - const IMMUTABLE = 4; |
|
| 12 | - const AUTO_INCREMENT = 8; |
|
| 13 | - const PRIMARY_KEY = 16; |
|
| 8 | + const NONE = 0; |
|
| 9 | + const UNIQUE = 1; |
|
| 10 | + const NOT_NULL = 2; |
|
| 11 | + const IMMUTABLE = 4; |
|
| 12 | + const AUTO_INCREMENT = 8; |
|
| 13 | + const PRIMARY_KEY = 16; |
|
| 14 | 14 | } |
| 15 | 15 | \ No newline at end of file |
@@ -2,7 +2,6 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace miBadger\ActiveRecord\Traits; |
| 4 | 4 | |
| 5 | -use miBadger\ActiveRecord\ColumnProperty; |
|
| 6 | 5 | use miBadger\ActiveRecord\ActiveRecordTraitException; |
| 7 | 6 | |
| 8 | 7 | const TRAIT_PASSWORD_FIELD_PASSWORD = "password"; |
@@ -13,136 +13,136 @@ |
||
| 13 | 13 | |
| 14 | 14 | trait Password |
| 15 | 15 | { |
| 16 | - /** @var string The password hash. */ |
|
| 17 | - protected $password; |
|
| 18 | - |
|
| 19 | - /** @var string|null The password reset token. */ |
|
| 20 | - protected $passwordResetToken; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * this method is required to be called in the constructor for each class that uses this trait. |
|
| 24 | - * It adds the fields necessary for the passwords struct to the table definition |
|
| 25 | - */ |
|
| 26 | - protected function initPassword() |
|
| 27 | - { |
|
| 28 | - $this->extendTableDefinition(TRAIT_PASSWORD_FIELD_PASSWORD, [ |
|
| 29 | - 'value' => &$this->password, |
|
| 30 | - 'validate' => null, |
|
| 31 | - 'type' => 'VARCHAR', |
|
| 32 | - 'length' => 1024, |
|
| 33 | - 'properties' => null |
|
| 34 | - ]); |
|
| 35 | - |
|
| 36 | - $this->extendTableDefinition(TRAIT_PASSWORD_FIELD_PASSWORD_RESET_TOKEN, [ |
|
| 37 | - 'value' => &$this->passwordResetToken, |
|
| 38 | - 'validate' => null, |
|
| 39 | - 'default' => 0, |
|
| 40 | - 'type' => 'VARCHAR', |
|
| 41 | - 'length' => 1024 |
|
| 42 | - ]); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Returns whether the users password has been set |
|
| 48 | - * @return boolean true if the user has a password |
|
| 49 | - */ |
|
| 50 | - public function hasPasswordBeenSet() |
|
| 51 | - { |
|
| 52 | - return $this->password !== null; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Returns true if the credentials are correct. |
|
| 57 | - * |
|
| 58 | - * @param string $password |
|
| 59 | - * @return boolean true if the credentials are correct |
|
| 60 | - */ |
|
| 61 | - public function isPassword($password) |
|
| 62 | - { |
|
| 63 | - if (!$this->hasPasswordBeenSet()) |
|
| 64 | - { |
|
| 65 | - throw new ActiveRecordTraitException("Password field has not been set"); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - if (!password_verify($password, $this->password)) { |
|
| 69 | - return false; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - if (password_needs_rehash($this->password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH])) { |
|
| 73 | - $this->setPassword($password)->sync(); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - return true; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Set the password. |
|
| 81 | - * |
|
| 82 | - * @param string $password |
|
| 83 | - * @return $this |
|
| 84 | - * @throws \Exception |
|
| 85 | - */ |
|
| 86 | - public function setPassword($password) |
|
| 87 | - { |
|
| 88 | - if (strlen($password) < TRAIT_PASSWORD_MIN_LENGTH) { |
|
| 89 | - throw new ActiveRecordTraitException(sprintf('\'Password\' must be atleast %s characters long. %s characters provied.', self::PASSWORD_MIN_LENGTH, strlen($password))); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $passwordHash = \password_hash($password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH]); |
|
| 93 | - |
|
| 94 | - if ($passwordHash === false) { |
|
| 95 | - throw new ActiveRecordTraitException('\'Password\' hash failed.'); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $this->password = $passwordHash; |
|
| 99 | - |
|
| 100 | - return $this; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @return string The Hash of the password |
|
| 105 | - */ |
|
| 106 | - public function getPasswordHash() |
|
| 107 | - { |
|
| 108 | - return $this->password; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Returns the currently set password token for the entity, or null if not set |
|
| 113 | - * @return string|null The password reset token |
|
| 114 | - */ |
|
| 115 | - public function getPasswordResetToken() |
|
| 116 | - { |
|
| 117 | - return $this->passwordResetToken; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Generates a new password reset token for the user |
|
| 122 | - */ |
|
| 123 | - public function generatePasswordResetToken() |
|
| 124 | - { |
|
| 125 | - $this->passwordResetToken = md5(uniqid(mt_rand(), true)); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Clears the current password reset token |
|
| 130 | - */ |
|
| 131 | - public function clearPasswordResetToken() |
|
| 132 | - { |
|
| 133 | - $this->passwordResetToken = null; |
|
| 134 | - } |
|
| 16 | + /** @var string The password hash. */ |
|
| 17 | + protected $password; |
|
| 18 | + |
|
| 19 | + /** @var string|null The password reset token. */ |
|
| 20 | + protected $passwordResetToken; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * this method is required to be called in the constructor for each class that uses this trait. |
|
| 24 | + * It adds the fields necessary for the passwords struct to the table definition |
|
| 25 | + */ |
|
| 26 | + protected function initPassword() |
|
| 27 | + { |
|
| 28 | + $this->extendTableDefinition(TRAIT_PASSWORD_FIELD_PASSWORD, [ |
|
| 29 | + 'value' => &$this->password, |
|
| 30 | + 'validate' => null, |
|
| 31 | + 'type' => 'VARCHAR', |
|
| 32 | + 'length' => 1024, |
|
| 33 | + 'properties' => null |
|
| 34 | + ]); |
|
| 35 | + |
|
| 36 | + $this->extendTableDefinition(TRAIT_PASSWORD_FIELD_PASSWORD_RESET_TOKEN, [ |
|
| 37 | + 'value' => &$this->passwordResetToken, |
|
| 38 | + 'validate' => null, |
|
| 39 | + 'default' => 0, |
|
| 40 | + 'type' => 'VARCHAR', |
|
| 41 | + 'length' => 1024 |
|
| 42 | + ]); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Returns whether the users password has been set |
|
| 48 | + * @return boolean true if the user has a password |
|
| 49 | + */ |
|
| 50 | + public function hasPasswordBeenSet() |
|
| 51 | + { |
|
| 52 | + return $this->password !== null; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Returns true if the credentials are correct. |
|
| 57 | + * |
|
| 58 | + * @param string $password |
|
| 59 | + * @return boolean true if the credentials are correct |
|
| 60 | + */ |
|
| 61 | + public function isPassword($password) |
|
| 62 | + { |
|
| 63 | + if (!$this->hasPasswordBeenSet()) |
|
| 64 | + { |
|
| 65 | + throw new ActiveRecordTraitException("Password field has not been set"); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + if (!password_verify($password, $this->password)) { |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + if (password_needs_rehash($this->password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH])) { |
|
| 73 | + $this->setPassword($password)->sync(); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + return true; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Set the password. |
|
| 81 | + * |
|
| 82 | + * @param string $password |
|
| 83 | + * @return $this |
|
| 84 | + * @throws \Exception |
|
| 85 | + */ |
|
| 86 | + public function setPassword($password) |
|
| 87 | + { |
|
| 88 | + if (strlen($password) < TRAIT_PASSWORD_MIN_LENGTH) { |
|
| 89 | + throw new ActiveRecordTraitException(sprintf('\'Password\' must be atleast %s characters long. %s characters provied.', self::PASSWORD_MIN_LENGTH, strlen($password))); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $passwordHash = \password_hash($password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH]); |
|
| 93 | + |
|
| 94 | + if ($passwordHash === false) { |
|
| 95 | + throw new ActiveRecordTraitException('\'Password\' hash failed.'); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $this->password = $passwordHash; |
|
| 99 | + |
|
| 100 | + return $this; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @return string The Hash of the password |
|
| 105 | + */ |
|
| 106 | + public function getPasswordHash() |
|
| 107 | + { |
|
| 108 | + return $this->password; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Returns the currently set password token for the entity, or null if not set |
|
| 113 | + * @return string|null The password reset token |
|
| 114 | + */ |
|
| 115 | + public function getPasswordResetToken() |
|
| 116 | + { |
|
| 117 | + return $this->passwordResetToken; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Generates a new password reset token for the user |
|
| 122 | + */ |
|
| 123 | + public function generatePasswordResetToken() |
|
| 124 | + { |
|
| 125 | + $this->passwordResetToken = md5(uniqid(mt_rand(), true)); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Clears the current password reset token |
|
| 130 | + */ |
|
| 131 | + public function clearPasswordResetToken() |
|
| 132 | + { |
|
| 133 | + $this->passwordResetToken = null; |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - abstract function extendTableDefinition($columnName, $definition); |
|
| 136 | + abstract function extendTableDefinition($columnName, $definition); |
|
| 137 | 137 | |
| 138 | - abstract function registerSearchHook($columnName, $fn); |
|
| 138 | + abstract function registerSearchHook($columnName, $fn); |
|
| 139 | 139 | |
| 140 | - abstract function registerDeleteHook($columnName, $fn); |
|
| 140 | + abstract function registerDeleteHook($columnName, $fn); |
|
| 141 | 141 | |
| 142 | - abstract function registerUpdateHook($columnName, $fn); |
|
| 142 | + abstract function registerUpdateHook($columnName, $fn); |
|
| 143 | 143 | |
| 144 | - abstract function registerReadHook($columnName, $fn); |
|
| 144 | + abstract function registerReadHook($columnName, $fn); |
|
| 145 | 145 | |
| 146 | - abstract function registerCreateHook($columnName, $fn); |
|
| 146 | + abstract function registerCreateHook($columnName, $fn); |
|
| 147 | 147 | |
| 148 | 148 | } |
| 149 | 149 | \ No newline at end of file |
@@ -2,8 +2,8 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace miBadger\ActiveRecord\Traits; |
| 4 | 4 | |
| 5 | -use miBadger\ActiveRecord\ColumnProperty; |
|
| 6 | 5 | use miBadger\ActiveRecord\ActiveRecordException; |
| 6 | +use miBadger\ActiveRecord\ColumnProperty; |
|
| 7 | 7 | use miBadger\Query\Query; |
| 8 | 8 | |
| 9 | 9 | trait AutoApi |
@@ -44,6 +44,8 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | 46 | * Copy all table variables between two instances |
| 47 | + * @param AutoApi $to |
|
| 48 | + * @param AutoApi $from |
|
| 47 | 49 | */ |
| 48 | 50 | private function syncInstances($to, $from) |
| 49 | 51 | { |
@@ -272,10 +274,19 @@ discard block |
||
| 272 | 274 | |
| 273 | 275 | abstract public function read($id); |
| 274 | 276 | |
| 277 | + /** |
|
| 278 | + * @return \PDO |
|
| 279 | + */ |
|
| 275 | 280 | abstract public function getPdo(); |
| 276 | 281 | |
| 282 | + /** |
|
| 283 | + * @param integer $id |
|
| 284 | + */ |
|
| 277 | 285 | abstract function setId($id); |
| 278 | 286 | |
| 287 | + /** |
|
| 288 | + * @return string |
|
| 289 | + */ |
|
| 279 | 290 | abstract function getActiveRecordTable(); |
| 280 | 291 | |
| 281 | 292 | abstract function getActiveRecordColumns(); |
@@ -8,275 +8,275 @@ |
||
| 8 | 8 | |
| 9 | 9 | trait AutoApi |
| 10 | 10 | { |
| 11 | - /* ======================================================================= |
|
| 11 | + /* ======================================================================= |
|
| 12 | 12 | * ===================== Automatic API Support =========================== |
| 13 | 13 | * ======================================================================= */ |
| 14 | 14 | |
| 15 | - public function apiSearch($inputs, $fieldWhitelist) |
|
| 16 | - { |
|
| 17 | - // @TODO: How to handle this case? |
|
| 18 | - // => Default parameter names for searching? (limit, pagination, sort order etc) |
|
| 19 | - // Find default names for this and store in class |
|
| 20 | - // => Limited search parameters? (We don't want to be able to search on a password field for example) |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - public function toArray($fieldWhitelist) |
|
| 24 | - { |
|
| 25 | - $output = []; |
|
| 26 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 27 | - if (in_array($colName, $fieldWhitelist)) { |
|
| 28 | - $output[$colName] = $definition['value']; |
|
| 29 | - } |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - return $output; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - public function apiRead($id, $fieldWhitelist) |
|
| 36 | - { |
|
| 37 | - $this->read($id); |
|
| 38 | - return $this->toArray($fieldWhitelist); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /* ============================================================= |
|
| 15 | + public function apiSearch($inputs, $fieldWhitelist) |
|
| 16 | + { |
|
| 17 | + // @TODO: How to handle this case? |
|
| 18 | + // => Default parameter names for searching? (limit, pagination, sort order etc) |
|
| 19 | + // Find default names for this and store in class |
|
| 20 | + // => Limited search parameters? (We don't want to be able to search on a password field for example) |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + public function toArray($fieldWhitelist) |
|
| 24 | + { |
|
| 25 | + $output = []; |
|
| 26 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 27 | + if (in_array($colName, $fieldWhitelist)) { |
|
| 28 | + $output[$colName] = $definition['value']; |
|
| 29 | + } |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + return $output; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + public function apiRead($id, $fieldWhitelist) |
|
| 36 | + { |
|
| 37 | + $this->read($id); |
|
| 38 | + return $this->toArray($fieldWhitelist); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /* ============================================================= |
|
| 42 | 42 | * ===================== Constraint validation ================= |
| 43 | 43 | * ============================================================= */ |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Copy all table variables between two instances |
|
| 47 | - */ |
|
| 48 | - private function syncInstances($to, $from) |
|
| 49 | - { |
|
| 50 | - foreach ($to->tableDefinition as $colName => $definition) { |
|
| 51 | - $definition['value'] = $from->tableDefinition[$colName]['value']; |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - private function filterInputColumns($input, $whitelist) |
|
| 56 | - { |
|
| 57 | - $filteredInput = $input; |
|
| 58 | - foreach ($input as $colName => $value) { |
|
| 59 | - if (!in_array($colName, $whitelist)) { |
|
| 60 | - unset($filteredInput[$colName]); |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - return $filteredInput; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - private function validateExcessKeys($input) |
|
| 67 | - { |
|
| 68 | - $errors = []; |
|
| 69 | - foreach ($input as $colName => $value) { |
|
| 70 | - if (!array_key_exists($colName, $this->tableDefinition)) { |
|
| 71 | - $errors[$colName] = "Unknown input field"; |
|
| 72 | - continue; |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - return $errors; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - private function validateImmutableColumns($input) |
|
| 79 | - { |
|
| 80 | - $errors = []; |
|
| 81 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 82 | - $property = $definition['properties'] ?? null; |
|
| 83 | - if (array_key_exists($colName, $input) |
|
| 84 | - && $property & ColumnProperty::IMMUTABLE) { |
|
| 85 | - $errors[$colName] = "Field cannot be changed"; |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - return $errors; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - private function validateInputValues($input) |
|
| 92 | - { |
|
| 93 | - $errors = []; |
|
| 94 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 95 | - // Validation check 1: If validate function is present |
|
| 96 | - if (array_key_exists($colName, $input) |
|
| 97 | - && is_callable($definition['validate'] ?? null)) { |
|
| 98 | - $inputValue = $input[$colName]; |
|
| 99 | - |
|
| 100 | - // If validation function fails |
|
| 101 | - [$status, $message] = $definition['validate']($inputValue); |
|
| 102 | - if (!$status) { |
|
| 103 | - $errors[$colName] = $message; |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // Validation check 2: If relation column, check whether entity exists |
|
| 108 | - $properties = $definition['properties'] ?? null; |
|
| 109 | - if (isset($definition['relation']) |
|
| 110 | - && ($properties & ColumnProperty::NOT_NULL)) { |
|
| 111 | - $instance = clone $definition['relation']; |
|
| 112 | - try { |
|
| 113 | - $instance->read($input[$colName] ?? null); |
|
| 114 | - } catch (ActiveRecordException $e) { |
|
| 115 | - $errors[$colName] = "Entity for this value doesn't exist"; |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - return $errors; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * This function is only used for API Update calls (direct getter/setter functions are unconstrained) |
|
| 124 | - */ |
|
| 125 | - private function validateMissingKeys() |
|
| 126 | - { |
|
| 127 | - $errors = []; |
|
| 128 | - |
|
| 129 | - foreach ($this->tableDefinition as $colName => $colDefinition) { |
|
| 130 | - $default = $colDefinition['default'] ?? null; |
|
| 131 | - $properties = $colDefinition['properties'] ?? null; |
|
| 132 | - $value = $colDefinition['value']; |
|
| 133 | - |
|
| 134 | - // If nullable and default not set => null |
|
| 135 | - // If nullable and default null => default (null) |
|
| 136 | - // If nullable and default set => default (value) |
|
| 137 | - |
|
| 138 | - // if not nullable and default not set => error |
|
| 139 | - // if not nullable and default null => error |
|
| 140 | - // if not nullable and default st => default (value) |
|
| 141 | - // => if not nullable and default null and value not set => error message in this method |
|
| 142 | - if ($properties & ColumnProperty::NOT_NULL |
|
| 143 | - && $default === null |
|
| 144 | - && !($properties & ColumnProperty::AUTO_INCREMENT) |
|
| 145 | - // && !array_key_exists($colName, $input) |
|
| 146 | - && $value === null) { |
|
| 147 | - $errors[$colName] = sprintf("The required field \"%s\" is missing", $colName); |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return $errors; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Copies the values for entries in the input with matching variable names in the record definition |
|
| 156 | - * @param Array $input The input data to be loaded into $this record |
|
| 157 | - */ |
|
| 158 | - private function loadData($input) |
|
| 159 | - { |
|
| 160 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 161 | - if (array_key_exists($colName, $input)) { |
|
| 162 | - $definition['value'] = $input[$colName]; |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @param Array $input Associative array of input values |
|
| 169 | - * @param Array $fieldWhitelist array of column names that are allowed to be filled by the input array |
|
| 170 | - * @return Array Array containing the set of optional errors (associative array) and an optional array representation (associative) |
|
| 171 | - * of the modified data. |
|
| 172 | - */ |
|
| 173 | - public function apiCreate($input, $fieldWhitelist) |
|
| 174 | - { |
|
| 175 | - // Clone $this to new instance (for restoring if validation goes wrong) |
|
| 176 | - $transaction = clone $this; |
|
| 177 | - $errors = []; |
|
| 178 | - |
|
| 179 | - // Filter out all non-whitelisted input values |
|
| 180 | - $input = $this->filterInputColumns($input, $fieldWhitelist); |
|
| 181 | - |
|
| 182 | - // Validate excess keys |
|
| 183 | - $errors += $transaction->validateExcessKeys($input); |
|
| 184 | - |
|
| 185 | - // Validate input values (using validation function) |
|
| 186 | - $errors += $transaction->validateInputValues($input); |
|
| 187 | - |
|
| 188 | - // "Copy" data into transaction |
|
| 189 | - $transaction->loadData($input); |
|
| 190 | - |
|
| 191 | - // Run create hooks |
|
| 192 | - foreach ($this->registeredCreateHooks as $colName => $fn) { |
|
| 193 | - $fn(); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - // Validate missing keys |
|
| 197 | - $errors += $transaction->validateMissingKeys(); |
|
| 198 | - |
|
| 199 | - // If no errors, commit the pending data |
|
| 200 | - if (empty($errors)) { |
|
| 201 | - $this->syncInstances($this, $transaction); |
|
| 202 | - |
|
| 203 | - try { |
|
| 204 | - $q = (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
| 205 | - ->insert($this->getActiveRecordColumns()) |
|
| 206 | - ->execute(); |
|
| 207 | - |
|
| 208 | - $this->setId(intval($this->getPdo()->lastInsertId())); |
|
| 209 | - } catch (\PDOException $e) { |
|
| 210 | - // @TODO: Potentially filter and store mysql messages (where possible) in error messages |
|
| 211 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - return [null, $this->toArray($fieldWhitelist)]; |
|
| 215 | - } else { |
|
| 216 | - return [$errors, null]; |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * @param Array $input Associative array of input values |
|
| 222 | - * @param Array $fieldWhitelist array of column names that are allowed to be filled by the input array |
|
| 223 | - * @return Array Array containing the set of optional errors (associative array) and an optional array representation (associative) |
|
| 224 | - * of the modified data. |
|
| 225 | - */ |
|
| 226 | - public function apiUpdate($input, $fieldWhitelist) |
|
| 227 | - { |
|
| 228 | - $transaction = clone $this; |
|
| 229 | - $errors = []; |
|
| 230 | - |
|
| 231 | - // Filter out all non-whitelisted input values |
|
| 232 | - $input = $this->filterInputColumns($input, $fieldWhitelist); |
|
| 233 | - |
|
| 234 | - // Check for excess keys |
|
| 235 | - $errors += $transaction->validateExcessKeys($input); |
|
| 236 | - |
|
| 237 | - // Check for immutable keys |
|
| 238 | - $errors += $transaction->validateImmutableColumns($input); |
|
| 239 | - |
|
| 240 | - // Validate input values (using validation function) |
|
| 241 | - $errors += $transaction->validateInputValues($input); |
|
| 242 | - |
|
| 243 | - // "Copy" data into transaction |
|
| 244 | - $transaction->loadData($input); |
|
| 245 | - |
|
| 246 | - // Run create hooks |
|
| 247 | - foreach ($this->registeredUpdateHooks as $colName => $fn) { |
|
| 248 | - $fn(); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - // Validate missing keys |
|
| 252 | - $errors += $transaction->validateMissingKeys(); |
|
| 253 | - |
|
| 254 | - // Update database |
|
| 255 | - if (empty($errors)) { |
|
| 256 | - $this->syncInstances($this, $transaction); |
|
| 257 | - |
|
| 258 | - try { |
|
| 259 | - (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
| 260 | - ->update($this->getActiveRecordColumns()) |
|
| 261 | - ->where('id', '=', $this->getId()) |
|
| 262 | - ->execute(); |
|
| 263 | - } catch (\PDOException $e) { |
|
| 264 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - return [null, $this->toArray($fieldWhitelist)]; |
|
| 268 | - } else { |
|
| 269 | - return [$errors, null]; |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - abstract public function read($id); |
|
| 274 | - |
|
| 275 | - abstract public function getPdo(); |
|
| 45 | + /** |
|
| 46 | + * Copy all table variables between two instances |
|
| 47 | + */ |
|
| 48 | + private function syncInstances($to, $from) |
|
| 49 | + { |
|
| 50 | + foreach ($to->tableDefinition as $colName => $definition) { |
|
| 51 | + $definition['value'] = $from->tableDefinition[$colName]['value']; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + private function filterInputColumns($input, $whitelist) |
|
| 56 | + { |
|
| 57 | + $filteredInput = $input; |
|
| 58 | + foreach ($input as $colName => $value) { |
|
| 59 | + if (!in_array($colName, $whitelist)) { |
|
| 60 | + unset($filteredInput[$colName]); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + return $filteredInput; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + private function validateExcessKeys($input) |
|
| 67 | + { |
|
| 68 | + $errors = []; |
|
| 69 | + foreach ($input as $colName => $value) { |
|
| 70 | + if (!array_key_exists($colName, $this->tableDefinition)) { |
|
| 71 | + $errors[$colName] = "Unknown input field"; |
|
| 72 | + continue; |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + return $errors; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + private function validateImmutableColumns($input) |
|
| 79 | + { |
|
| 80 | + $errors = []; |
|
| 81 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 82 | + $property = $definition['properties'] ?? null; |
|
| 83 | + if (array_key_exists($colName, $input) |
|
| 84 | + && $property & ColumnProperty::IMMUTABLE) { |
|
| 85 | + $errors[$colName] = "Field cannot be changed"; |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + return $errors; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + private function validateInputValues($input) |
|
| 92 | + { |
|
| 93 | + $errors = []; |
|
| 94 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 95 | + // Validation check 1: If validate function is present |
|
| 96 | + if (array_key_exists($colName, $input) |
|
| 97 | + && is_callable($definition['validate'] ?? null)) { |
|
| 98 | + $inputValue = $input[$colName]; |
|
| 99 | + |
|
| 100 | + // If validation function fails |
|
| 101 | + [$status, $message] = $definition['validate']($inputValue); |
|
| 102 | + if (!$status) { |
|
| 103 | + $errors[$colName] = $message; |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // Validation check 2: If relation column, check whether entity exists |
|
| 108 | + $properties = $definition['properties'] ?? null; |
|
| 109 | + if (isset($definition['relation']) |
|
| 110 | + && ($properties & ColumnProperty::NOT_NULL)) { |
|
| 111 | + $instance = clone $definition['relation']; |
|
| 112 | + try { |
|
| 113 | + $instance->read($input[$colName] ?? null); |
|
| 114 | + } catch (ActiveRecordException $e) { |
|
| 115 | + $errors[$colName] = "Entity for this value doesn't exist"; |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + return $errors; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * This function is only used for API Update calls (direct getter/setter functions are unconstrained) |
|
| 124 | + */ |
|
| 125 | + private function validateMissingKeys() |
|
| 126 | + { |
|
| 127 | + $errors = []; |
|
| 128 | + |
|
| 129 | + foreach ($this->tableDefinition as $colName => $colDefinition) { |
|
| 130 | + $default = $colDefinition['default'] ?? null; |
|
| 131 | + $properties = $colDefinition['properties'] ?? null; |
|
| 132 | + $value = $colDefinition['value']; |
|
| 133 | + |
|
| 134 | + // If nullable and default not set => null |
|
| 135 | + // If nullable and default null => default (null) |
|
| 136 | + // If nullable and default set => default (value) |
|
| 137 | + |
|
| 138 | + // if not nullable and default not set => error |
|
| 139 | + // if not nullable and default null => error |
|
| 140 | + // if not nullable and default st => default (value) |
|
| 141 | + // => if not nullable and default null and value not set => error message in this method |
|
| 142 | + if ($properties & ColumnProperty::NOT_NULL |
|
| 143 | + && $default === null |
|
| 144 | + && !($properties & ColumnProperty::AUTO_INCREMENT) |
|
| 145 | + // && !array_key_exists($colName, $input) |
|
| 146 | + && $value === null) { |
|
| 147 | + $errors[$colName] = sprintf("The required field \"%s\" is missing", $colName); |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return $errors; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Copies the values for entries in the input with matching variable names in the record definition |
|
| 156 | + * @param Array $input The input data to be loaded into $this record |
|
| 157 | + */ |
|
| 158 | + private function loadData($input) |
|
| 159 | + { |
|
| 160 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 161 | + if (array_key_exists($colName, $input)) { |
|
| 162 | + $definition['value'] = $input[$colName]; |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @param Array $input Associative array of input values |
|
| 169 | + * @param Array $fieldWhitelist array of column names that are allowed to be filled by the input array |
|
| 170 | + * @return Array Array containing the set of optional errors (associative array) and an optional array representation (associative) |
|
| 171 | + * of the modified data. |
|
| 172 | + */ |
|
| 173 | + public function apiCreate($input, $fieldWhitelist) |
|
| 174 | + { |
|
| 175 | + // Clone $this to new instance (for restoring if validation goes wrong) |
|
| 176 | + $transaction = clone $this; |
|
| 177 | + $errors = []; |
|
| 178 | + |
|
| 179 | + // Filter out all non-whitelisted input values |
|
| 180 | + $input = $this->filterInputColumns($input, $fieldWhitelist); |
|
| 181 | + |
|
| 182 | + // Validate excess keys |
|
| 183 | + $errors += $transaction->validateExcessKeys($input); |
|
| 184 | + |
|
| 185 | + // Validate input values (using validation function) |
|
| 186 | + $errors += $transaction->validateInputValues($input); |
|
| 187 | + |
|
| 188 | + // "Copy" data into transaction |
|
| 189 | + $transaction->loadData($input); |
|
| 190 | + |
|
| 191 | + // Run create hooks |
|
| 192 | + foreach ($this->registeredCreateHooks as $colName => $fn) { |
|
| 193 | + $fn(); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + // Validate missing keys |
|
| 197 | + $errors += $transaction->validateMissingKeys(); |
|
| 198 | + |
|
| 199 | + // If no errors, commit the pending data |
|
| 200 | + if (empty($errors)) { |
|
| 201 | + $this->syncInstances($this, $transaction); |
|
| 202 | + |
|
| 203 | + try { |
|
| 204 | + $q = (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
| 205 | + ->insert($this->getActiveRecordColumns()) |
|
| 206 | + ->execute(); |
|
| 207 | + |
|
| 208 | + $this->setId(intval($this->getPdo()->lastInsertId())); |
|
| 209 | + } catch (\PDOException $e) { |
|
| 210 | + // @TODO: Potentially filter and store mysql messages (where possible) in error messages |
|
| 211 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + return [null, $this->toArray($fieldWhitelist)]; |
|
| 215 | + } else { |
|
| 216 | + return [$errors, null]; |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * @param Array $input Associative array of input values |
|
| 222 | + * @param Array $fieldWhitelist array of column names that are allowed to be filled by the input array |
|
| 223 | + * @return Array Array containing the set of optional errors (associative array) and an optional array representation (associative) |
|
| 224 | + * of the modified data. |
|
| 225 | + */ |
|
| 226 | + public function apiUpdate($input, $fieldWhitelist) |
|
| 227 | + { |
|
| 228 | + $transaction = clone $this; |
|
| 229 | + $errors = []; |
|
| 230 | + |
|
| 231 | + // Filter out all non-whitelisted input values |
|
| 232 | + $input = $this->filterInputColumns($input, $fieldWhitelist); |
|
| 233 | + |
|
| 234 | + // Check for excess keys |
|
| 235 | + $errors += $transaction->validateExcessKeys($input); |
|
| 236 | + |
|
| 237 | + // Check for immutable keys |
|
| 238 | + $errors += $transaction->validateImmutableColumns($input); |
|
| 239 | + |
|
| 240 | + // Validate input values (using validation function) |
|
| 241 | + $errors += $transaction->validateInputValues($input); |
|
| 242 | + |
|
| 243 | + // "Copy" data into transaction |
|
| 244 | + $transaction->loadData($input); |
|
| 245 | + |
|
| 246 | + // Run create hooks |
|
| 247 | + foreach ($this->registeredUpdateHooks as $colName => $fn) { |
|
| 248 | + $fn(); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + // Validate missing keys |
|
| 252 | + $errors += $transaction->validateMissingKeys(); |
|
| 253 | + |
|
| 254 | + // Update database |
|
| 255 | + if (empty($errors)) { |
|
| 256 | + $this->syncInstances($this, $transaction); |
|
| 257 | + |
|
| 258 | + try { |
|
| 259 | + (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
| 260 | + ->update($this->getActiveRecordColumns()) |
|
| 261 | + ->where('id', '=', $this->getId()) |
|
| 262 | + ->execute(); |
|
| 263 | + } catch (\PDOException $e) { |
|
| 264 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + return [null, $this->toArray($fieldWhitelist)]; |
|
| 268 | + } else { |
|
| 269 | + return [$errors, null]; |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + abstract public function read($id); |
|
| 274 | + |
|
| 275 | + abstract public function getPdo(); |
|
| 276 | 276 | |
| 277 | - abstract function setId($id); |
|
| 277 | + abstract function setId($id); |
|
| 278 | 278 | |
| 279 | - abstract function getActiveRecordTable(); |
|
| 279 | + abstract function getActiveRecordTable(); |
|
| 280 | 280 | |
| 281 | - abstract function getActiveRecordColumns(); |
|
| 281 | + abstract function getActiveRecordColumns(); |
|
| 282 | 282 | } |
@@ -6,5 +6,5 @@ |
||
| 6 | 6 | |
| 7 | 7 | trait Pagination |
| 8 | 8 | { |
| 9 | - // @TODO: This one would most likely only be relevant for api requests (and perhaps supply a public method for searching with a page) |
|
| 9 | + // @TODO: This one would most likely only be relevant for api requests (and perhaps supply a public method for searching with a page) |
|
| 10 | 10 | } |
| 11 | 11 | \ No newline at end of file |