@@ -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 |
@@ -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 |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | // Cleanup SQL |
10 | 10 | $user_delete = "DROP USER IF EXISTS $dbuser@'localhost'; "; |
11 | 11 | $database_delete = "DROP DATABASE IF EXISTS $dbname; "; |
12 | -$sql_cleanup = $user_delete . $database_delete; |
|
12 | +$sql_cleanup = $user_delete.$database_delete; |
|
13 | 13 | |
14 | 14 | // Setup SQL |
15 | 15 | $db_create = "CREATE DATABASE $dbname; "; |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | $user_alter = "ALTER USER '$dbuser'@'localhost' IDENTIFIED with mysql_native_password BY '$dbpass'; "; |
18 | 18 | $user_grant = "GRANT ALL PRIVILEGES ON *.* TO '$dbuser'@'localhost';"; |
19 | 19 | |
20 | -$sql_setup = $sql_cleanup . $db_create . $user_create . $user_alter . $user_grant; |
|
20 | +$sql_setup = $sql_cleanup.$db_create.$user_create.$user_alter.$user_grant; |
|
21 | 21 | print $sql_setup; |
22 | 22 | echo "Please enter mysql root password to set up a database environment for testing\n"; |
23 | 23 | exec("echo \"$sql_setup\" | mysql -u root -p"); |
@@ -11,154 +11,154 @@ |
||
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 | - /** |
|
27 | - * Registers the Address trait on the including class |
|
28 | - * @return void |
|
29 | - */ |
|
30 | - protected function initAddress() |
|
31 | - { |
|
32 | - $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ADDRESS, [ |
|
33 | - 'value' => &$this->address, |
|
34 | - 'validate' => null, |
|
35 | - 'type' => 'VARCHAR', |
|
36 | - 'length' => 1024, |
|
37 | - 'properties' => null |
|
38 | - ]); |
|
39 | - |
|
40 | - $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ZIPCODE, [ |
|
41 | - 'value' => &$this->zipcode, |
|
42 | - 'validate' => null, |
|
43 | - 'type' => 'VARCHAR', |
|
44 | - 'length' => 1024, |
|
45 | - 'properties' => null |
|
46 | - ]); |
|
47 | - |
|
48 | - $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_CITY, [ |
|
49 | - 'value' => &$this->city, |
|
50 | - 'validate' => null, |
|
51 | - 'type' => 'VARCHAR', |
|
52 | - 'length' => 1024, |
|
53 | - 'properties' => null |
|
54 | - ]); |
|
55 | - |
|
56 | - $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_COUNTRY, [ |
|
57 | - 'value' => &$this->country, |
|
58 | - 'validate' => null, |
|
59 | - 'type' => 'VARCHAR', |
|
60 | - 'length' => 1024, |
|
61 | - 'properties' => null |
|
62 | - ]); |
|
63 | - |
|
64 | - $this->address = null; |
|
65 | - $this->zipcode = null; |
|
66 | - $this->city = null; |
|
67 | - $this->country = null; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @return string |
|
72 | - */ |
|
73 | - public function getAddress() |
|
74 | - { |
|
75 | - return $this->address; |
|
76 | - } |
|
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 | + /** |
|
27 | + * Registers the Address trait on the including class |
|
28 | + * @return void |
|
29 | + */ |
|
30 | + protected function initAddress() |
|
31 | + { |
|
32 | + $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ADDRESS, [ |
|
33 | + 'value' => &$this->address, |
|
34 | + 'validate' => null, |
|
35 | + 'type' => 'VARCHAR', |
|
36 | + 'length' => 1024, |
|
37 | + 'properties' => null |
|
38 | + ]); |
|
39 | + |
|
40 | + $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_ZIPCODE, [ |
|
41 | + 'value' => &$this->zipcode, |
|
42 | + 'validate' => null, |
|
43 | + 'type' => 'VARCHAR', |
|
44 | + 'length' => 1024, |
|
45 | + 'properties' => null |
|
46 | + ]); |
|
47 | + |
|
48 | + $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_CITY, [ |
|
49 | + 'value' => &$this->city, |
|
50 | + 'validate' => null, |
|
51 | + 'type' => 'VARCHAR', |
|
52 | + 'length' => 1024, |
|
53 | + 'properties' => null |
|
54 | + ]); |
|
55 | + |
|
56 | + $this->extendTableDefinition(TRAIT_ADDRESS_FIELD_COUNTRY, [ |
|
57 | + 'value' => &$this->country, |
|
58 | + 'validate' => null, |
|
59 | + 'type' => 'VARCHAR', |
|
60 | + 'length' => 1024, |
|
61 | + 'properties' => null |
|
62 | + ]); |
|
63 | + |
|
64 | + $this->address = null; |
|
65 | + $this->zipcode = null; |
|
66 | + $this->city = null; |
|
67 | + $this->country = null; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @return string |
|
72 | + */ |
|
73 | + public function getAddress() |
|
74 | + { |
|
75 | + return $this->address; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @param string $address |
|
80 | - */ |
|
81 | - public function setAddress($address) |
|
82 | - { |
|
83 | - $this->address = $address; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * @return string |
|
88 | - */ |
|
89 | - public function getZipcode() |
|
90 | - { |
|
91 | - return $this->zipcode; |
|
92 | - } |
|
78 | + /** |
|
79 | + * @param string $address |
|
80 | + */ |
|
81 | + public function setAddress($address) |
|
82 | + { |
|
83 | + $this->address = $address; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * @return string |
|
88 | + */ |
|
89 | + public function getZipcode() |
|
90 | + { |
|
91 | + return $this->zipcode; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * @param string $zipcode |
|
96 | - */ |
|
97 | - public function setZipcode($zipcode) |
|
98 | - { |
|
99 | - $this->zipcode = $zipcode; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @return string |
|
104 | - */ |
|
105 | - public function getCity() |
|
106 | - { |
|
107 | - return $this->city; |
|
108 | - } |
|
94 | + /** |
|
95 | + * @param string $zipcode |
|
96 | + */ |
|
97 | + public function setZipcode($zipcode) |
|
98 | + { |
|
99 | + $this->zipcode = $zipcode; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @return string |
|
104 | + */ |
|
105 | + public function getCity() |
|
106 | + { |
|
107 | + return $this->city; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * @param string $city |
|
112 | - */ |
|
113 | - public function setCity($city) |
|
114 | - { |
|
115 | - $this->city = $city; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @return string |
|
120 | - */ |
|
121 | - public function getCountry() |
|
122 | - { |
|
123 | - return $this->country; |
|
124 | - } |
|
110 | + /** |
|
111 | + * @param string $city |
|
112 | + */ |
|
113 | + public function setCity($city) |
|
114 | + { |
|
115 | + $this->city = $city; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @return string |
|
120 | + */ |
|
121 | + public function getCountry() |
|
122 | + { |
|
123 | + return $this->country; |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * @param string $country |
|
128 | - */ |
|
129 | - public function setCountry($country) |
|
130 | - { |
|
131 | - $this->country = $country; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @return void |
|
136 | - */ |
|
137 | - abstract protected function extendTableDefinition($columnName, $definition); |
|
126 | + /** |
|
127 | + * @param string $country |
|
128 | + */ |
|
129 | + public function setCountry($country) |
|
130 | + { |
|
131 | + $this->country = $country; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @return void |
|
136 | + */ |
|
137 | + abstract protected function extendTableDefinition($columnName, $definition); |
|
138 | 138 | |
139 | - /** |
|
140 | - * @return void |
|
141 | - */ |
|
142 | - abstract protected function registerSearchHook($columnName, $fn); |
|
143 | - |
|
144 | - /** |
|
145 | - * @return void |
|
146 | - */ |
|
147 | - abstract protected function registerDeleteHook($columnName, $fn); |
|
148 | - |
|
149 | - /** |
|
150 | - * @return void |
|
151 | - */ |
|
152 | - abstract protected function registerUpdateHook($columnName, $fn); |
|
153 | - |
|
154 | - /** |
|
155 | - * @return void |
|
156 | - */ |
|
157 | - abstract protected function registerReadHook($columnName, $fn); |
|
158 | - |
|
159 | - /** |
|
160 | - * @return void |
|
161 | - */ |
|
162 | - abstract protected function registerCreateHook($columnName, $fn); |
|
139 | + /** |
|
140 | + * @return void |
|
141 | + */ |
|
142 | + abstract protected function registerSearchHook($columnName, $fn); |
|
143 | + |
|
144 | + /** |
|
145 | + * @return void |
|
146 | + */ |
|
147 | + abstract protected function registerDeleteHook($columnName, $fn); |
|
148 | + |
|
149 | + /** |
|
150 | + * @return void |
|
151 | + */ |
|
152 | + abstract protected function registerUpdateHook($columnName, $fn); |
|
153 | + |
|
154 | + /** |
|
155 | + * @return void |
|
156 | + */ |
|
157 | + abstract protected function registerReadHook($columnName, $fn); |
|
158 | + |
|
159 | + /** |
|
160 | + * @return void |
|
161 | + */ |
|
162 | + abstract protected function registerCreateHook($columnName, $fn); |
|
163 | 163 | |
164 | 164 | } |
165 | 165 | \ No newline at end of file |
@@ -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 protected 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 protected function extendTableDefinition($columnName, $definition); |
|
88 | 88 | |
89 | - /** |
|
90 | - * @return void |
|
91 | - */ |
|
92 | - abstract protected function registerSearchHook($columnName, $fn); |
|
93 | - |
|
94 | - /** |
|
95 | - * @return void |
|
96 | - */ |
|
97 | - abstract protected function registerDeleteHook($columnName, $fn); |
|
98 | - |
|
99 | - /** |
|
100 | - * @return void |
|
101 | - */ |
|
102 | - abstract protected function registerUpdateHook($columnName, $fn); |
|
103 | - |
|
104 | - /** |
|
105 | - * @return void |
|
106 | - */ |
|
107 | - abstract protected function registerReadHook($columnName, $fn); |
|
108 | - |
|
109 | - /** |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - abstract protected function registerCreateHook($columnName, $fn); |
|
89 | + /** |
|
90 | + * @return void |
|
91 | + */ |
|
92 | + abstract protected function registerSearchHook($columnName, $fn); |
|
93 | + |
|
94 | + /** |
|
95 | + * @return void |
|
96 | + */ |
|
97 | + abstract protected function registerDeleteHook($columnName, $fn); |
|
98 | + |
|
99 | + /** |
|
100 | + * @return void |
|
101 | + */ |
|
102 | + abstract protected function registerUpdateHook($columnName, $fn); |
|
103 | + |
|
104 | + /** |
|
105 | + * @return void |
|
106 | + */ |
|
107 | + abstract protected function registerReadHook($columnName, $fn); |
|
108 | + |
|
109 | + /** |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + abstract protected 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 |
@@ -13,163 +13,163 @@ |
||
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' => [$this, 'validatePassword'], |
|
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 | - public function validatePassword($password) { |
|
80 | - if (strlen($password) < TRAIT_PASSWORD_MIN_LENGTH) { |
|
81 | - $message = sprintf('\'Password\' must be atleast %s characters long. %s characters provied.', TRAIT_PASSWORD_MIN_LENGTH, strlen($password)); |
|
82 | - return [false, $message]; |
|
83 | - } |
|
84 | - return [true, '']; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Set the password. |
|
89 | - * |
|
90 | - * @param string $password |
|
91 | - * @return $this |
|
92 | - * @throws \Exception |
|
93 | - */ |
|
94 | - public function setPassword($password) |
|
95 | - { |
|
96 | - [$status, $error] = $this->validatePassword($password); |
|
97 | - if (!$status) { |
|
98 | - throw new ActiveRecordTraitException($error); |
|
99 | - } |
|
100 | - |
|
101 | - $passwordHash = \password_hash($password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH]); |
|
102 | - |
|
103 | - if ($passwordHash === false) { |
|
104 | - throw new ActiveRecordTraitException('\'Password\' hash failed.'); |
|
105 | - } |
|
106 | - |
|
107 | - $this->password = $passwordHash; |
|
108 | - |
|
109 | - return $this; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @return string The Hash of the password |
|
114 | - */ |
|
115 | - public function getPasswordHash() |
|
116 | - { |
|
117 | - return $this->password; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Returns the currently set password token for the entity, or null if not set |
|
122 | - * @return string|null The password reset token |
|
123 | - */ |
|
124 | - public function getPasswordResetToken() |
|
125 | - { |
|
126 | - return $this->passwordResetToken; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Generates a new password reset token for the user |
|
131 | - */ |
|
132 | - public function generatePasswordResetToken() |
|
133 | - { |
|
134 | - $this->passwordResetToken = md5(uniqid(mt_rand(), true)); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Clears the current password reset token |
|
139 | - */ |
|
140 | - public function clearPasswordResetToken() |
|
141 | - { |
|
142 | - $this->passwordResetToken = null; |
|
143 | - } |
|
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' => [$this, 'validatePassword'], |
|
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 | + public function validatePassword($password) { |
|
80 | + if (strlen($password) < TRAIT_PASSWORD_MIN_LENGTH) { |
|
81 | + $message = sprintf('\'Password\' must be atleast %s characters long. %s characters provied.', TRAIT_PASSWORD_MIN_LENGTH, strlen($password)); |
|
82 | + return [false, $message]; |
|
83 | + } |
|
84 | + return [true, '']; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Set the password. |
|
89 | + * |
|
90 | + * @param string $password |
|
91 | + * @return $this |
|
92 | + * @throws \Exception |
|
93 | + */ |
|
94 | + public function setPassword($password) |
|
95 | + { |
|
96 | + [$status, $error] = $this->validatePassword($password); |
|
97 | + if (!$status) { |
|
98 | + throw new ActiveRecordTraitException($error); |
|
99 | + } |
|
100 | + |
|
101 | + $passwordHash = \password_hash($password, TRAIT_PASSWORD_ENCRYPTION, ['cost' => TRAIT_PASSWORD_STRENTH]); |
|
102 | + |
|
103 | + if ($passwordHash === false) { |
|
104 | + throw new ActiveRecordTraitException('\'Password\' hash failed.'); |
|
105 | + } |
|
106 | + |
|
107 | + $this->password = $passwordHash; |
|
108 | + |
|
109 | + return $this; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @return string The Hash of the password |
|
114 | + */ |
|
115 | + public function getPasswordHash() |
|
116 | + { |
|
117 | + return $this->password; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Returns the currently set password token for the entity, or null if not set |
|
122 | + * @return string|null The password reset token |
|
123 | + */ |
|
124 | + public function getPasswordResetToken() |
|
125 | + { |
|
126 | + return $this->passwordResetToken; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Generates a new password reset token for the user |
|
131 | + */ |
|
132 | + public function generatePasswordResetToken() |
|
133 | + { |
|
134 | + $this->passwordResetToken = md5(uniqid(mt_rand(), true)); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Clears the current password reset token |
|
139 | + */ |
|
140 | + public function clearPasswordResetToken() |
|
141 | + { |
|
142 | + $this->passwordResetToken = null; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * @return void |
|
147 | - */ |
|
148 | - abstract protected function extendTableDefinition($columnName, $definition); |
|
145 | + /** |
|
146 | + * @return void |
|
147 | + */ |
|
148 | + abstract protected function extendTableDefinition($columnName, $definition); |
|
149 | 149 | |
150 | - /** |
|
151 | - * @return void |
|
152 | - */ |
|
153 | - abstract protected function registerSearchHook($columnName, $fn); |
|
154 | - |
|
155 | - /** |
|
156 | - * @return void |
|
157 | - */ |
|
158 | - abstract protected function registerDeleteHook($columnName, $fn); |
|
159 | - |
|
160 | - /** |
|
161 | - * @return void |
|
162 | - */ |
|
163 | - abstract protected function registerUpdateHook($columnName, $fn); |
|
164 | - |
|
165 | - /** |
|
166 | - * @return void |
|
167 | - */ |
|
168 | - abstract protected function registerReadHook($columnName, $fn); |
|
169 | - |
|
170 | - /** |
|
171 | - * @return void |
|
172 | - */ |
|
173 | - abstract protected function registerCreateHook($columnName, $fn); |
|
150 | + /** |
|
151 | + * @return void |
|
152 | + */ |
|
153 | + abstract protected function registerSearchHook($columnName, $fn); |
|
154 | + |
|
155 | + /** |
|
156 | + * @return void |
|
157 | + */ |
|
158 | + abstract protected function registerDeleteHook($columnName, $fn); |
|
159 | + |
|
160 | + /** |
|
161 | + * @return void |
|
162 | + */ |
|
163 | + abstract protected function registerUpdateHook($columnName, $fn); |
|
164 | + |
|
165 | + /** |
|
166 | + * @return void |
|
167 | + */ |
|
168 | + abstract protected function registerReadHook($columnName, $fn); |
|
169 | + |
|
170 | + /** |
|
171 | + * @return void |
|
172 | + */ |
|
173 | + abstract protected function registerCreateHook($columnName, $fn); |
|
174 | 174 | |
175 | 175 | } |
176 | 176 | \ No newline at end of file |
@@ -9,103 +9,103 @@ |
||
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() |
|
39 | - { |
|
40 | - return Query::Equal(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() |
|
39 | + { |
|
40 | + return Query::Equal(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 protected 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 protected function extendTableDefinition($columnName, $definition); |
|
85 | 85 | |
86 | - /** |
|
87 | - * @return void |
|
88 | - */ |
|
89 | - abstract protected function registerSearchHook($columnName, $fn); |
|
90 | - |
|
91 | - /** |
|
92 | - * @return void |
|
93 | - */ |
|
94 | - abstract protected function registerDeleteHook($columnName, $fn); |
|
95 | - |
|
96 | - /** |
|
97 | - * @return void |
|
98 | - */ |
|
99 | - abstract protected function registerUpdateHook($columnName, $fn); |
|
100 | - |
|
101 | - /** |
|
102 | - * @return void |
|
103 | - */ |
|
104 | - abstract protected function registerReadHook($columnName, $fn); |
|
105 | - |
|
106 | - /** |
|
107 | - * @return void |
|
108 | - */ |
|
109 | - abstract protected function registerCreateHook($columnName, $fn); |
|
86 | + /** |
|
87 | + * @return void |
|
88 | + */ |
|
89 | + abstract protected function registerSearchHook($columnName, $fn); |
|
90 | + |
|
91 | + /** |
|
92 | + * @return void |
|
93 | + */ |
|
94 | + abstract protected function registerDeleteHook($columnName, $fn); |
|
95 | + |
|
96 | + /** |
|
97 | + * @return void |
|
98 | + */ |
|
99 | + abstract protected function registerUpdateHook($columnName, $fn); |
|
100 | + |
|
101 | + /** |
|
102 | + * @return void |
|
103 | + */ |
|
104 | + abstract protected function registerReadHook($columnName, $fn); |
|
105 | + |
|
106 | + /** |
|
107 | + * @return void |
|
108 | + */ |
|
109 | + abstract protected function registerCreateHook($columnName, $fn); |
|
110 | 110 | |
111 | 111 | } |
112 | 112 | \ No newline at end of file |
@@ -57,7 +57,7 @@ |
||
57 | 57 | } else if (count($clauses) >= 2) |
58 | 58 | { |
59 | 59 | $rest = array_slice($clauses, 1); |
60 | - $this->query->where(Query::And($clauses[0], ...$rest)); |
|
60 | + $this->query->where(Query:: And ($clauses[0], ...$rest)); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | $this->query->select(); |
@@ -21,190 +21,190 @@ |
||
21 | 21 | class ActiveRecordQuery implements \IteratorAggregate |
22 | 22 | { |
23 | 23 | |
24 | - private $clauses = []; |
|
24 | + private $clauses = []; |
|
25 | 25 | |
26 | - private $query; |
|
26 | + private $query; |
|
27 | 27 | |
28 | - private $results; |
|
28 | + private $results; |
|
29 | 29 | |
30 | - private $table; |
|
30 | + private $table; |
|
31 | 31 | |
32 | - private $type; |
|
32 | + private $type; |
|
33 | 33 | |
34 | - private $whereExpression = null; |
|
35 | - |
|
36 | - /** |
|
37 | - * Constructs a new Active Record Query |
|
38 | - */ |
|
39 | - public function __construct(AbstractActiveRecord $instance, $table, Array $additionalWhereClauses) |
|
40 | - { |
|
41 | - $this->table = $table; |
|
42 | - $this->query = new Query($instance->getPdo(), $table); |
|
43 | - $this->type = $instance; |
|
44 | - $this->clauses = $additionalWhereClauses; |
|
45 | - $this->results = null; |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Executes the query |
|
50 | - */ |
|
51 | - private function execute() |
|
52 | - { |
|
53 | - $clauses = $this->clauses; |
|
54 | - |
|
55 | - // Optionally add user concatenated where expression |
|
56 | - if ($this->whereExpression !== null) |
|
57 | - { |
|
58 | - $clauses[] = $this->whereExpression; |
|
59 | - } |
|
60 | - |
|
61 | - // Construct where clause |
|
62 | - if (count($clauses) == 1) |
|
63 | - { |
|
64 | - $this->query->where($clauses[0]); |
|
65 | - } else if (count($clauses) >= 2) |
|
66 | - { |
|
67 | - $rest = array_slice($clauses, 1); |
|
68 | - $this->query->where(Query::And($clauses[0], ...$rest)); |
|
69 | - } |
|
70 | - |
|
71 | - $this->query->select(); |
|
72 | - |
|
73 | - $this->results = $this->query->execute(); |
|
74 | - |
|
75 | - return $this; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Returns an iterator for the result set |
|
80 | - * @return ArrayIterator |
|
81 | - */ |
|
82 | - public function getIterator() |
|
83 | - { |
|
84 | - return new \ArrayIterator($this->fetchAll()); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * returns the result set of ActiveRecord instances for this query |
|
89 | - * @return Array |
|
90 | - */ |
|
91 | - public function fetchAll() |
|
92 | - { |
|
93 | - try { |
|
94 | - if ($this->results === null) { |
|
95 | - $this->execute(); |
|
96 | - } |
|
97 | - |
|
98 | - $entries = $this->results->fetchAll(); |
|
99 | - if ($entries === false) { |
|
100 | - throw new ActiveRecordException(sprintf('Can not search non-existent entries from the `%s` table.', $this->table)); |
|
101 | - } |
|
102 | - |
|
103 | - $typedResults = []; |
|
104 | - |
|
105 | - foreach ($entries as $entry) { |
|
106 | - $typedEntry = $this->type->newInstance(); |
|
107 | - $typedEntry->fill($entry); |
|
108 | - $typedResults[] = $typedEntry; |
|
109 | - } |
|
110 | - |
|
111 | - return $typedResults; |
|
112 | - } catch (\PDOException $e) { |
|
113 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Fetch one record from the database |
|
119 | - * @return AbstractActiveRecord |
|
120 | - */ |
|
121 | - public function fetch() |
|
122 | - { |
|
123 | - try { |
|
124 | - if ($this->results === null) |
|
125 | - { |
|
126 | - $this->execute(); |
|
127 | - } |
|
128 | - |
|
129 | - $typedResult = $this->type->newInstance(); |
|
130 | - |
|
131 | - $entry = $this->results->fetch(); |
|
132 | - if ($entry === false) { |
|
133 | - throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->table)); |
|
134 | - } |
|
135 | - |
|
136 | - $typedResult->fill($entry); |
|
137 | - |
|
138 | - return $typedResult; |
|
139 | - } catch (\PDOException $e) { |
|
140 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
141 | - } |
|
142 | - } |
|
143 | - |
|
144 | - |
|
145 | - /** |
|
146 | - * Set the where condition |
|
147 | - * |
|
148 | - * @param QueryExpression $expression the query expression |
|
149 | - * @return $this |
|
150 | - * @see https://en.wikipedia.org/wiki/SQL#Operators |
|
151 | - * @see https://en.wikipedia.org/wiki/Where_(SQL) |
|
152 | - */ |
|
153 | - public function where(QueryExpression $expression) |
|
154 | - { |
|
155 | - $this->whereExpression = $expression; |
|
156 | - return $this; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Set an additional group by. |
|
161 | - * |
|
162 | - * @param string $column |
|
163 | - * @return $this |
|
164 | - * @see https://en.wikipedia.org/wiki/SQL#Queries |
|
165 | - */ |
|
166 | - public function groupBy($column) |
|
167 | - { |
|
168 | - $this->query->groupBy($column); |
|
169 | - return $this; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Set an additional order condition. |
|
174 | - * |
|
175 | - * @param string $column |
|
176 | - * @param string|null $order |
|
177 | - * @return $this |
|
178 | - * @see https://en.wikipedia.org/wiki/SQL#Queries |
|
179 | - * @see https://en.wikipedia.org/wiki/Order_by |
|
180 | - */ |
|
181 | - public function orderBy($column, $order = null) |
|
182 | - { |
|
183 | - $this->query->orderBy($column, $order); |
|
184 | - return $this; |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Set the limit. |
|
189 | - * |
|
190 | - * @param mixed $limit |
|
191 | - * @return $this |
|
192 | - */ |
|
193 | - public function limit($limit) |
|
194 | - { |
|
195 | - $this->query->limit($limit); |
|
196 | - return $this; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Set the offset. |
|
201 | - * |
|
202 | - * @param mixed $offset |
|
203 | - * @return $this |
|
204 | - */ |
|
205 | - public function offset($offset) |
|
206 | - { |
|
207 | - $this->query->offset($offset); |
|
208 | - return $this; |
|
209 | - } |
|
34 | + private $whereExpression = null; |
|
35 | + |
|
36 | + /** |
|
37 | + * Constructs a new Active Record Query |
|
38 | + */ |
|
39 | + public function __construct(AbstractActiveRecord $instance, $table, Array $additionalWhereClauses) |
|
40 | + { |
|
41 | + $this->table = $table; |
|
42 | + $this->query = new Query($instance->getPdo(), $table); |
|
43 | + $this->type = $instance; |
|
44 | + $this->clauses = $additionalWhereClauses; |
|
45 | + $this->results = null; |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Executes the query |
|
50 | + */ |
|
51 | + private function execute() |
|
52 | + { |
|
53 | + $clauses = $this->clauses; |
|
54 | + |
|
55 | + // Optionally add user concatenated where expression |
|
56 | + if ($this->whereExpression !== null) |
|
57 | + { |
|
58 | + $clauses[] = $this->whereExpression; |
|
59 | + } |
|
60 | + |
|
61 | + // Construct where clause |
|
62 | + if (count($clauses) == 1) |
|
63 | + { |
|
64 | + $this->query->where($clauses[0]); |
|
65 | + } else if (count($clauses) >= 2) |
|
66 | + { |
|
67 | + $rest = array_slice($clauses, 1); |
|
68 | + $this->query->where(Query::And($clauses[0], ...$rest)); |
|
69 | + } |
|
70 | + |
|
71 | + $this->query->select(); |
|
72 | + |
|
73 | + $this->results = $this->query->execute(); |
|
74 | + |
|
75 | + return $this; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Returns an iterator for the result set |
|
80 | + * @return ArrayIterator |
|
81 | + */ |
|
82 | + public function getIterator() |
|
83 | + { |
|
84 | + return new \ArrayIterator($this->fetchAll()); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * returns the result set of ActiveRecord instances for this query |
|
89 | + * @return Array |
|
90 | + */ |
|
91 | + public function fetchAll() |
|
92 | + { |
|
93 | + try { |
|
94 | + if ($this->results === null) { |
|
95 | + $this->execute(); |
|
96 | + } |
|
97 | + |
|
98 | + $entries = $this->results->fetchAll(); |
|
99 | + if ($entries === false) { |
|
100 | + throw new ActiveRecordException(sprintf('Can not search non-existent entries from the `%s` table.', $this->table)); |
|
101 | + } |
|
102 | + |
|
103 | + $typedResults = []; |
|
104 | + |
|
105 | + foreach ($entries as $entry) { |
|
106 | + $typedEntry = $this->type->newInstance(); |
|
107 | + $typedEntry->fill($entry); |
|
108 | + $typedResults[] = $typedEntry; |
|
109 | + } |
|
110 | + |
|
111 | + return $typedResults; |
|
112 | + } catch (\PDOException $e) { |
|
113 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Fetch one record from the database |
|
119 | + * @return AbstractActiveRecord |
|
120 | + */ |
|
121 | + public function fetch() |
|
122 | + { |
|
123 | + try { |
|
124 | + if ($this->results === null) |
|
125 | + { |
|
126 | + $this->execute(); |
|
127 | + } |
|
128 | + |
|
129 | + $typedResult = $this->type->newInstance(); |
|
130 | + |
|
131 | + $entry = $this->results->fetch(); |
|
132 | + if ($entry === false) { |
|
133 | + throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->table)); |
|
134 | + } |
|
135 | + |
|
136 | + $typedResult->fill($entry); |
|
137 | + |
|
138 | + return $typedResult; |
|
139 | + } catch (\PDOException $e) { |
|
140 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
141 | + } |
|
142 | + } |
|
143 | + |
|
144 | + |
|
145 | + /** |
|
146 | + * Set the where condition |
|
147 | + * |
|
148 | + * @param QueryExpression $expression the query expression |
|
149 | + * @return $this |
|
150 | + * @see https://en.wikipedia.org/wiki/SQL#Operators |
|
151 | + * @see https://en.wikipedia.org/wiki/Where_(SQL) |
|
152 | + */ |
|
153 | + public function where(QueryExpression $expression) |
|
154 | + { |
|
155 | + $this->whereExpression = $expression; |
|
156 | + return $this; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Set an additional group by. |
|
161 | + * |
|
162 | + * @param string $column |
|
163 | + * @return $this |
|
164 | + * @see https://en.wikipedia.org/wiki/SQL#Queries |
|
165 | + */ |
|
166 | + public function groupBy($column) |
|
167 | + { |
|
168 | + $this->query->groupBy($column); |
|
169 | + return $this; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Set an additional order condition. |
|
174 | + * |
|
175 | + * @param string $column |
|
176 | + * @param string|null $order |
|
177 | + * @return $this |
|
178 | + * @see https://en.wikipedia.org/wiki/SQL#Queries |
|
179 | + * @see https://en.wikipedia.org/wiki/Order_by |
|
180 | + */ |
|
181 | + public function orderBy($column, $order = null) |
|
182 | + { |
|
183 | + $this->query->orderBy($column, $order); |
|
184 | + return $this; |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Set the limit. |
|
189 | + * |
|
190 | + * @param mixed $limit |
|
191 | + * @return $this |
|
192 | + */ |
|
193 | + public function limit($limit) |
|
194 | + { |
|
195 | + $this->query->limit($limit); |
|
196 | + return $this; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Set the offset. |
|
201 | + * |
|
202 | + * @param mixed $offset |
|
203 | + * @return $this |
|
204 | + */ |
|
205 | + public function offset($offset) |
|
206 | + { |
|
207 | + $this->query->offset($offset); |
|
208 | + return $this; |
|
209 | + } |
|
210 | 210 | } |
@@ -19,72 +19,72 @@ |
||
19 | 19 | interface ActiveRecordInterface |
20 | 20 | { |
21 | 21 | |
22 | - public function __construct(\PDO $pdo); |
|
22 | + public function __construct(\PDO $pdo); |
|
23 | 23 | |
24 | - /** |
|
25 | - * Returns this active record after creating an entry with the records attributes. |
|
26 | - * |
|
27 | - * @return $this |
|
28 | - * @throws ActiveRecordException on failure. |
|
29 | - */ |
|
30 | - public function create(); |
|
24 | + /** |
|
25 | + * Returns this active record after creating an entry with the records attributes. |
|
26 | + * |
|
27 | + * @return $this |
|
28 | + * @throws ActiveRecordException on failure. |
|
29 | + */ |
|
30 | + public function create(); |
|
31 | 31 | |
32 | - /** |
|
33 | - * Returns this active record after reading the attributes from the entry with the given identifier. |
|
34 | - * |
|
35 | - * @param mixed $id |
|
36 | - * @return $this |
|
37 | - * @throws ActiveRecordException on failure. |
|
38 | - */ |
|
39 | - public function read($id); |
|
32 | + /** |
|
33 | + * Returns this active record after reading the attributes from the entry with the given identifier. |
|
34 | + * |
|
35 | + * @param mixed $id |
|
36 | + * @return $this |
|
37 | + * @throws ActiveRecordException on failure. |
|
38 | + */ |
|
39 | + public function read($id); |
|
40 | 40 | |
41 | - /** |
|
42 | - * Returns this active record after updating the attributes to the corresponding entry. |
|
43 | - * |
|
44 | - * @return $this |
|
45 | - * @throws ActiveRecordException on failure. |
|
46 | - */ |
|
47 | - public function update(); |
|
41 | + /** |
|
42 | + * Returns this active record after updating the attributes to the corresponding entry. |
|
43 | + * |
|
44 | + * @return $this |
|
45 | + * @throws ActiveRecordException on failure. |
|
46 | + */ |
|
47 | + public function update(); |
|
48 | 48 | |
49 | - /** |
|
50 | - * Returns this record after deleting the corresponding entry. |
|
51 | - * |
|
52 | - * @return $this |
|
53 | - * @throws ActiveRecordException on failure. |
|
54 | - */ |
|
55 | - public function delete(); |
|
49 | + /** |
|
50 | + * Returns this record after deleting the corresponding entry. |
|
51 | + * |
|
52 | + * @return $this |
|
53 | + * @throws ActiveRecordException on failure. |
|
54 | + */ |
|
55 | + public function delete(); |
|
56 | 56 | |
57 | - /** |
|
58 | - * Returns this record after synchronizing it with the corresponding entry. |
|
59 | - * A new entry is created if this active record does not have a corresponding entry. |
|
60 | - * |
|
61 | - * @return $this |
|
62 | - * @throws ActiveRecordException on failure. |
|
63 | - */ |
|
64 | - public function sync(); |
|
57 | + /** |
|
58 | + * Returns this record after synchronizing it with the corresponding entry. |
|
59 | + * A new entry is created if this active record does not have a corresponding entry. |
|
60 | + * |
|
61 | + * @return $this |
|
62 | + * @throws ActiveRecordException on failure. |
|
63 | + */ |
|
64 | + public function sync(); |
|
65 | 65 | |
66 | - /** |
|
67 | - * Returns true if this active record has a corresponding entry. |
|
68 | - * |
|
69 | - * @return bool true if this active record has a corresponding entry. |
|
70 | - */ |
|
71 | - public function exists(); |
|
66 | + /** |
|
67 | + * Returns true if this active record has a corresponding entry. |
|
68 | + * |
|
69 | + * @return bool true if this active record has a corresponding entry. |
|
70 | + */ |
|
71 | + public function exists(); |
|
72 | 72 | |
73 | - /** |
|
74 | - * Returns this record after filling it with the given attributes. |
|
75 | - * |
|
76 | - * @param array $attributes = [] |
|
77 | - * @return $this |
|
78 | - * @throws ActiveRecordException on failure. |
|
79 | - */ |
|
80 | - public function fill(array $attributes); |
|
73 | + /** |
|
74 | + * Returns this record after filling it with the given attributes. |
|
75 | + * |
|
76 | + * @param array $attributes = [] |
|
77 | + * @return $this |
|
78 | + * @throws ActiveRecordException on failure. |
|
79 | + */ |
|
80 | + public function fill(array $attributes); |
|
81 | 81 | |
82 | - /** |
|
83 | - * Returns the records with the given where, order by, limit and offset clauses. |
|
84 | - * |
|
85 | - * @param array $excludedTraits |
|
86 | - * @return ActiveRecordQuery the query representing the current search. |
|
87 | - * @throws ActiveRecordException on failure. |
|
88 | - */ |
|
89 | - public function search(Array $excludedTraits); |
|
82 | + /** |
|
83 | + * Returns the records with the given where, order by, limit and offset clauses. |
|
84 | + * |
|
85 | + * @param array $excludedTraits |
|
86 | + * @return ActiveRecordQuery the query representing the current search. |
|
87 | + * @throws ActiveRecordException on failure. |
|
88 | + */ |
|
89 | + public function search(Array $excludedTraits); |
|
90 | 90 | } |