@@ -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,126 +11,126 @@ |
||
| 11 | 11 | |
| 12 | 12 | trait Address |
| 13 | 13 | { |
| 14 | - /** @var string the address line */ |
|
| 15 | - protected $address; |
|
| 16 | - |
|
| 17 | - /** @var string the zipcode */ |
|
| 18 | - protected $zipcode; |
|
| 19 | - |
|
| 20 | - /** @var string the city */ |
|
| 21 | - protected $city; |
|
| 22 | - |
|
| 23 | - /** @var string the country */ |
|
| 24 | - protected $country; |
|
| 25 | - |
|
| 26 | - protected function initAddress() |
|
| 27 | - { |
|
| 28 | - $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ADDRESS, [ |
|
| 29 | - 'value' => &$this->address, |
|
| 30 | - 'validate' => null, |
|
| 31 | - 'type' => 'VARCHAR', |
|
| 32 | - 'length' => 1024, |
|
| 33 | - 'properties' => null |
|
| 34 | - ]); |
|
| 35 | - |
|
| 36 | - $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ZIPCODE, [ |
|
| 37 | - 'value' => &$this->zipcode, |
|
| 38 | - 'validate' => null, |
|
| 39 | - 'type' => 'VARCHAR', |
|
| 40 | - 'length' => 1024, |
|
| 41 | - 'properties' => null |
|
| 42 | - ]); |
|
| 43 | - |
|
| 44 | - $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_CITY, [ |
|
| 45 | - 'value' => &$this->city, |
|
| 46 | - 'validate' => null, |
|
| 47 | - 'type' => 'VARCHAR', |
|
| 48 | - 'length' => 1024, |
|
| 49 | - 'properties' => null |
|
| 50 | - ]); |
|
| 51 | - |
|
| 52 | - $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_COUNTRY, [ |
|
| 53 | - 'value' => &$this->country, |
|
| 54 | - 'validate' => null, |
|
| 55 | - 'type' => 'VARCHAR', |
|
| 56 | - 'length' => 1024, |
|
| 57 | - 'properties' => null |
|
| 58 | - ]); |
|
| 59 | - |
|
| 60 | - $this->address = null; |
|
| 61 | - $this->zipcode = null; |
|
| 62 | - $this->city = null; |
|
| 63 | - $this->country = null; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - public function getAddress() |
|
| 67 | - { |
|
| 68 | - return $this->address; |
|
| 69 | - } |
|
| 14 | + /** @var string the address line */ |
|
| 15 | + protected $address; |
|
| 16 | + |
|
| 17 | + /** @var string the zipcode */ |
|
| 18 | + protected $zipcode; |
|
| 19 | + |
|
| 20 | + /** @var string the city */ |
|
| 21 | + protected $city; |
|
| 22 | + |
|
| 23 | + /** @var string the country */ |
|
| 24 | + protected $country; |
|
| 25 | + |
|
| 26 | + protected function initAddress() |
|
| 27 | + { |
|
| 28 | + $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ADDRESS, [ |
|
| 29 | + 'value' => &$this->address, |
|
| 30 | + 'validate' => null, |
|
| 31 | + 'type' => 'VARCHAR', |
|
| 32 | + 'length' => 1024, |
|
| 33 | + 'properties' => null |
|
| 34 | + ]); |
|
| 35 | + |
|
| 36 | + $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ZIPCODE, [ |
|
| 37 | + 'value' => &$this->zipcode, |
|
| 38 | + 'validate' => null, |
|
| 39 | + 'type' => 'VARCHAR', |
|
| 40 | + 'length' => 1024, |
|
| 41 | + 'properties' => null |
|
| 42 | + ]); |
|
| 43 | + |
|
| 44 | + $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_CITY, [ |
|
| 45 | + 'value' => &$this->city, |
|
| 46 | + 'validate' => null, |
|
| 47 | + 'type' => 'VARCHAR', |
|
| 48 | + 'length' => 1024, |
|
| 49 | + 'properties' => null |
|
| 50 | + ]); |
|
| 51 | + |
|
| 52 | + $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_COUNTRY, [ |
|
| 53 | + 'value' => &$this->country, |
|
| 54 | + 'validate' => null, |
|
| 55 | + 'type' => 'VARCHAR', |
|
| 56 | + 'length' => 1024, |
|
| 57 | + 'properties' => null |
|
| 58 | + ]); |
|
| 59 | + |
|
| 60 | + $this->address = null; |
|
| 61 | + $this->zipcode = null; |
|
| 62 | + $this->city = null; |
|
| 63 | + $this->country = null; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + public function getAddress() |
|
| 67 | + { |
|
| 68 | + return $this->address; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - public function setAddress($address) |
|
| 72 | - { |
|
| 73 | - $this->address = $address; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - public function getZipcode() |
|
| 77 | - { |
|
| 78 | - return $this->zipcode; |
|
| 79 | - } |
|
| 71 | + public function setAddress($address) |
|
| 72 | + { |
|
| 73 | + $this->address = $address; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + public function getZipcode() |
|
| 77 | + { |
|
| 78 | + return $this->zipcode; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - public function setZipcode($zipcode) |
|
| 82 | - { |
|
| 83 | - $this->zipcode = $zipcode; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - public function getCity() |
|
| 87 | - { |
|
| 88 | - return $this->city; |
|
| 89 | - } |
|
| 81 | + public function setZipcode($zipcode) |
|
| 82 | + { |
|
| 83 | + $this->zipcode = $zipcode; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + public function getCity() |
|
| 87 | + { |
|
| 88 | + return $this->city; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - public function setCity($city) |
|
| 92 | - { |
|
| 93 | - $this->city = $city; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - public function getCountry() |
|
| 97 | - { |
|
| 98 | - return $this->country; |
|
| 99 | - } |
|
| 91 | + public function setCity($city) |
|
| 92 | + { |
|
| 93 | + $this->city = $city; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + public function getCountry() |
|
| 97 | + { |
|
| 98 | + return $this->country; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - public function setCountry($country) |
|
| 102 | - { |
|
| 103 | - $this->country = $country; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @return void |
|
| 108 | - */ |
|
| 109 | - abstract function extendTableDefinition($columnName, $definition); |
|
| 101 | + public function setCountry($country) |
|
| 102 | + { |
|
| 103 | + $this->country = $country; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @return void |
|
| 108 | + */ |
|
| 109 | + abstract function extendTableDefinition($columnName, $definition); |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * @return void |
|
| 113 | - */ |
|
| 114 | - abstract function registerSearchHook($columnName, $fn); |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @return void |
|
| 118 | - */ |
|
| 119 | - abstract function registerDeleteHook($columnName, $fn); |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @return void |
|
| 123 | - */ |
|
| 124 | - abstract function registerUpdateHook($columnName, $fn); |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @return void |
|
| 128 | - */ |
|
| 129 | - abstract function registerReadHook($columnName, $fn); |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @return void |
|
| 133 | - */ |
|
| 134 | - abstract function registerCreateHook($columnName, $fn); |
|
| 111 | + /** |
|
| 112 | + * @return void |
|
| 113 | + */ |
|
| 114 | + abstract function registerSearchHook($columnName, $fn); |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @return void |
|
| 118 | + */ |
|
| 119 | + abstract function registerDeleteHook($columnName, $fn); |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @return void |
|
| 123 | + */ |
|
| 124 | + abstract function registerUpdateHook($columnName, $fn); |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @return void |
|
| 128 | + */ |
|
| 129 | + abstract function registerReadHook($columnName, $fn); |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @return void |
|
| 133 | + */ |
|
| 134 | + abstract function registerCreateHook($columnName, $fn); |
|
| 135 | 135 | |
| 136 | 136 | } |
| 137 | 137 | \ 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 | { |
@@ -97,6 +97,7 @@ discard block |
||
| 97 | 97 | abstract function registerDeleteHook($columnName, $fn); |
| 98 | 98 | |
| 99 | 99 | /** |
| 100 | + * @param string $fn |
|
| 100 | 101 | * @return void |
| 101 | 102 | */ |
| 102 | 103 | abstract function registerUpdateHook($columnName, $fn); |
@@ -107,6 +108,7 @@ discard block |
||
| 107 | 108 | abstract function registerReadHook($columnName, $fn); |
| 108 | 109 | |
| 109 | 110 | /** |
| 111 | + * @param string $fn |
|
| 110 | 112 | * @return void |
| 111 | 113 | */ |
| 112 | 114 | abstract function registerCreateHook($columnName, $fn); |
@@ -10,106 +10,106 @@ |
||
| 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 | - * @return void |
|
| 86 | - */ |
|
| 87 | - abstract function extendTableDefinition($columnName, $definition); |
|
| 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 | + * @return void |
|
| 86 | + */ |
|
| 87 | + abstract function extendTableDefinition($columnName, $definition); |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * @return void |
|
| 91 | - */ |
|
| 92 | - abstract function registerSearchHook($columnName, $fn); |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @return void |
|
| 96 | - */ |
|
| 97 | - abstract function registerDeleteHook($columnName, $fn); |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @return void |
|
| 101 | - */ |
|
| 102 | - abstract function registerUpdateHook($columnName, $fn); |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @return void |
|
| 106 | - */ |
|
| 107 | - abstract function registerReadHook($columnName, $fn); |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @return void |
|
| 111 | - */ |
|
| 112 | - abstract function registerCreateHook($columnName, $fn); |
|
| 89 | + /** |
|
| 90 | + * @return void |
|
| 91 | + */ |
|
| 92 | + abstract function registerSearchHook($columnName, $fn); |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @return void |
|
| 96 | + */ |
|
| 97 | + abstract function registerDeleteHook($columnName, $fn); |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @return void |
|
| 101 | + */ |
|
| 102 | + abstract function registerUpdateHook($columnName, $fn); |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @return void |
|
| 106 | + */ |
|
| 107 | + abstract function registerReadHook($columnName, $fn); |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @return void |
|
| 111 | + */ |
|
| 112 | + abstract function registerCreateHook($columnName, $fn); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - |
|
| 116 | 115 | \ No newline at end of file |
| 116 | + |
|
| 117 | 117 | \ 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 | { |
@@ -85,11 +85,13 @@ |
||
| 85 | 85 | abstract function getActiveRecordTable(); |
| 86 | 86 | |
| 87 | 87 | /** |
| 88 | + * @param string $parentColumn |
|
| 88 | 89 | * @return void |
| 89 | 90 | */ |
| 90 | 91 | abstract function buildConstraint($parentTable, $parentColumn, $childTable, $childColumn); |
| 91 | 92 | |
| 92 | 93 | /** |
| 94 | + * @param string $columnName |
|
| 93 | 95 | * @return void |
| 94 | 96 | */ |
| 95 | 97 | abstract function extendTableDefinition($columnName, $definition); |
@@ -8,115 +8,115 @@ |
||
| 8 | 8 | |
| 9 | 9 | Trait ManyToManyRelation |
| 10 | 10 | { |
| 11 | - // These variables are relevant for internal bookkeeping (constraint generation etc) |
|
| 12 | - |
|
| 13 | - /** @var string The name of the left column of the relation. */ |
|
| 14 | - private $_leftColumnName; |
|
| 15 | - |
|
| 16 | - /** @var string The name of the right column of the relation. */ |
|
| 17 | - private $_rightColumnName; |
|
| 18 | - |
|
| 19 | - /** @var string The name of the left table of the relation. */ |
|
| 20 | - private $_leftEntityTable; |
|
| 21 | - |
|
| 22 | - /** @var string The name of the right table of the relation. */ |
|
| 23 | - private $_rightEntityTable; |
|
| 24 | - |
|
| 25 | - /** @var \PDO The PDO object. */ |
|
| 26 | - protected $pdo; |
|
| 27 | - /** |
|
| 28 | - * Initializes the the ManyToManyRelation trait on the included object |
|
| 29 | - * |
|
| 30 | - * @param AbstractActiveRecord $leftEntity The left entity of the relation |
|
| 31 | - * @param &variable $leftVariable The variable where the id for the left entity will be stored |
|
| 32 | - * @param AbstractActiveRecord $rightEntity The left entity of the relation |
|
| 33 | - * @param &variable $leftVariable The variable where the id for the right entity will be stored |
|
| 34 | - * @return void |
|
| 35 | - */ |
|
| 36 | - protected function initManyToManyRelation(AbstractActiveRecord $leftEntity, &$leftVariable, AbstractActiveRecord $rightEntity, &$rightVariable) |
|
| 37 | - { |
|
| 38 | - $this->_leftEntityTable = $leftEntity->getActiveRecordTable(); |
|
| 39 | - $this->_rightEntityTable = $rightEntity->getActiveRecordTable(); |
|
| 40 | - |
|
| 41 | - if (get_class($leftEntity) === get_class($rightEntity)) { |
|
| 42 | - $this->_leftColumnName = sprintf("id_%s_left", $leftEntity->getActiveRecordTable()); |
|
| 43 | - $this->_rightColumnName = sprintf("id_%s_right", $rightEntity->getActiveRecordTable()); |
|
| 44 | - } else { |
|
| 45 | - $this->_leftColumnName = sprintf("id_%s", $leftEntity->getActiveRecordTable()); |
|
| 46 | - $this->_rightColumnName = sprintf("id_%s", $rightEntity->getActiveRecordTable()); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - $this->extendTableDefinition($this->_leftColumnName, [ |
|
| 50 | - 'value' => &$leftVariable, |
|
| 51 | - 'validate' => null, |
|
| 52 | - 'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
| 53 | - 'properties' => ColumnProperty::NOT_NULL |
|
| 54 | - ]); |
|
| 55 | - |
|
| 56 | - $this->extendTableDefinition($this->_rightColumnName, [ |
|
| 57 | - 'value' => &$rightVariable, |
|
| 58 | - 'validate' => null, |
|
| 59 | - 'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
| 60 | - 'properties' => ColumnProperty::NOT_NULL |
|
| 61 | - ]); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Build the constraints for the many-to-many relation table |
|
| 66 | - * @return void |
|
| 67 | - */ |
|
| 68 | - public function createTableConstraints() |
|
| 69 | - { |
|
| 70 | - $childTable = $this->getActiveRecordTable(); |
|
| 71 | - |
|
| 72 | - $leftParentTable = $this->_leftEntityTable; |
|
| 73 | - $rightParentTable = $this->_rightEntityTable; |
|
| 74 | - |
|
| 75 | - $leftConstraint = $this->buildConstraint($leftParentTable, 'id', $childTable, $this->_leftColumnName); |
|
| 76 | - $rightConstraint = $this->buildConstraint($rightParentTable, 'id', $childTable, $this->_rightColumnName); |
|
| 77 | - |
|
| 78 | - $this->pdo->query($leftConstraint); |
|
| 79 | - $this->pdo->query($rightConstraint); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @return void |
|
| 84 | - */ |
|
| 85 | - abstract function getActiveRecordTable(); |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return void |
|
| 89 | - */ |
|
| 90 | - abstract function buildConstraint($parentTable, $parentColumn, $childTable, $childColumn); |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @return void |
|
| 94 | - */ |
|
| 95 | - abstract function extendTableDefinition($columnName, $definition); |
|
| 11 | + // These variables are relevant for internal bookkeeping (constraint generation etc) |
|
| 12 | + |
|
| 13 | + /** @var string The name of the left column of the relation. */ |
|
| 14 | + private $_leftColumnName; |
|
| 15 | + |
|
| 16 | + /** @var string The name of the right column of the relation. */ |
|
| 17 | + private $_rightColumnName; |
|
| 18 | + |
|
| 19 | + /** @var string The name of the left table of the relation. */ |
|
| 20 | + private $_leftEntityTable; |
|
| 21 | + |
|
| 22 | + /** @var string The name of the right table of the relation. */ |
|
| 23 | + private $_rightEntityTable; |
|
| 24 | + |
|
| 25 | + /** @var \PDO The PDO object. */ |
|
| 26 | + protected $pdo; |
|
| 27 | + /** |
|
| 28 | + * Initializes the the ManyToManyRelation trait on the included object |
|
| 29 | + * |
|
| 30 | + * @param AbstractActiveRecord $leftEntity The left entity of the relation |
|
| 31 | + * @param &variable $leftVariable The variable where the id for the left entity will be stored |
|
| 32 | + * @param AbstractActiveRecord $rightEntity The left entity of the relation |
|
| 33 | + * @param &variable $leftVariable The variable where the id for the right entity will be stored |
|
| 34 | + * @return void |
|
| 35 | + */ |
|
| 36 | + protected function initManyToManyRelation(AbstractActiveRecord $leftEntity, &$leftVariable, AbstractActiveRecord $rightEntity, &$rightVariable) |
|
| 37 | + { |
|
| 38 | + $this->_leftEntityTable = $leftEntity->getActiveRecordTable(); |
|
| 39 | + $this->_rightEntityTable = $rightEntity->getActiveRecordTable(); |
|
| 40 | + |
|
| 41 | + if (get_class($leftEntity) === get_class($rightEntity)) { |
|
| 42 | + $this->_leftColumnName = sprintf("id_%s_left", $leftEntity->getActiveRecordTable()); |
|
| 43 | + $this->_rightColumnName = sprintf("id_%s_right", $rightEntity->getActiveRecordTable()); |
|
| 44 | + } else { |
|
| 45 | + $this->_leftColumnName = sprintf("id_%s", $leftEntity->getActiveRecordTable()); |
|
| 46 | + $this->_rightColumnName = sprintf("id_%s", $rightEntity->getActiveRecordTable()); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + $this->extendTableDefinition($this->_leftColumnName, [ |
|
| 50 | + 'value' => &$leftVariable, |
|
| 51 | + 'validate' => null, |
|
| 52 | + 'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
| 53 | + 'properties' => ColumnProperty::NOT_NULL |
|
| 54 | + ]); |
|
| 55 | + |
|
| 56 | + $this->extendTableDefinition($this->_rightColumnName, [ |
|
| 57 | + 'value' => &$rightVariable, |
|
| 58 | + 'validate' => null, |
|
| 59 | + 'type' => AbstractActiveRecord::COLUMN_TYPE_ID, |
|
| 60 | + 'properties' => ColumnProperty::NOT_NULL |
|
| 61 | + ]); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Build the constraints for the many-to-many relation table |
|
| 66 | + * @return void |
|
| 67 | + */ |
|
| 68 | + public function createTableConstraints() |
|
| 69 | + { |
|
| 70 | + $childTable = $this->getActiveRecordTable(); |
|
| 71 | + |
|
| 72 | + $leftParentTable = $this->_leftEntityTable; |
|
| 73 | + $rightParentTable = $this->_rightEntityTable; |
|
| 74 | + |
|
| 75 | + $leftConstraint = $this->buildConstraint($leftParentTable, 'id', $childTable, $this->_leftColumnName); |
|
| 76 | + $rightConstraint = $this->buildConstraint($rightParentTable, 'id', $childTable, $this->_rightColumnName); |
|
| 77 | + |
|
| 78 | + $this->pdo->query($leftConstraint); |
|
| 79 | + $this->pdo->query($rightConstraint); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @return void |
|
| 84 | + */ |
|
| 85 | + abstract function getActiveRecordTable(); |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return void |
|
| 89 | + */ |
|
| 90 | + abstract function buildConstraint($parentTable, $parentColumn, $childTable, $childColumn); |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @return void |
|
| 94 | + */ |
|
| 95 | + abstract function extendTableDefinition($columnName, $definition); |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * @return void |
|
| 99 | - */ |
|
| 100 | - abstract function registerSearchHook($columnName, $fn); |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @return void |
|
| 104 | - */ |
|
| 105 | - abstract function registerDeleteHook($columnName, $fn); |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @return void |
|
| 109 | - */ |
|
| 110 | - abstract function registerUpdateHook($columnName, $fn); |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @return void |
|
| 114 | - */ |
|
| 115 | - abstract function registerReadHook($columnName, $fn); |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @return void |
|
| 119 | - */ |
|
| 120 | - abstract function registerCreateHook($columnName, $fn); |
|
| 97 | + /** |
|
| 98 | + * @return void |
|
| 99 | + */ |
|
| 100 | + abstract function registerSearchHook($columnName, $fn); |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @return void |
|
| 104 | + */ |
|
| 105 | + abstract function registerDeleteHook($columnName, $fn); |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @return void |
|
| 109 | + */ |
|
| 110 | + abstract function registerUpdateHook($columnName, $fn); |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @return void |
|
| 114 | + */ |
|
| 115 | + abstract function registerReadHook($columnName, $fn); |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @return void |
|
| 119 | + */ |
|
| 120 | + abstract function registerCreateHook($columnName, $fn); |
|
| 121 | 121 | |
| 122 | 122 | } |
@@ -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 | |
@@ -84,6 +84,7 @@ |
||
| 84 | 84 | abstract function extendTableDefinition($columnName, $definition); |
| 85 | 85 | |
| 86 | 86 | /** |
| 87 | + * @param string $fn |
|
| 87 | 88 | * @return void |
| 88 | 89 | */ |
| 89 | 90 | abstract function registerSearchHook($columnName, $fn); |
@@ -9,102 +9,102 @@ |
||
| 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 | - /** |
|
| 82 | - * @return void |
|
| 83 | - */ |
|
| 84 | - abstract function extendTableDefinition($columnName, $definition); |
|
| 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 | + /** |
|
| 82 | + * @return void |
|
| 83 | + */ |
|
| 84 | + abstract function extendTableDefinition($columnName, $definition); |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * @return void |
|
| 88 | - */ |
|
| 89 | - abstract function registerSearchHook($columnName, $fn); |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @return void |
|
| 93 | - */ |
|
| 94 | - abstract function registerDeleteHook($columnName, $fn); |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @return void |
|
| 98 | - */ |
|
| 99 | - abstract function registerUpdateHook($columnName, $fn); |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @return void |
|
| 103 | - */ |
|
| 104 | - abstract function registerReadHook($columnName, $fn); |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @return void |
|
| 108 | - */ |
|
| 109 | - abstract function registerCreateHook($columnName, $fn); |
|
| 86 | + /** |
|
| 87 | + * @return void |
|
| 88 | + */ |
|
| 89 | + abstract function registerSearchHook($columnName, $fn); |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @return void |
|
| 93 | + */ |
|
| 94 | + abstract function registerDeleteHook($columnName, $fn); |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @return void |
|
| 98 | + */ |
|
| 99 | + abstract function registerUpdateHook($columnName, $fn); |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @return void |
|
| 103 | + */ |
|
| 104 | + abstract function registerReadHook($columnName, $fn); |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @return void |
|
| 108 | + */ |
|
| 109 | + abstract function registerCreateHook($columnName, $fn); |
|
| 110 | 110 | } |
| 111 | 111 | \ 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,154 +13,154 @@ |
||
| 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 | - /** |
|
| 137 | - * @return void |
|
| 138 | - */ |
|
| 139 | - abstract function extendTableDefinition($columnName, $definition); |
|
| 136 | + /** |
|
| 137 | + * @return void |
|
| 138 | + */ |
|
| 139 | + abstract function extendTableDefinition($columnName, $definition); |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * @return void |
|
| 143 | - */ |
|
| 144 | - abstract function registerSearchHook($columnName, $fn); |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @return void |
|
| 148 | - */ |
|
| 149 | - abstract function registerDeleteHook($columnName, $fn); |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @return void |
|
| 153 | - */ |
|
| 154 | - abstract function registerUpdateHook($columnName, $fn); |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @return void |
|
| 158 | - */ |
|
| 159 | - abstract function registerReadHook($columnName, $fn); |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @return void |
|
| 163 | - */ |
|
| 164 | - abstract function registerCreateHook($columnName, $fn); |
|
| 141 | + /** |
|
| 142 | + * @return void |
|
| 143 | + */ |
|
| 144 | + abstract function registerSearchHook($columnName, $fn); |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @return void |
|
| 148 | + */ |
|
| 149 | + abstract function registerDeleteHook($columnName, $fn); |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @return void |
|
| 153 | + */ |
|
| 154 | + abstract function registerUpdateHook($columnName, $fn); |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @return void |
|
| 158 | + */ |
|
| 159 | + abstract function registerReadHook($columnName, $fn); |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @return void |
|
| 163 | + */ |
|
| 164 | + abstract function registerCreateHook($columnName, $fn); |
|
| 165 | 165 | |
| 166 | 166 | } |
| 167 | 167 | \ 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 |
@@ -62,6 +62,8 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | 64 | * Copy all table variables between two instances |
| 65 | + * @param AutoApi $to |
|
| 66 | + * @param AutoApi $from |
|
| 65 | 67 | */ |
| 66 | 68 | private function syncInstances($to, $from) |
| 67 | 69 | { |
@@ -290,12 +292,21 @@ discard block |
||
| 290 | 292 | |
| 291 | 293 | abstract public function read($id); |
| 292 | 294 | |
| 295 | + /** |
|
| 296 | + * @return \PDO |
|
| 297 | + */ |
|
| 293 | 298 | abstract public function getPdo(); |
| 294 | 299 | |
| 300 | + /** |
|
| 301 | + * @param integer $id |
|
| 302 | + */ |
|
| 295 | 303 | abstract function setId($id); |
| 296 | 304 | |
| 297 | 305 | abstract function getId(); |
| 298 | 306 | |
| 307 | + /** |
|
| 308 | + * @return string |
|
| 309 | + */ |
|
| 299 | 310 | abstract function getActiveRecordTable(); |
| 300 | 311 | |
| 301 | 312 | abstract function getActiveRecordColumns(); |
@@ -8,295 +8,295 @@ |
||
| 8 | 8 | |
| 9 | 9 | trait AutoApi |
| 10 | 10 | { |
| 11 | - /* ======================================================================= |
|
| 11 | + /* ======================================================================= |
|
| 12 | 12 | * ===================== Automatic API Support =========================== |
| 13 | 13 | * ======================================================================= */ |
| 14 | 14 | |
| 15 | - /** @var array A map of column name to functions that hook the insert function */ |
|
| 16 | - protected $registeredCreateHooks; |
|
| 15 | + /** @var array A map of column name to functions that hook the insert function */ |
|
| 16 | + protected $registeredCreateHooks; |
|
| 17 | 17 | |
| 18 | - /** @var array A map of column name to functions that hook the read function */ |
|
| 19 | - protected $registeredReadHooks; |
|
| 18 | + /** @var array A map of column name to functions that hook the read function */ |
|
| 19 | + protected $registeredReadHooks; |
|
| 20 | 20 | |
| 21 | - /** @var array A map of column name to functions that hook the update function */ |
|
| 22 | - protected $registeredUpdateHooks; |
|
| 21 | + /** @var array A map of column name to functions that hook the update function */ |
|
| 22 | + protected $registeredUpdateHooks; |
|
| 23 | 23 | |
| 24 | - /** @var array A map of column name to functions that hook the update function */ |
|
| 25 | - protected $registeredDeleteHooks; |
|
| 24 | + /** @var array A map of column name to functions that hook the update function */ |
|
| 25 | + protected $registeredDeleteHooks; |
|
| 26 | 26 | |
| 27 | - /** @var array A map of column name to functions that hook the search function */ |
|
| 28 | - protected $registeredSearchHooks; |
|
| 27 | + /** @var array A map of column name to functions that hook the search function */ |
|
| 28 | + protected $registeredSearchHooks; |
|
| 29 | 29 | |
| 30 | - /** @var array A list of table column definitions */ |
|
| 31 | - protected $tableDefinition; |
|
| 30 | + /** @var array A list of table column definitions */ |
|
| 31 | + protected $tableDefinition; |
|
| 32 | 32 | |
| 33 | - public function apiSearch($inputs, $fieldWhitelist) |
|
| 34 | - { |
|
| 35 | - // @TODO: How to handle this case? |
|
| 36 | - // => Default parameter names for searching? (limit, pagination, sort order etc) |
|
| 37 | - // Find default names for this and store in class |
|
| 38 | - // => Limited search parameters? (We don't want to be able to search on a password field for example) |
|
| 39 | - } |
|
| 33 | + public function apiSearch($inputs, $fieldWhitelist) |
|
| 34 | + { |
|
| 35 | + // @TODO: How to handle this case? |
|
| 36 | + // => Default parameter names for searching? (limit, pagination, sort order etc) |
|
| 37 | + // Find default names for this and store in class |
|
| 38 | + // => Limited search parameters? (We don't want to be able to search on a password field for example) |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - public function toArray($fieldWhitelist) |
|
| 42 | - { |
|
| 43 | - $output = []; |
|
| 44 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 45 | - if (in_array($colName, $fieldWhitelist)) { |
|
| 46 | - $output[$colName] = $definition['value']; |
|
| 47 | - } |
|
| 48 | - } |
|
| 41 | + public function toArray($fieldWhitelist) |
|
| 42 | + { |
|
| 43 | + $output = []; |
|
| 44 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 45 | + if (in_array($colName, $fieldWhitelist)) { |
|
| 46 | + $output[$colName] = $definition['value']; |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - return $output; |
|
| 51 | - } |
|
| 50 | + return $output; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - public function apiRead($id, $fieldWhitelist) |
|
| 54 | - { |
|
| 55 | - $this->read($id); |
|
| 56 | - return $this->toArray($fieldWhitelist); |
|
| 57 | - } |
|
| 53 | + public function apiRead($id, $fieldWhitelist) |
|
| 54 | + { |
|
| 55 | + $this->read($id); |
|
| 56 | + return $this->toArray($fieldWhitelist); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /* ============================================================= |
|
| 59 | + /* ============================================================= |
|
| 60 | 60 | * ===================== Constraint validation ================= |
| 61 | 61 | * ============================================================= */ |
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Copy all table variables between two instances |
|
| 65 | - */ |
|
| 66 | - private function syncInstances($to, $from) |
|
| 67 | - { |
|
| 68 | - foreach ($to->tableDefinition as $colName => $definition) { |
|
| 69 | - $definition['value'] = $from->tableDefinition[$colName]['value']; |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - private function filterInputColumns($input, $whitelist) |
|
| 74 | - { |
|
| 75 | - $filteredInput = $input; |
|
| 76 | - foreach ($input as $colName => $value) { |
|
| 77 | - if (!in_array($colName, $whitelist)) { |
|
| 78 | - unset($filteredInput[$colName]); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - return $filteredInput; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - private function validateExcessKeys($input) |
|
| 85 | - { |
|
| 86 | - $errors = []; |
|
| 87 | - foreach ($input as $colName => $value) { |
|
| 88 | - if (!array_key_exists($colName, $this->tableDefinition)) { |
|
| 89 | - $errors[$colName] = "Unknown input field"; |
|
| 90 | - continue; |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - return $errors; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - private function validateImmutableColumns($input) |
|
| 97 | - { |
|
| 98 | - $errors = []; |
|
| 99 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 100 | - $property = $definition['properties'] ?? null; |
|
| 101 | - if (array_key_exists($colName, $input) |
|
| 102 | - && $property & ColumnProperty::IMMUTABLE) { |
|
| 103 | - $errors[$colName] = "Field cannot be changed"; |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - return $errors; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - private function validateInputValues($input) |
|
| 110 | - { |
|
| 111 | - $errors = []; |
|
| 112 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 113 | - // Validation check 1: If validate function is present |
|
| 114 | - if (array_key_exists($colName, $input) |
|
| 115 | - && is_callable($definition['validate'] ?? null)) { |
|
| 116 | - $inputValue = $input[$colName]; |
|
| 117 | - |
|
| 118 | - // If validation function fails |
|
| 119 | - [$status, $message] = $definition['validate']($inputValue); |
|
| 120 | - if (!$status) { |
|
| 121 | - $errors[$colName] = $message; |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Validation check 2: If relation column, check whether entity exists |
|
| 126 | - $properties = $definition['properties'] ?? null; |
|
| 127 | - if (isset($definition['relation']) |
|
| 128 | - && ($properties & ColumnProperty::NOT_NULL)) { |
|
| 129 | - $instance = clone $definition['relation']; |
|
| 130 | - try { |
|
| 131 | - $instance->read($input[$colName] ?? null); |
|
| 132 | - } catch (ActiveRecordException $e) { |
|
| 133 | - $errors[$colName] = "Entity for this value doesn't exist"; |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - return $errors; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * This function is only used for API Update calls (direct getter/setter functions are unconstrained) |
|
| 142 | - */ |
|
| 143 | - private function validateMissingKeys() |
|
| 144 | - { |
|
| 145 | - $errors = []; |
|
| 146 | - |
|
| 147 | - foreach ($this->tableDefinition as $colName => $colDefinition) { |
|
| 148 | - $default = $colDefinition['default'] ?? null; |
|
| 149 | - $properties = $colDefinition['properties'] ?? null; |
|
| 150 | - $value = $colDefinition['value']; |
|
| 151 | - |
|
| 152 | - // If nullable and default not set => null |
|
| 153 | - // If nullable and default null => default (null) |
|
| 154 | - // If nullable and default set => default (value) |
|
| 155 | - |
|
| 156 | - // if not nullable and default not set => error |
|
| 157 | - // if not nullable and default null => error |
|
| 158 | - // if not nullable and default st => default (value) |
|
| 159 | - // => if not nullable and default null and value not set => error message in this method |
|
| 160 | - if ($properties & ColumnProperty::NOT_NULL |
|
| 161 | - && $default === null |
|
| 162 | - && !($properties & ColumnProperty::AUTO_INCREMENT) |
|
| 163 | - // && !array_key_exists($colName, $input) |
|
| 164 | - && $value === null) { |
|
| 165 | - $errors[$colName] = sprintf("The required field \"%s\" is missing", $colName); |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return $errors; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Copies the values for entries in the input with matching variable names in the record definition |
|
| 174 | - * @param Array $input The input data to be loaded into $this record |
|
| 175 | - */ |
|
| 176 | - private function loadData($input) |
|
| 177 | - { |
|
| 178 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 179 | - if (array_key_exists($colName, $input)) { |
|
| 180 | - $definition['value'] = $input[$colName]; |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @param Array $input Associative array of input values |
|
| 187 | - * @param Array $fieldWhitelist array of column names that are allowed to be filled by the input array |
|
| 188 | - * @return Array Array containing the set of optional errors (associative array) and an optional array representation (associative) |
|
| 189 | - * of the modified data. |
|
| 190 | - */ |
|
| 191 | - public function apiCreate($input, $fieldWhitelist) |
|
| 192 | - { |
|
| 193 | - // Clone $this to new instance (for restoring if validation goes wrong) |
|
| 194 | - $transaction = clone $this; |
|
| 195 | - $errors = []; |
|
| 196 | - |
|
| 197 | - // Filter out all non-whitelisted input values |
|
| 198 | - $input = $this->filterInputColumns($input, $fieldWhitelist); |
|
| 199 | - |
|
| 200 | - // Validate excess keys |
|
| 201 | - $errors += $transaction->validateExcessKeys($input); |
|
| 202 | - |
|
| 203 | - // Validate input values (using validation function) |
|
| 204 | - $errors += $transaction->validateInputValues($input); |
|
| 205 | - |
|
| 206 | - // "Copy" data into transaction |
|
| 207 | - $transaction->loadData($input); |
|
| 208 | - |
|
| 209 | - // Run create hooks |
|
| 210 | - foreach ($this->registeredCreateHooks as $colName => $fn) { |
|
| 211 | - $fn(); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - // Validate missing keys |
|
| 215 | - $errors += $transaction->validateMissingKeys(); |
|
| 216 | - |
|
| 217 | - // If no errors, commit the pending data |
|
| 218 | - if (empty($errors)) { |
|
| 219 | - $this->syncInstances($this, $transaction); |
|
| 220 | - |
|
| 221 | - try { |
|
| 222 | - $q = (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
| 223 | - ->insert($this->getActiveRecordColumns()) |
|
| 224 | - ->execute(); |
|
| 225 | - |
|
| 226 | - $this->setId(intval($this->getPdo()->lastInsertId())); |
|
| 227 | - } catch (\PDOException $e) { |
|
| 228 | - // @TODO: Potentially filter and store mysql messages (where possible) in error messages |
|
| 229 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - return [null, $this->toArray($fieldWhitelist)]; |
|
| 233 | - } else { |
|
| 234 | - return [$errors, null]; |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * @param Array $input Associative array of input values |
|
| 240 | - * @param Array $fieldWhitelist array of column names that are allowed to be filled by the input array |
|
| 241 | - * @return Array Array containing the set of optional errors (associative array) and an optional array representation (associative) |
|
| 242 | - * of the modified data. |
|
| 243 | - */ |
|
| 244 | - public function apiUpdate($input, $fieldWhitelist) |
|
| 245 | - { |
|
| 246 | - $transaction = clone $this; |
|
| 247 | - $errors = []; |
|
| 248 | - |
|
| 249 | - // Filter out all non-whitelisted input values |
|
| 250 | - $input = $this->filterInputColumns($input, $fieldWhitelist); |
|
| 251 | - |
|
| 252 | - // Check for excess keys |
|
| 253 | - $errors += $transaction->validateExcessKeys($input); |
|
| 254 | - |
|
| 255 | - // Check for immutable keys |
|
| 256 | - $errors += $transaction->validateImmutableColumns($input); |
|
| 257 | - |
|
| 258 | - // Validate input values (using validation function) |
|
| 259 | - $errors += $transaction->validateInputValues($input); |
|
| 260 | - |
|
| 261 | - // "Copy" data into transaction |
|
| 262 | - $transaction->loadData($input); |
|
| 263 | - |
|
| 264 | - // Run create hooks |
|
| 265 | - foreach ($this->registeredUpdateHooks as $colName => $fn) { |
|
| 266 | - $fn(); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - // Validate missing keys |
|
| 270 | - $errors += $transaction->validateMissingKeys(); |
|
| 271 | - |
|
| 272 | - // Update database |
|
| 273 | - if (empty($errors)) { |
|
| 274 | - $this->syncInstances($this, $transaction); |
|
| 275 | - |
|
| 276 | - try { |
|
| 277 | - (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
| 278 | - ->update($this->getActiveRecordColumns()) |
|
| 279 | - ->where('id', '=', $this->getId()) |
|
| 280 | - ->execute(); |
|
| 281 | - } catch (\PDOException $e) { |
|
| 282 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - return [null, $this->toArray($fieldWhitelist)]; |
|
| 286 | - } else { |
|
| 287 | - return [$errors, null]; |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - abstract public function read($id); |
|
| 63 | + /** |
|
| 64 | + * Copy all table variables between two instances |
|
| 65 | + */ |
|
| 66 | + private function syncInstances($to, $from) |
|
| 67 | + { |
|
| 68 | + foreach ($to->tableDefinition as $colName => $definition) { |
|
| 69 | + $definition['value'] = $from->tableDefinition[$colName]['value']; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + private function filterInputColumns($input, $whitelist) |
|
| 74 | + { |
|
| 75 | + $filteredInput = $input; |
|
| 76 | + foreach ($input as $colName => $value) { |
|
| 77 | + if (!in_array($colName, $whitelist)) { |
|
| 78 | + unset($filteredInput[$colName]); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + return $filteredInput; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + private function validateExcessKeys($input) |
|
| 85 | + { |
|
| 86 | + $errors = []; |
|
| 87 | + foreach ($input as $colName => $value) { |
|
| 88 | + if (!array_key_exists($colName, $this->tableDefinition)) { |
|
| 89 | + $errors[$colName] = "Unknown input field"; |
|
| 90 | + continue; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + return $errors; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + private function validateImmutableColumns($input) |
|
| 97 | + { |
|
| 98 | + $errors = []; |
|
| 99 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 100 | + $property = $definition['properties'] ?? null; |
|
| 101 | + if (array_key_exists($colName, $input) |
|
| 102 | + && $property & ColumnProperty::IMMUTABLE) { |
|
| 103 | + $errors[$colName] = "Field cannot be changed"; |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + return $errors; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + private function validateInputValues($input) |
|
| 110 | + { |
|
| 111 | + $errors = []; |
|
| 112 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 113 | + // Validation check 1: If validate function is present |
|
| 114 | + if (array_key_exists($colName, $input) |
|
| 115 | + && is_callable($definition['validate'] ?? null)) { |
|
| 116 | + $inputValue = $input[$colName]; |
|
| 117 | + |
|
| 118 | + // If validation function fails |
|
| 119 | + [$status, $message] = $definition['validate']($inputValue); |
|
| 120 | + if (!$status) { |
|
| 121 | + $errors[$colName] = $message; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Validation check 2: If relation column, check whether entity exists |
|
| 126 | + $properties = $definition['properties'] ?? null; |
|
| 127 | + if (isset($definition['relation']) |
|
| 128 | + && ($properties & ColumnProperty::NOT_NULL)) { |
|
| 129 | + $instance = clone $definition['relation']; |
|
| 130 | + try { |
|
| 131 | + $instance->read($input[$colName] ?? null); |
|
| 132 | + } catch (ActiveRecordException $e) { |
|
| 133 | + $errors[$colName] = "Entity for this value doesn't exist"; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + return $errors; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * This function is only used for API Update calls (direct getter/setter functions are unconstrained) |
|
| 142 | + */ |
|
| 143 | + private function validateMissingKeys() |
|
| 144 | + { |
|
| 145 | + $errors = []; |
|
| 146 | + |
|
| 147 | + foreach ($this->tableDefinition as $colName => $colDefinition) { |
|
| 148 | + $default = $colDefinition['default'] ?? null; |
|
| 149 | + $properties = $colDefinition['properties'] ?? null; |
|
| 150 | + $value = $colDefinition['value']; |
|
| 151 | + |
|
| 152 | + // If nullable and default not set => null |
|
| 153 | + // If nullable and default null => default (null) |
|
| 154 | + // If nullable and default set => default (value) |
|
| 155 | + |
|
| 156 | + // if not nullable and default not set => error |
|
| 157 | + // if not nullable and default null => error |
|
| 158 | + // if not nullable and default st => default (value) |
|
| 159 | + // => if not nullable and default null and value not set => error message in this method |
|
| 160 | + if ($properties & ColumnProperty::NOT_NULL |
|
| 161 | + && $default === null |
|
| 162 | + && !($properties & ColumnProperty::AUTO_INCREMENT) |
|
| 163 | + // && !array_key_exists($colName, $input) |
|
| 164 | + && $value === null) { |
|
| 165 | + $errors[$colName] = sprintf("The required field \"%s\" is missing", $colName); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return $errors; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Copies the values for entries in the input with matching variable names in the record definition |
|
| 174 | + * @param Array $input The input data to be loaded into $this record |
|
| 175 | + */ |
|
| 176 | + private function loadData($input) |
|
| 177 | + { |
|
| 178 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 179 | + if (array_key_exists($colName, $input)) { |
|
| 180 | + $definition['value'] = $input[$colName]; |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @param Array $input Associative array of input values |
|
| 187 | + * @param Array $fieldWhitelist array of column names that are allowed to be filled by the input array |
|
| 188 | + * @return Array Array containing the set of optional errors (associative array) and an optional array representation (associative) |
|
| 189 | + * of the modified data. |
|
| 190 | + */ |
|
| 191 | + public function apiCreate($input, $fieldWhitelist) |
|
| 192 | + { |
|
| 193 | + // Clone $this to new instance (for restoring if validation goes wrong) |
|
| 194 | + $transaction = clone $this; |
|
| 195 | + $errors = []; |
|
| 196 | + |
|
| 197 | + // Filter out all non-whitelisted input values |
|
| 198 | + $input = $this->filterInputColumns($input, $fieldWhitelist); |
|
| 199 | + |
|
| 200 | + // Validate excess keys |
|
| 201 | + $errors += $transaction->validateExcessKeys($input); |
|
| 202 | + |
|
| 203 | + // Validate input values (using validation function) |
|
| 204 | + $errors += $transaction->validateInputValues($input); |
|
| 205 | + |
|
| 206 | + // "Copy" data into transaction |
|
| 207 | + $transaction->loadData($input); |
|
| 208 | + |
|
| 209 | + // Run create hooks |
|
| 210 | + foreach ($this->registeredCreateHooks as $colName => $fn) { |
|
| 211 | + $fn(); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + // Validate missing keys |
|
| 215 | + $errors += $transaction->validateMissingKeys(); |
|
| 216 | + |
|
| 217 | + // If no errors, commit the pending data |
|
| 218 | + if (empty($errors)) { |
|
| 219 | + $this->syncInstances($this, $transaction); |
|
| 220 | + |
|
| 221 | + try { |
|
| 222 | + $q = (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
| 223 | + ->insert($this->getActiveRecordColumns()) |
|
| 224 | + ->execute(); |
|
| 225 | + |
|
| 226 | + $this->setId(intval($this->getPdo()->lastInsertId())); |
|
| 227 | + } catch (\PDOException $e) { |
|
| 228 | + // @TODO: Potentially filter and store mysql messages (where possible) in error messages |
|
| 229 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + return [null, $this->toArray($fieldWhitelist)]; |
|
| 233 | + } else { |
|
| 234 | + return [$errors, null]; |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * @param Array $input Associative array of input values |
|
| 240 | + * @param Array $fieldWhitelist array of column names that are allowed to be filled by the input array |
|
| 241 | + * @return Array Array containing the set of optional errors (associative array) and an optional array representation (associative) |
|
| 242 | + * of the modified data. |
|
| 243 | + */ |
|
| 244 | + public function apiUpdate($input, $fieldWhitelist) |
|
| 245 | + { |
|
| 246 | + $transaction = clone $this; |
|
| 247 | + $errors = []; |
|
| 248 | + |
|
| 249 | + // Filter out all non-whitelisted input values |
|
| 250 | + $input = $this->filterInputColumns($input, $fieldWhitelist); |
|
| 251 | + |
|
| 252 | + // Check for excess keys |
|
| 253 | + $errors += $transaction->validateExcessKeys($input); |
|
| 254 | + |
|
| 255 | + // Check for immutable keys |
|
| 256 | + $errors += $transaction->validateImmutableColumns($input); |
|
| 257 | + |
|
| 258 | + // Validate input values (using validation function) |
|
| 259 | + $errors += $transaction->validateInputValues($input); |
|
| 260 | + |
|
| 261 | + // "Copy" data into transaction |
|
| 262 | + $transaction->loadData($input); |
|
| 263 | + |
|
| 264 | + // Run create hooks |
|
| 265 | + foreach ($this->registeredUpdateHooks as $colName => $fn) { |
|
| 266 | + $fn(); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + // Validate missing keys |
|
| 270 | + $errors += $transaction->validateMissingKeys(); |
|
| 271 | + |
|
| 272 | + // Update database |
|
| 273 | + if (empty($errors)) { |
|
| 274 | + $this->syncInstances($this, $transaction); |
|
| 275 | + |
|
| 276 | + try { |
|
| 277 | + (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
| 278 | + ->update($this->getActiveRecordColumns()) |
|
| 279 | + ->where('id', '=', $this->getId()) |
|
| 280 | + ->execute(); |
|
| 281 | + } catch (\PDOException $e) { |
|
| 282 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + return [null, $this->toArray($fieldWhitelist)]; |
|
| 286 | + } else { |
|
| 287 | + return [$errors, null]; |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + abstract public function read($id); |
|
| 292 | 292 | |
| 293 | - abstract public function getPdo(); |
|
| 293 | + abstract public function getPdo(); |
|
| 294 | 294 | |
| 295 | - abstract function setId($id); |
|
| 295 | + abstract function setId($id); |
|
| 296 | 296 | |
| 297 | - abstract function getId(); |
|
| 297 | + abstract function getId(); |
|
| 298 | 298 | |
| 299 | - abstract function getActiveRecordTable(); |
|
| 299 | + abstract function getActiveRecordTable(); |
|
| 300 | 300 | |
| 301 | - abstract function getActiveRecordColumns(); |
|
| 301 | + abstract function getActiveRecordColumns(); |
|
| 302 | 302 | } |
@@ -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 |