@@ -16,283 +16,283 @@ |
||
16 | 16 | class Controller |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * Controller action is publicly accessible |
|
21 | - */ |
|
22 | - const ACTION_PUBLIC = 0; |
|
23 | - |
|
24 | - /** |
|
25 | - * Controller action requires authentication |
|
26 | - * Will redirect to login page if not authenticated |
|
27 | - */ |
|
28 | - const ACTION_PROTECTED = 1; |
|
29 | - |
|
30 | - /** |
|
31 | - * Controller action requires authentication |
|
32 | - * Will result in an 404 if not authenticated |
|
33 | - */ |
|
34 | - const ACTION_PRIVATE = 2; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var array Controller action access rules |
|
38 | - */ |
|
39 | - protected $access = []; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var \PDO PDO |
|
43 | - */ |
|
44 | - protected $pdo; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var string App file path |
|
48 | - */ |
|
49 | - protected $appPath; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var null|User User |
|
53 | - */ |
|
54 | - protected $user; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var string Controller action |
|
58 | - */ |
|
59 | - protected $action; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var array View variables |
|
63 | - */ |
|
64 | - protected $variables = []; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var bool Should render view for controller action |
|
68 | - */ |
|
69 | - protected $render = true; |
|
70 | - |
|
71 | - /** |
|
72 | - * @var string File path for layout template file |
|
73 | - */ |
|
74 | - protected $layout; |
|
75 | - |
|
76 | - /** |
|
77 | - * @var string File path for view template file |
|
78 | - */ |
|
79 | - protected $view; |
|
80 | - |
|
81 | - /** |
|
82 | - * @var array Headers for output |
|
83 | - * |
|
84 | - * @todo JSON content type |
|
85 | - * |
|
86 | - * Content-Type: application/javascript; charset=utf-8 |
|
87 | - */ |
|
88 | - protected $headers = [ |
|
89 | - 'content-type' => 'Content-Type: text/html; charset=utf-8' |
|
90 | - ]; |
|
91 | - |
|
92 | - /** |
|
93 | - * @return string Controller name without namespace |
|
94 | - */ |
|
95 | - public function getShortName() |
|
96 | - { |
|
97 | - return str_replace('Controller', null, (new \ReflectionClass($this))->getShortName()); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @param string $action Controller action |
|
102 | - * |
|
103 | - * @throws ControllerActionNonexistentException |
|
104 | - * @throws ControllerActionPrivateInsufficientAuthenticationException |
|
105 | - * @throws ControllerActionProtectedInsufficientAuthenticationException |
|
106 | - */ |
|
107 | - public function setAction($action) |
|
108 | - { |
|
109 | - /** |
|
110 | - * Check if method exists and that access has been defined |
|
111 | - */ |
|
112 | - if (!method_exists($this, $action) || !isset($this->access[$action])) { |
|
113 | - throw new ControllerActionNonexistentException([get_class($this), $action]); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Check controller action access level if user is not authenticated |
|
118 | - */ |
|
119 | - if (!$this->user) { |
|
120 | - if ($this->access[$action] === self::ACTION_PRIVATE) { |
|
121 | - throw new ControllerActionPrivateInsufficientAuthenticationException([get_class($this), $action]); |
|
122 | - } elseif ($this->access[$action] === self::ACTION_PROTECTED) { |
|
123 | - throw new ControllerActionProtectedInsufficientAuthenticationException([get_class($this), $action]); |
|
124 | - } |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Set controller action |
|
129 | - */ |
|
130 | - $this->action = $action; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Call action |
|
135 | - * |
|
136 | - * @return array<string,array|string>|false Headers and output if render is enabled, otherwise FALSE |
|
137 | - * |
|
138 | - * @throws \LogicException If controller action is not set |
|
139 | - */ |
|
140 | - public function callAction() |
|
141 | - { |
|
142 | - if (!isset($this->action)) { |
|
143 | - throw new \LogicException('Controller action must be set before being called'); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Before controller action hook |
|
148 | - */ |
|
149 | - $this->beforeAction(); |
|
150 | - |
|
151 | - /** |
|
152 | - * Call controller action |
|
153 | - */ |
|
154 | - $action = $this->action; |
|
155 | - |
|
156 | - $this->$action(); |
|
157 | - |
|
158 | - /** |
|
159 | - * After controller action hook |
|
160 | - */ |
|
161 | - $this->afterAction(); |
|
162 | - |
|
163 | - /** |
|
164 | - * Render view |
|
165 | - */ |
|
166 | - if ($this->render) { |
|
167 | - $render = new HtmlRender($this->variables); |
|
168 | - |
|
169 | - $output = $render->render(['_view' => $this->getViewTemplate(), '_layout' => $this->getLayoutTemplate()]); |
|
170 | - |
|
171 | - return [ |
|
172 | - 'headers' => $this->headers, |
|
173 | - 'output' => $output |
|
174 | - ]; |
|
175 | - } |
|
176 | - |
|
177 | - return false; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Before controller action hook |
|
182 | - * |
|
183 | - * Called right before controller action is called |
|
184 | - */ |
|
185 | - protected function beforeAction() |
|
186 | - { |
|
187 | - /** |
|
188 | - * Set New Relic transaction name |
|
189 | - */ |
|
190 | - if (extension_loaded('newrelic')) { |
|
191 | - newrelic_name_transaction(sprintf('%s/%s', $this->getShortName(), $this->action)); |
|
192 | - } |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * After controller action hook |
|
197 | - * |
|
198 | - * Called right after controller action is called, but before rendering of the view |
|
199 | - */ |
|
200 | - protected function afterAction() |
|
201 | - { |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Set view variable |
|
206 | - * |
|
207 | - * @param string $variable |
|
208 | - * @param mixed $value |
|
209 | - */ |
|
210 | - protected function set($variable, $value) |
|
211 | - { |
|
212 | - $this->variables[$variable] = $value; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Get layout template |
|
217 | - * |
|
218 | - * @return string Layout template file path |
|
219 | - */ |
|
220 | - protected function getLayoutTemplate() |
|
221 | - { |
|
222 | - $layout = $this->layout; |
|
223 | - |
|
224 | - if (empty($layout)) { |
|
225 | - $layout = 'View/Layout/default'; |
|
226 | - } |
|
227 | - |
|
228 | - return "{$this->appPath}$layout.layout"; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Get view template |
|
233 | - * |
|
234 | - * @return string View template file path |
|
235 | - */ |
|
236 | - protected function getViewTemplate() |
|
237 | - { |
|
238 | - $view = $this->view; |
|
239 | - |
|
240 | - if (empty($view)) { |
|
241 | - $view = sprintf('View/%s/%s', $this->getShortName(), $this->action); |
|
242 | - } |
|
243 | - |
|
244 | - return "{$this->appPath}$view.view"; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Set response code |
|
249 | - * |
|
250 | - * Supports 200 OK, 403 Forbidden, 404 Not Found & 500 Internal Server Error |
|
251 | - * |
|
252 | - * @param int $code HTTP response code |
|
253 | - * |
|
254 | - * @throws \InvalidArgumentException If unsupported code is provided |
|
255 | - */ |
|
256 | - protected function setResponseCode($code) |
|
257 | - { |
|
258 | - switch ($code) { |
|
259 | - case 200: |
|
260 | - $text = 'OK'; |
|
261 | - break; |
|
262 | - |
|
263 | - case 403: |
|
264 | - $text = 'Forbidden'; |
|
265 | - break; |
|
266 | - |
|
267 | - case 404: |
|
268 | - $text = 'Not Found'; |
|
269 | - break; |
|
270 | - |
|
271 | - case 500: |
|
272 | - $text = 'Internal Server Error'; |
|
273 | - break; |
|
274 | - |
|
275 | - default: |
|
276 | - throw new \InvalidArgumentException("HTTP status '$code' is not implemented"); |
|
277 | - break; |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * Set header |
|
282 | - */ |
|
283 | - // @todo test that running response code multiple times only results in one response code header |
|
284 | - $this->headers['response_code'] = "HTTP/1.1 $code $text"; |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * @param \PDO $pdo |
|
289 | - * @param string $appPath |
|
290 | - * @param null|User $user |
|
291 | - */ |
|
292 | - public function __construct(\PDO $pdo, $appPath, User $user = null) |
|
293 | - { |
|
294 | - $this->pdo = $pdo; |
|
295 | - $this->appPath = $appPath; |
|
296 | - $this->user = $user; |
|
297 | - } |
|
19 | + /** |
|
20 | + * Controller action is publicly accessible |
|
21 | + */ |
|
22 | + const ACTION_PUBLIC = 0; |
|
23 | + |
|
24 | + /** |
|
25 | + * Controller action requires authentication |
|
26 | + * Will redirect to login page if not authenticated |
|
27 | + */ |
|
28 | + const ACTION_PROTECTED = 1; |
|
29 | + |
|
30 | + /** |
|
31 | + * Controller action requires authentication |
|
32 | + * Will result in an 404 if not authenticated |
|
33 | + */ |
|
34 | + const ACTION_PRIVATE = 2; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var array Controller action access rules |
|
38 | + */ |
|
39 | + protected $access = []; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var \PDO PDO |
|
43 | + */ |
|
44 | + protected $pdo; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var string App file path |
|
48 | + */ |
|
49 | + protected $appPath; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var null|User User |
|
53 | + */ |
|
54 | + protected $user; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var string Controller action |
|
58 | + */ |
|
59 | + protected $action; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var array View variables |
|
63 | + */ |
|
64 | + protected $variables = []; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var bool Should render view for controller action |
|
68 | + */ |
|
69 | + protected $render = true; |
|
70 | + |
|
71 | + /** |
|
72 | + * @var string File path for layout template file |
|
73 | + */ |
|
74 | + protected $layout; |
|
75 | + |
|
76 | + /** |
|
77 | + * @var string File path for view template file |
|
78 | + */ |
|
79 | + protected $view; |
|
80 | + |
|
81 | + /** |
|
82 | + * @var array Headers for output |
|
83 | + * |
|
84 | + * @todo JSON content type |
|
85 | + * |
|
86 | + * Content-Type: application/javascript; charset=utf-8 |
|
87 | + */ |
|
88 | + protected $headers = [ |
|
89 | + 'content-type' => 'Content-Type: text/html; charset=utf-8' |
|
90 | + ]; |
|
91 | + |
|
92 | + /** |
|
93 | + * @return string Controller name without namespace |
|
94 | + */ |
|
95 | + public function getShortName() |
|
96 | + { |
|
97 | + return str_replace('Controller', null, (new \ReflectionClass($this))->getShortName()); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @param string $action Controller action |
|
102 | + * |
|
103 | + * @throws ControllerActionNonexistentException |
|
104 | + * @throws ControllerActionPrivateInsufficientAuthenticationException |
|
105 | + * @throws ControllerActionProtectedInsufficientAuthenticationException |
|
106 | + */ |
|
107 | + public function setAction($action) |
|
108 | + { |
|
109 | + /** |
|
110 | + * Check if method exists and that access has been defined |
|
111 | + */ |
|
112 | + if (!method_exists($this, $action) || !isset($this->access[$action])) { |
|
113 | + throw new ControllerActionNonexistentException([get_class($this), $action]); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Check controller action access level if user is not authenticated |
|
118 | + */ |
|
119 | + if (!$this->user) { |
|
120 | + if ($this->access[$action] === self::ACTION_PRIVATE) { |
|
121 | + throw new ControllerActionPrivateInsufficientAuthenticationException([get_class($this), $action]); |
|
122 | + } elseif ($this->access[$action] === self::ACTION_PROTECTED) { |
|
123 | + throw new ControllerActionProtectedInsufficientAuthenticationException([get_class($this), $action]); |
|
124 | + } |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Set controller action |
|
129 | + */ |
|
130 | + $this->action = $action; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Call action |
|
135 | + * |
|
136 | + * @return array<string,array|string>|false Headers and output if render is enabled, otherwise FALSE |
|
137 | + * |
|
138 | + * @throws \LogicException If controller action is not set |
|
139 | + */ |
|
140 | + public function callAction() |
|
141 | + { |
|
142 | + if (!isset($this->action)) { |
|
143 | + throw new \LogicException('Controller action must be set before being called'); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Before controller action hook |
|
148 | + */ |
|
149 | + $this->beforeAction(); |
|
150 | + |
|
151 | + /** |
|
152 | + * Call controller action |
|
153 | + */ |
|
154 | + $action = $this->action; |
|
155 | + |
|
156 | + $this->$action(); |
|
157 | + |
|
158 | + /** |
|
159 | + * After controller action hook |
|
160 | + */ |
|
161 | + $this->afterAction(); |
|
162 | + |
|
163 | + /** |
|
164 | + * Render view |
|
165 | + */ |
|
166 | + if ($this->render) { |
|
167 | + $render = new HtmlRender($this->variables); |
|
168 | + |
|
169 | + $output = $render->render(['_view' => $this->getViewTemplate(), '_layout' => $this->getLayoutTemplate()]); |
|
170 | + |
|
171 | + return [ |
|
172 | + 'headers' => $this->headers, |
|
173 | + 'output' => $output |
|
174 | + ]; |
|
175 | + } |
|
176 | + |
|
177 | + return false; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Before controller action hook |
|
182 | + * |
|
183 | + * Called right before controller action is called |
|
184 | + */ |
|
185 | + protected function beforeAction() |
|
186 | + { |
|
187 | + /** |
|
188 | + * Set New Relic transaction name |
|
189 | + */ |
|
190 | + if (extension_loaded('newrelic')) { |
|
191 | + newrelic_name_transaction(sprintf('%s/%s', $this->getShortName(), $this->action)); |
|
192 | + } |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * After controller action hook |
|
197 | + * |
|
198 | + * Called right after controller action is called, but before rendering of the view |
|
199 | + */ |
|
200 | + protected function afterAction() |
|
201 | + { |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Set view variable |
|
206 | + * |
|
207 | + * @param string $variable |
|
208 | + * @param mixed $value |
|
209 | + */ |
|
210 | + protected function set($variable, $value) |
|
211 | + { |
|
212 | + $this->variables[$variable] = $value; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Get layout template |
|
217 | + * |
|
218 | + * @return string Layout template file path |
|
219 | + */ |
|
220 | + protected function getLayoutTemplate() |
|
221 | + { |
|
222 | + $layout = $this->layout; |
|
223 | + |
|
224 | + if (empty($layout)) { |
|
225 | + $layout = 'View/Layout/default'; |
|
226 | + } |
|
227 | + |
|
228 | + return "{$this->appPath}$layout.layout"; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Get view template |
|
233 | + * |
|
234 | + * @return string View template file path |
|
235 | + */ |
|
236 | + protected function getViewTemplate() |
|
237 | + { |
|
238 | + $view = $this->view; |
|
239 | + |
|
240 | + if (empty($view)) { |
|
241 | + $view = sprintf('View/%s/%s', $this->getShortName(), $this->action); |
|
242 | + } |
|
243 | + |
|
244 | + return "{$this->appPath}$view.view"; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Set response code |
|
249 | + * |
|
250 | + * Supports 200 OK, 403 Forbidden, 404 Not Found & 500 Internal Server Error |
|
251 | + * |
|
252 | + * @param int $code HTTP response code |
|
253 | + * |
|
254 | + * @throws \InvalidArgumentException If unsupported code is provided |
|
255 | + */ |
|
256 | + protected function setResponseCode($code) |
|
257 | + { |
|
258 | + switch ($code) { |
|
259 | + case 200: |
|
260 | + $text = 'OK'; |
|
261 | + break; |
|
262 | + |
|
263 | + case 403: |
|
264 | + $text = 'Forbidden'; |
|
265 | + break; |
|
266 | + |
|
267 | + case 404: |
|
268 | + $text = 'Not Found'; |
|
269 | + break; |
|
270 | + |
|
271 | + case 500: |
|
272 | + $text = 'Internal Server Error'; |
|
273 | + break; |
|
274 | + |
|
275 | + default: |
|
276 | + throw new \InvalidArgumentException("HTTP status '$code' is not implemented"); |
|
277 | + break; |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * Set header |
|
282 | + */ |
|
283 | + // @todo test that running response code multiple times only results in one response code header |
|
284 | + $this->headers['response_code'] = "HTTP/1.1 $code $text"; |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * @param \PDO $pdo |
|
289 | + * @param string $appPath |
|
290 | + * @param null|User $user |
|
291 | + */ |
|
292 | + public function __construct(\PDO $pdo, $appPath, User $user = null) |
|
293 | + { |
|
294 | + $this->pdo = $pdo; |
|
295 | + $this->appPath = $appPath; |
|
296 | + $this->user = $user; |
|
297 | + } |
|
298 | 298 | } |
@@ -13,8 +13,7 @@ discard block |
||
13 | 13 | * |
14 | 14 | * @package Zortje\MVC\Controller |
15 | 15 | */ |
16 | -class Controller |
|
17 | -{ |
|
16 | +class Controller { |
|
18 | 17 | |
19 | 18 | /** |
20 | 19 | * Controller action is publicly accessible |
@@ -92,8 +91,7 @@ discard block |
||
92 | 91 | /** |
93 | 92 | * @return string Controller name without namespace |
94 | 93 | */ |
95 | - public function getShortName() |
|
96 | - { |
|
94 | + public function getShortName() { |
|
97 | 95 | return str_replace('Controller', null, (new \ReflectionClass($this))->getShortName()); |
98 | 96 | } |
99 | 97 | |
@@ -104,8 +102,7 @@ discard block |
||
104 | 102 | * @throws ControllerActionPrivateInsufficientAuthenticationException |
105 | 103 | * @throws ControllerActionProtectedInsufficientAuthenticationException |
106 | 104 | */ |
107 | - public function setAction($action) |
|
108 | - { |
|
105 | + public function setAction($action) { |
|
109 | 106 | /** |
110 | 107 | * Check if method exists and that access has been defined |
111 | 108 | */ |
@@ -137,8 +134,7 @@ discard block |
||
137 | 134 | * |
138 | 135 | * @throws \LogicException If controller action is not set |
139 | 136 | */ |
140 | - public function callAction() |
|
141 | - { |
|
137 | + public function callAction() { |
|
142 | 138 | if (!isset($this->action)) { |
143 | 139 | throw new \LogicException('Controller action must be set before being called'); |
144 | 140 | } |
@@ -182,8 +178,7 @@ discard block |
||
182 | 178 | * |
183 | 179 | * Called right before controller action is called |
184 | 180 | */ |
185 | - protected function beforeAction() |
|
186 | - { |
|
181 | + protected function beforeAction() { |
|
187 | 182 | /** |
188 | 183 | * Set New Relic transaction name |
189 | 184 | */ |
@@ -197,8 +192,7 @@ discard block |
||
197 | 192 | * |
198 | 193 | * Called right after controller action is called, but before rendering of the view |
199 | 194 | */ |
200 | - protected function afterAction() |
|
201 | - { |
|
195 | + protected function afterAction() { |
|
202 | 196 | } |
203 | 197 | |
204 | 198 | /** |
@@ -207,8 +201,7 @@ discard block |
||
207 | 201 | * @param string $variable |
208 | 202 | * @param mixed $value |
209 | 203 | */ |
210 | - protected function set($variable, $value) |
|
211 | - { |
|
204 | + protected function set($variable, $value) { |
|
212 | 205 | $this->variables[$variable] = $value; |
213 | 206 | } |
214 | 207 | |
@@ -217,8 +210,7 @@ discard block |
||
217 | 210 | * |
218 | 211 | * @return string Layout template file path |
219 | 212 | */ |
220 | - protected function getLayoutTemplate() |
|
221 | - { |
|
213 | + protected function getLayoutTemplate() { |
|
222 | 214 | $layout = $this->layout; |
223 | 215 | |
224 | 216 | if (empty($layout)) { |
@@ -233,8 +225,7 @@ discard block |
||
233 | 225 | * |
234 | 226 | * @return string View template file path |
235 | 227 | */ |
236 | - protected function getViewTemplate() |
|
237 | - { |
|
228 | + protected function getViewTemplate() { |
|
238 | 229 | $view = $this->view; |
239 | 230 | |
240 | 231 | if (empty($view)) { |
@@ -253,8 +244,7 @@ discard block |
||
253 | 244 | * |
254 | 245 | * @throws \InvalidArgumentException If unsupported code is provided |
255 | 246 | */ |
256 | - protected function setResponseCode($code) |
|
257 | - { |
|
247 | + protected function setResponseCode($code) { |
|
258 | 248 | switch ($code) { |
259 | 249 | case 200: |
260 | 250 | $text = 'OK'; |
@@ -289,8 +279,7 @@ discard block |
||
289 | 279 | * @param string $appPath |
290 | 280 | * @param null|User $user |
291 | 281 | */ |
292 | - public function __construct(\PDO $pdo, $appPath, User $user = null) |
|
293 | - { |
|
282 | + public function __construct(\PDO $pdo, $appPath, User $user = null) { |
|
294 | 283 | $this->pdo = $pdo; |
295 | 284 | $this->appPath = $appPath; |
296 | 285 | $this->user = $user; |
@@ -14,56 +14,56 @@ |
||
14 | 14 | class ControllerFactory |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * @var \PDO PDO |
|
19 | - */ |
|
20 | - protected $pdo; |
|
17 | + /** |
|
18 | + * @var \PDO PDO |
|
19 | + */ |
|
20 | + protected $pdo; |
|
21 | 21 | |
22 | - /** |
|
23 | - * @var string App file path |
|
24 | - */ |
|
25 | - protected $appPath; |
|
22 | + /** |
|
23 | + * @var string App file path |
|
24 | + */ |
|
25 | + protected $appPath; |
|
26 | 26 | |
27 | - /** |
|
28 | - * @var null|User User |
|
29 | - */ |
|
30 | - protected $user; |
|
27 | + /** |
|
28 | + * @var null|User User |
|
29 | + */ |
|
30 | + protected $user; |
|
31 | 31 | |
32 | - /** |
|
33 | - * Initialize controller |
|
34 | - * |
|
35 | - * @param string $controller Controller class name |
|
36 | - * |
|
37 | - * @return Controller Controller object |
|
38 | - * |
|
39 | - * @throws ControllerInvalidSuperclassException |
|
40 | - * @throws ControllerNonexistentException |
|
41 | - */ |
|
42 | - public function create($controller) |
|
43 | - { |
|
44 | - if (!class_exists($controller)) { |
|
45 | - throw new ControllerNonexistentException([$controller]); |
|
46 | - } elseif (!is_subclass_of($controller, Controller::class)) { |
|
47 | - throw new ControllerInvalidSuperclassException([$controller]); |
|
48 | - } |
|
32 | + /** |
|
33 | + * Initialize controller |
|
34 | + * |
|
35 | + * @param string $controller Controller class name |
|
36 | + * |
|
37 | + * @return Controller Controller object |
|
38 | + * |
|
39 | + * @throws ControllerInvalidSuperclassException |
|
40 | + * @throws ControllerNonexistentException |
|
41 | + */ |
|
42 | + public function create($controller) |
|
43 | + { |
|
44 | + if (!class_exists($controller)) { |
|
45 | + throw new ControllerNonexistentException([$controller]); |
|
46 | + } elseif (!is_subclass_of($controller, Controller::class)) { |
|
47 | + throw new ControllerInvalidSuperclassException([$controller]); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @var Controller $controller |
|
52 | - */ |
|
53 | - $controller = new $controller($this->pdo, $this->appPath, $this->user); |
|
50 | + /** |
|
51 | + * @var Controller $controller |
|
52 | + */ |
|
53 | + $controller = new $controller($this->pdo, $this->appPath, $this->user); |
|
54 | 54 | |
55 | - return $controller; |
|
56 | - } |
|
55 | + return $controller; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param \PDO $pdo |
|
60 | - * @param string $appPath |
|
61 | - * @param null|User $user |
|
62 | - */ |
|
63 | - public function __construct(\PDO $pdo, $appPath, User $user = null) |
|
64 | - { |
|
65 | - $this->pdo = $pdo; |
|
66 | - $this->appPath = $appPath; |
|
67 | - $this->user = $user; |
|
68 | - } |
|
58 | + /** |
|
59 | + * @param \PDO $pdo |
|
60 | + * @param string $appPath |
|
61 | + * @param null|User $user |
|
62 | + */ |
|
63 | + public function __construct(\PDO $pdo, $appPath, User $user = null) |
|
64 | + { |
|
65 | + $this->pdo = $pdo; |
|
66 | + $this->appPath = $appPath; |
|
67 | + $this->user = $user; |
|
68 | + } |
|
69 | 69 | } |
@@ -11,8 +11,7 @@ discard block |
||
11 | 11 | * |
12 | 12 | * @package Zortje\MVC\Controller |
13 | 13 | */ |
14 | -class ControllerFactory |
|
15 | -{ |
|
14 | +class ControllerFactory { |
|
16 | 15 | |
17 | 16 | /** |
18 | 17 | * @var \PDO PDO |
@@ -39,8 +38,7 @@ discard block |
||
39 | 38 | * @throws ControllerInvalidSuperclassException |
40 | 39 | * @throws ControllerNonexistentException |
41 | 40 | */ |
42 | - public function create($controller) |
|
43 | - { |
|
41 | + public function create($controller) { |
|
44 | 42 | if (!class_exists($controller)) { |
45 | 43 | throw new ControllerNonexistentException([$controller]); |
46 | 44 | } elseif (!is_subclass_of($controller, Controller::class)) { |
@@ -60,8 +58,7 @@ discard block |
||
60 | 58 | * @param string $appPath |
61 | 59 | * @param null|User $user |
62 | 60 | */ |
63 | - public function __construct(\PDO $pdo, $appPath, User $user = null) |
|
64 | - { |
|
61 | + public function __construct(\PDO $pdo, $appPath, User $user = null) { |
|
65 | 62 | $this->pdo = $pdo; |
66 | 63 | $this->appPath = $appPath; |
67 | 64 | $this->user = $user; |
@@ -10,12 +10,12 @@ |
||
10 | 10 | class NotFoundController extends Controller |
11 | 11 | { |
12 | 12 | |
13 | - protected $access = [ |
|
14 | - 'index' => Controller::ACTION_PUBLIC |
|
15 | - ]; |
|
13 | + protected $access = [ |
|
14 | + 'index' => Controller::ACTION_PUBLIC |
|
15 | + ]; |
|
16 | 16 | |
17 | - protected function index() |
|
18 | - { |
|
19 | - $this->setResponseCode(404); |
|
20 | - } |
|
17 | + protected function index() |
|
18 | + { |
|
19 | + $this->setResponseCode(404); |
|
20 | + } |
|
21 | 21 | } |
@@ -7,15 +7,13 @@ |
||
7 | 7 | * |
8 | 8 | * @package Zortje\MVC\Controller |
9 | 9 | */ |
10 | -class NotFoundController extends Controller |
|
11 | -{ |
|
10 | +class NotFoundController extends Controller { |
|
12 | 11 | |
13 | 12 | protected $access = [ |
14 | 13 | 'index' => Controller::ACTION_PUBLIC |
15 | 14 | ]; |
16 | 15 | |
17 | - protected function index() |
|
18 | - { |
|
16 | + protected function index() { |
|
19 | 17 | $this->setResponseCode(404); |
20 | 18 | } |
21 | 19 | } |
@@ -10,120 +10,120 @@ |
||
10 | 10 | class SQLCommand |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @var String Table name |
|
15 | - */ |
|
16 | - private $tableName; |
|
17 | - |
|
18 | - /** |
|
19 | - * @var String[] Table columns |
|
20 | - */ |
|
21 | - private $columns; |
|
22 | - |
|
23 | - /** |
|
24 | - * Create INSERT INTO command |
|
25 | - * |
|
26 | - * @return string INSERT INTO query |
|
27 | - */ |
|
28 | - public function insertInto() |
|
29 | - { |
|
30 | - $tableColumnNames = $this->getColumnNames($this->columns); |
|
31 | - |
|
32 | - $columns = $this->columns; |
|
33 | - unset($columns['id']); |
|
34 | - |
|
35 | - $tableColumnValues = $this->getColumnValues($columns); |
|
36 | - |
|
37 | - return "INSERT INTO `$this->tableName` ($tableColumnNames) VALUES (NULL, $tableColumnValues);"; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Create SELECT FROM command |
|
42 | - * |
|
43 | - * @return string SELECT FROM query |
|
44 | - */ |
|
45 | - public function selectFrom() |
|
46 | - { |
|
47 | - $tableColumnNames = $this->getColumnNames($this->columns); |
|
48 | - |
|
49 | - return "SELECT $tableColumnNames FROM `$this->tableName`;"; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Create SELECT FROM command with WHERE |
|
54 | - * |
|
55 | - * @param string|string[] $keys WHERE keys |
|
56 | - * |
|
57 | - * @return string SELECT FROM query |
|
58 | - */ |
|
59 | - public function selectFromWhere($keys) |
|
60 | - { |
|
61 | - $tableColumnNames = $this->getColumnNames($this->columns); |
|
62 | - |
|
63 | - $where = $this->getWhereConditionFromKeys($keys); |
|
64 | - |
|
65 | - return "SELECT $tableColumnNames FROM `$this->tableName` WHERE $where;"; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Get columns names for SQL command |
|
70 | - * |
|
71 | - * @param String[] $columns |
|
72 | - * |
|
73 | - * @return string Column names for column list |
|
74 | - */ |
|
75 | - protected function getColumnNames($columns) |
|
76 | - { |
|
77 | - $tableColumnNames = implode('`, `', array_keys($columns)); |
|
78 | - |
|
79 | - return "`{$tableColumnNames}`"; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Get columns values for SQL command |
|
84 | - * |
|
85 | - * @param String[] $columns |
|
86 | - * |
|
87 | - * @return string Column names for column values |
|
88 | - */ |
|
89 | - protected function getColumnValues($columns) |
|
90 | - { |
|
91 | - $tableColumnValues = implode(', :', array_keys($columns)); |
|
92 | - |
|
93 | - return ":{$tableColumnValues}"; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Get WHERE condition for SQL command |
|
98 | - * |
|
99 | - * @param string|string[] $keys |
|
100 | - * |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - protected function getWhereConditionFromKeys($keys) |
|
104 | - { |
|
105 | - $where = []; |
|
106 | - |
|
107 | - if (is_string($keys)) { |
|
108 | - $keys = [$keys]; |
|
109 | - } elseif (!is_array($keys)) { |
|
110 | - throw new \InvalidArgumentException('Keys must be a string or an array of strings'); |
|
111 | - } |
|
112 | - |
|
113 | - foreach ($keys as $key) { |
|
114 | - $where[] = "`$key` = :$key"; |
|
115 | - } |
|
116 | - |
|
117 | - return implode(' AND ', $where); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @param String $tableName |
|
122 | - * @param String[] $columns |
|
123 | - */ |
|
124 | - public function __construct($tableName, $columns) |
|
125 | - { |
|
126 | - $this->tableName = $tableName; |
|
127 | - $this->columns = $columns; |
|
128 | - } |
|
13 | + /** |
|
14 | + * @var String Table name |
|
15 | + */ |
|
16 | + private $tableName; |
|
17 | + |
|
18 | + /** |
|
19 | + * @var String[] Table columns |
|
20 | + */ |
|
21 | + private $columns; |
|
22 | + |
|
23 | + /** |
|
24 | + * Create INSERT INTO command |
|
25 | + * |
|
26 | + * @return string INSERT INTO query |
|
27 | + */ |
|
28 | + public function insertInto() |
|
29 | + { |
|
30 | + $tableColumnNames = $this->getColumnNames($this->columns); |
|
31 | + |
|
32 | + $columns = $this->columns; |
|
33 | + unset($columns['id']); |
|
34 | + |
|
35 | + $tableColumnValues = $this->getColumnValues($columns); |
|
36 | + |
|
37 | + return "INSERT INTO `$this->tableName` ($tableColumnNames) VALUES (NULL, $tableColumnValues);"; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Create SELECT FROM command |
|
42 | + * |
|
43 | + * @return string SELECT FROM query |
|
44 | + */ |
|
45 | + public function selectFrom() |
|
46 | + { |
|
47 | + $tableColumnNames = $this->getColumnNames($this->columns); |
|
48 | + |
|
49 | + return "SELECT $tableColumnNames FROM `$this->tableName`;"; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Create SELECT FROM command with WHERE |
|
54 | + * |
|
55 | + * @param string|string[] $keys WHERE keys |
|
56 | + * |
|
57 | + * @return string SELECT FROM query |
|
58 | + */ |
|
59 | + public function selectFromWhere($keys) |
|
60 | + { |
|
61 | + $tableColumnNames = $this->getColumnNames($this->columns); |
|
62 | + |
|
63 | + $where = $this->getWhereConditionFromKeys($keys); |
|
64 | + |
|
65 | + return "SELECT $tableColumnNames FROM `$this->tableName` WHERE $where;"; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Get columns names for SQL command |
|
70 | + * |
|
71 | + * @param String[] $columns |
|
72 | + * |
|
73 | + * @return string Column names for column list |
|
74 | + */ |
|
75 | + protected function getColumnNames($columns) |
|
76 | + { |
|
77 | + $tableColumnNames = implode('`, `', array_keys($columns)); |
|
78 | + |
|
79 | + return "`{$tableColumnNames}`"; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Get columns values for SQL command |
|
84 | + * |
|
85 | + * @param String[] $columns |
|
86 | + * |
|
87 | + * @return string Column names for column values |
|
88 | + */ |
|
89 | + protected function getColumnValues($columns) |
|
90 | + { |
|
91 | + $tableColumnValues = implode(', :', array_keys($columns)); |
|
92 | + |
|
93 | + return ":{$tableColumnValues}"; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Get WHERE condition for SQL command |
|
98 | + * |
|
99 | + * @param string|string[] $keys |
|
100 | + * |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + protected function getWhereConditionFromKeys($keys) |
|
104 | + { |
|
105 | + $where = []; |
|
106 | + |
|
107 | + if (is_string($keys)) { |
|
108 | + $keys = [$keys]; |
|
109 | + } elseif (!is_array($keys)) { |
|
110 | + throw new \InvalidArgumentException('Keys must be a string or an array of strings'); |
|
111 | + } |
|
112 | + |
|
113 | + foreach ($keys as $key) { |
|
114 | + $where[] = "`$key` = :$key"; |
|
115 | + } |
|
116 | + |
|
117 | + return implode(' AND ', $where); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @param String $tableName |
|
122 | + * @param String[] $columns |
|
123 | + */ |
|
124 | + public function __construct($tableName, $columns) |
|
125 | + { |
|
126 | + $this->tableName = $tableName; |
|
127 | + $this->columns = $columns; |
|
128 | + } |
|
129 | 129 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | * |
8 | 8 | * @package Zortje\MVC\Model |
9 | 9 | */ |
10 | -class SQLCommand |
|
11 | -{ |
|
10 | +class SQLCommand { |
|
12 | 11 | |
13 | 12 | /** |
14 | 13 | * @var String Table name |
@@ -25,8 +24,7 @@ discard block |
||
25 | 24 | * |
26 | 25 | * @return string INSERT INTO query |
27 | 26 | */ |
28 | - public function insertInto() |
|
29 | - { |
|
27 | + public function insertInto() { |
|
30 | 28 | $tableColumnNames = $this->getColumnNames($this->columns); |
31 | 29 | |
32 | 30 | $columns = $this->columns; |
@@ -42,8 +40,7 @@ discard block |
||
42 | 40 | * |
43 | 41 | * @return string SELECT FROM query |
44 | 42 | */ |
45 | - public function selectFrom() |
|
46 | - { |
|
43 | + public function selectFrom() { |
|
47 | 44 | $tableColumnNames = $this->getColumnNames($this->columns); |
48 | 45 | |
49 | 46 | return "SELECT $tableColumnNames FROM `$this->tableName`;"; |
@@ -56,8 +53,7 @@ discard block |
||
56 | 53 | * |
57 | 54 | * @return string SELECT FROM query |
58 | 55 | */ |
59 | - public function selectFromWhere($keys) |
|
60 | - { |
|
56 | + public function selectFromWhere($keys) { |
|
61 | 57 | $tableColumnNames = $this->getColumnNames($this->columns); |
62 | 58 | |
63 | 59 | $where = $this->getWhereConditionFromKeys($keys); |
@@ -72,8 +68,7 @@ discard block |
||
72 | 68 | * |
73 | 69 | * @return string Column names for column list |
74 | 70 | */ |
75 | - protected function getColumnNames($columns) |
|
76 | - { |
|
71 | + protected function getColumnNames($columns) { |
|
77 | 72 | $tableColumnNames = implode('`, `', array_keys($columns)); |
78 | 73 | |
79 | 74 | return "`{$tableColumnNames}`"; |
@@ -86,8 +81,7 @@ discard block |
||
86 | 81 | * |
87 | 82 | * @return string Column names for column values |
88 | 83 | */ |
89 | - protected function getColumnValues($columns) |
|
90 | - { |
|
84 | + protected function getColumnValues($columns) { |
|
91 | 85 | $tableColumnValues = implode(', :', array_keys($columns)); |
92 | 86 | |
93 | 87 | return ":{$tableColumnValues}"; |
@@ -100,8 +94,7 @@ discard block |
||
100 | 94 | * |
101 | 95 | * @return string |
102 | 96 | */ |
103 | - protected function getWhereConditionFromKeys($keys) |
|
104 | - { |
|
97 | + protected function getWhereConditionFromKeys($keys) { |
|
105 | 98 | $where = []; |
106 | 99 | |
107 | 100 | if (is_string($keys)) { |
@@ -121,8 +114,7 @@ discard block |
||
121 | 114 | * @param String $tableName |
122 | 115 | * @param String[] $columns |
123 | 116 | */ |
124 | - public function __construct($tableName, $columns) |
|
125 | - { |
|
117 | + public function __construct($tableName, $columns) { |
|
126 | 118 | $this->tableName = $tableName; |
127 | 119 | $this->columns = $columns; |
128 | 120 | } |
@@ -13,165 +13,165 @@ |
||
13 | 13 | abstract class Entity |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * @var array Columns |
|
18 | - */ |
|
19 | - protected static $columns = []; |
|
20 | - |
|
21 | - /** |
|
22 | - * @var array Internal entity properties |
|
23 | - */ |
|
24 | - protected $properties = []; |
|
25 | - |
|
26 | - /** |
|
27 | - * Get entity columns |
|
28 | - * |
|
29 | - * @return array Entity columns |
|
30 | - */ |
|
31 | - public static function getColumns() |
|
32 | - { |
|
33 | - $columns = array_merge([ |
|
34 | - 'id' => 'integer' |
|
35 | - ], static::$columns); |
|
36 | - |
|
37 | - $columns = array_merge($columns, [ |
|
38 | - 'modified' => 'DateTime', |
|
39 | - 'created' => 'DateTime' |
|
40 | - ]); |
|
41 | - |
|
42 | - return $columns; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Set entity property |
|
47 | - * |
|
48 | - * @param string $key Entity property name |
|
49 | - * @param object|integer|double|string|array|boolean|null $value Entity property value |
|
50 | - * |
|
51 | - * @throws InvalidEntityPropertyException If entity does not have that property |
|
52 | - * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
|
53 | - */ |
|
54 | - public function set($key, $value) |
|
55 | - { |
|
56 | - if (!isset(self::getColumns()[$key])) { |
|
57 | - throw new InvalidEntityPropertyException([get_class($this), $key]); |
|
58 | - } |
|
59 | - |
|
60 | - $this->properties[$key] = $this->validatePropertyForValue($key, $value); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Get entity property |
|
65 | - * |
|
66 | - * @param string $key Entity property |
|
67 | - * |
|
68 | - * @return object|integer|double|string|array|boolean|null Entity property value for given key |
|
69 | - * |
|
70 | - * @throws InvalidEntityPropertyException If entity does not have that property |
|
71 | - */ |
|
72 | - public function get($key) |
|
73 | - { |
|
74 | - if (!isset(self::getColumns()[$key])) { |
|
75 | - throw new InvalidEntityPropertyException([get_class($this), $key]); |
|
76 | - } |
|
77 | - |
|
78 | - return $this->properties[$key]; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Return table structur for saving |
|
83 | - * Example: `['table_field_name' => $this->fieldName]` |
|
84 | - * |
|
85 | - * @param bool $includeId Should the ID column be included |
|
86 | - * |
|
87 | - * @return array |
|
88 | - */ |
|
89 | - public function toArray($includeId) |
|
90 | - { |
|
91 | - $array = []; |
|
92 | - |
|
93 | - foreach (self::getColumns() as $column => $type) { |
|
94 | - if ($column === 'id' && !$includeId) { |
|
95 | - continue; |
|
96 | - } |
|
97 | - |
|
98 | - $property = new EntityProperty($type); |
|
99 | - |
|
100 | - $value = $this->get($column); |
|
101 | - $value = $property->formatValueForDatabase($value); |
|
102 | - |
|
103 | - $array[":$column"] = $value; |
|
104 | - } |
|
105 | - |
|
106 | - return $array; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Validate property for given value |
|
111 | - * |
|
112 | - * @param string $key Entity property name |
|
113 | - * @param mixed $value Entity property value |
|
114 | - * |
|
115 | - * @return object|integer|double|string|array|boolean|null Value |
|
116 | - * |
|
117 | - * @throws InvalidEntityPropertyException If entity does not have that property |
|
118 | - * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
|
119 | - */ |
|
120 | - protected function validatePropertyForValue($key, $value) |
|
121 | - { |
|
122 | - if (!isset(self::getColumns()[$key])) { |
|
123 | - throw new InvalidEntityPropertyException([get_class($this), $key]); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Allow NULL |
|
128 | - */ |
|
129 | - if (!is_null($value)) { |
|
130 | - $valueType = gettype($value); |
|
131 | - |
|
132 | - /** |
|
133 | - * Get class if object |
|
134 | - */ |
|
135 | - if ($valueType === 'object') { |
|
136 | - $valueType = get_class($value); |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Handle alias types |
|
141 | - */ |
|
142 | - $columnType = self::getColumns()[$key]; |
|
143 | - |
|
144 | - switch ($columnType) { |
|
145 | - case 'Date': |
|
146 | - $columnType = 'DateTime'; |
|
147 | - break; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Validate type |
|
152 | - */ |
|
153 | - if ($valueType !== $columnType) { |
|
154 | - throw new InvalidValueTypeForEntityPropertyException([ |
|
155 | - get_class($this), |
|
156 | - $key, |
|
157 | - $valueType, |
|
158 | - $columnType |
|
159 | - ]); |
|
160 | - } |
|
161 | - } |
|
162 | - |
|
163 | - return $value; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @param int $id |
|
168 | - * @param \DateTime $modified |
|
169 | - * @param \DateTime $created |
|
170 | - */ |
|
171 | - public function __construct($id, \DateTime $modified, \DateTime $created) |
|
172 | - { |
|
173 | - $this->set('id', $id); |
|
174 | - $this->set('modified', $modified); |
|
175 | - $this->set('created', $created); |
|
176 | - } |
|
16 | + /** |
|
17 | + * @var array Columns |
|
18 | + */ |
|
19 | + protected static $columns = []; |
|
20 | + |
|
21 | + /** |
|
22 | + * @var array Internal entity properties |
|
23 | + */ |
|
24 | + protected $properties = []; |
|
25 | + |
|
26 | + /** |
|
27 | + * Get entity columns |
|
28 | + * |
|
29 | + * @return array Entity columns |
|
30 | + */ |
|
31 | + public static function getColumns() |
|
32 | + { |
|
33 | + $columns = array_merge([ |
|
34 | + 'id' => 'integer' |
|
35 | + ], static::$columns); |
|
36 | + |
|
37 | + $columns = array_merge($columns, [ |
|
38 | + 'modified' => 'DateTime', |
|
39 | + 'created' => 'DateTime' |
|
40 | + ]); |
|
41 | + |
|
42 | + return $columns; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Set entity property |
|
47 | + * |
|
48 | + * @param string $key Entity property name |
|
49 | + * @param object|integer|double|string|array|boolean|null $value Entity property value |
|
50 | + * |
|
51 | + * @throws InvalidEntityPropertyException If entity does not have that property |
|
52 | + * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
|
53 | + */ |
|
54 | + public function set($key, $value) |
|
55 | + { |
|
56 | + if (!isset(self::getColumns()[$key])) { |
|
57 | + throw new InvalidEntityPropertyException([get_class($this), $key]); |
|
58 | + } |
|
59 | + |
|
60 | + $this->properties[$key] = $this->validatePropertyForValue($key, $value); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Get entity property |
|
65 | + * |
|
66 | + * @param string $key Entity property |
|
67 | + * |
|
68 | + * @return object|integer|double|string|array|boolean|null Entity property value for given key |
|
69 | + * |
|
70 | + * @throws InvalidEntityPropertyException If entity does not have that property |
|
71 | + */ |
|
72 | + public function get($key) |
|
73 | + { |
|
74 | + if (!isset(self::getColumns()[$key])) { |
|
75 | + throw new InvalidEntityPropertyException([get_class($this), $key]); |
|
76 | + } |
|
77 | + |
|
78 | + return $this->properties[$key]; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Return table structur for saving |
|
83 | + * Example: `['table_field_name' => $this->fieldName]` |
|
84 | + * |
|
85 | + * @param bool $includeId Should the ID column be included |
|
86 | + * |
|
87 | + * @return array |
|
88 | + */ |
|
89 | + public function toArray($includeId) |
|
90 | + { |
|
91 | + $array = []; |
|
92 | + |
|
93 | + foreach (self::getColumns() as $column => $type) { |
|
94 | + if ($column === 'id' && !$includeId) { |
|
95 | + continue; |
|
96 | + } |
|
97 | + |
|
98 | + $property = new EntityProperty($type); |
|
99 | + |
|
100 | + $value = $this->get($column); |
|
101 | + $value = $property->formatValueForDatabase($value); |
|
102 | + |
|
103 | + $array[":$column"] = $value; |
|
104 | + } |
|
105 | + |
|
106 | + return $array; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Validate property for given value |
|
111 | + * |
|
112 | + * @param string $key Entity property name |
|
113 | + * @param mixed $value Entity property value |
|
114 | + * |
|
115 | + * @return object|integer|double|string|array|boolean|null Value |
|
116 | + * |
|
117 | + * @throws InvalidEntityPropertyException If entity does not have that property |
|
118 | + * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
|
119 | + */ |
|
120 | + protected function validatePropertyForValue($key, $value) |
|
121 | + { |
|
122 | + if (!isset(self::getColumns()[$key])) { |
|
123 | + throw new InvalidEntityPropertyException([get_class($this), $key]); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Allow NULL |
|
128 | + */ |
|
129 | + if (!is_null($value)) { |
|
130 | + $valueType = gettype($value); |
|
131 | + |
|
132 | + /** |
|
133 | + * Get class if object |
|
134 | + */ |
|
135 | + if ($valueType === 'object') { |
|
136 | + $valueType = get_class($value); |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Handle alias types |
|
141 | + */ |
|
142 | + $columnType = self::getColumns()[$key]; |
|
143 | + |
|
144 | + switch ($columnType) { |
|
145 | + case 'Date': |
|
146 | + $columnType = 'DateTime'; |
|
147 | + break; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Validate type |
|
152 | + */ |
|
153 | + if ($valueType !== $columnType) { |
|
154 | + throw new InvalidValueTypeForEntityPropertyException([ |
|
155 | + get_class($this), |
|
156 | + $key, |
|
157 | + $valueType, |
|
158 | + $columnType |
|
159 | + ]); |
|
160 | + } |
|
161 | + } |
|
162 | + |
|
163 | + return $value; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @param int $id |
|
168 | + * @param \DateTime $modified |
|
169 | + * @param \DateTime $created |
|
170 | + */ |
|
171 | + public function __construct($id, \DateTime $modified, \DateTime $created) |
|
172 | + { |
|
173 | + $this->set('id', $id); |
|
174 | + $this->set('modified', $modified); |
|
175 | + $this->set('created', $created); |
|
176 | + } |
|
177 | 177 | } |
@@ -10,8 +10,7 @@ discard block |
||
10 | 10 | * |
11 | 11 | * @package Zortje\MVC\Model\Table\Entity |
12 | 12 | */ |
13 | -abstract class Entity |
|
14 | -{ |
|
13 | +abstract class Entity { |
|
15 | 14 | |
16 | 15 | /** |
17 | 16 | * @var array Columns |
@@ -28,8 +27,7 @@ discard block |
||
28 | 27 | * |
29 | 28 | * @return array Entity columns |
30 | 29 | */ |
31 | - public static function getColumns() |
|
32 | - { |
|
30 | + public static function getColumns() { |
|
33 | 31 | $columns = array_merge([ |
34 | 32 | 'id' => 'integer' |
35 | 33 | ], static::$columns); |
@@ -51,8 +49,7 @@ discard block |
||
51 | 49 | * @throws InvalidEntityPropertyException If entity does not have that property |
52 | 50 | * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
53 | 51 | */ |
54 | - public function set($key, $value) |
|
55 | - { |
|
52 | + public function set($key, $value) { |
|
56 | 53 | if (!isset(self::getColumns()[$key])) { |
57 | 54 | throw new InvalidEntityPropertyException([get_class($this), $key]); |
58 | 55 | } |
@@ -69,8 +66,7 @@ discard block |
||
69 | 66 | * |
70 | 67 | * @throws InvalidEntityPropertyException If entity does not have that property |
71 | 68 | */ |
72 | - public function get($key) |
|
73 | - { |
|
69 | + public function get($key) { |
|
74 | 70 | if (!isset(self::getColumns()[$key])) { |
75 | 71 | throw new InvalidEntityPropertyException([get_class($this), $key]); |
76 | 72 | } |
@@ -86,8 +82,7 @@ discard block |
||
86 | 82 | * |
87 | 83 | * @return array |
88 | 84 | */ |
89 | - public function toArray($includeId) |
|
90 | - { |
|
85 | + public function toArray($includeId) { |
|
91 | 86 | $array = []; |
92 | 87 | |
93 | 88 | foreach (self::getColumns() as $column => $type) { |
@@ -117,8 +112,7 @@ discard block |
||
117 | 112 | * @throws InvalidEntityPropertyException If entity does not have that property |
118 | 113 | * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
119 | 114 | */ |
120 | - protected function validatePropertyForValue($key, $value) |
|
121 | - { |
|
115 | + protected function validatePropertyForValue($key, $value) { |
|
122 | 116 | if (!isset(self::getColumns()[$key])) { |
123 | 117 | throw new InvalidEntityPropertyException([get_class($this), $key]); |
124 | 118 | } |
@@ -168,8 +162,7 @@ discard block |
||
168 | 162 | * @param \DateTime $modified |
169 | 163 | * @param \DateTime $created |
170 | 164 | */ |
171 | - public function __construct($id, \DateTime $modified, \DateTime $created) |
|
172 | - { |
|
165 | + public function __construct($id, \DateTime $modified, \DateTime $created) { |
|
173 | 166 | $this->set('id', $id); |
174 | 167 | $this->set('modified', $modified); |
175 | 168 | $this->set('created', $created); |
@@ -10,49 +10,49 @@ |
||
10 | 10 | class EntityFactory |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @var String Entity class |
|
15 | - */ |
|
16 | - protected $entityClass; |
|
17 | - |
|
18 | - /** |
|
19 | - * @param array $array |
|
20 | - * |
|
21 | - * @return object |
|
22 | - */ |
|
23 | - public function createFromArray(array $array) |
|
24 | - { |
|
25 | - /** |
|
26 | - * @var Entity $entity |
|
27 | - */ |
|
28 | - $reflector = new \ReflectionClass($this->entityClass); |
|
29 | - |
|
30 | - $entity = $reflector->newInstanceWithoutConstructor(); |
|
31 | - |
|
32 | - $columns = $entity::getColumns(); |
|
33 | - unset($columns['id'], $columns['modified'], $columns['created']); |
|
34 | - |
|
35 | - $arguments = []; |
|
36 | - |
|
37 | - foreach ($columns as $column => $type) { |
|
38 | - $property = new EntityProperty($type); |
|
39 | - |
|
40 | - $arguments[$column] = $property->formatValueForEntity($array[$column]); |
|
41 | - } |
|
42 | - |
|
43 | - $entity = $reflector->newInstanceArgs($arguments); |
|
44 | - $entity->set('id', (int) $array['id']); |
|
45 | - $entity->set('modified', new \DateTime($array['modified'])); |
|
46 | - $entity->set('created', new \DateTime($array['created'])); |
|
47 | - |
|
48 | - return $entity; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @param string $entityClass |
|
53 | - */ |
|
54 | - public function __construct($entityClass) |
|
55 | - { |
|
56 | - $this->entityClass = $entityClass; |
|
57 | - } |
|
13 | + /** |
|
14 | + * @var String Entity class |
|
15 | + */ |
|
16 | + protected $entityClass; |
|
17 | + |
|
18 | + /** |
|
19 | + * @param array $array |
|
20 | + * |
|
21 | + * @return object |
|
22 | + */ |
|
23 | + public function createFromArray(array $array) |
|
24 | + { |
|
25 | + /** |
|
26 | + * @var Entity $entity |
|
27 | + */ |
|
28 | + $reflector = new \ReflectionClass($this->entityClass); |
|
29 | + |
|
30 | + $entity = $reflector->newInstanceWithoutConstructor(); |
|
31 | + |
|
32 | + $columns = $entity::getColumns(); |
|
33 | + unset($columns['id'], $columns['modified'], $columns['created']); |
|
34 | + |
|
35 | + $arguments = []; |
|
36 | + |
|
37 | + foreach ($columns as $column => $type) { |
|
38 | + $property = new EntityProperty($type); |
|
39 | + |
|
40 | + $arguments[$column] = $property->formatValueForEntity($array[$column]); |
|
41 | + } |
|
42 | + |
|
43 | + $entity = $reflector->newInstanceArgs($arguments); |
|
44 | + $entity->set('id', (int) $array['id']); |
|
45 | + $entity->set('modified', new \DateTime($array['modified'])); |
|
46 | + $entity->set('created', new \DateTime($array['created'])); |
|
47 | + |
|
48 | + return $entity; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @param string $entityClass |
|
53 | + */ |
|
54 | + public function __construct($entityClass) |
|
55 | + { |
|
56 | + $this->entityClass = $entityClass; |
|
57 | + } |
|
58 | 58 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | * |
8 | 8 | * @package Zortje\MVC\Model\Table\Entity |
9 | 9 | */ |
10 | -class EntityFactory |
|
11 | -{ |
|
10 | +class EntityFactory { |
|
12 | 11 | |
13 | 12 | /** |
14 | 13 | * @var String Entity class |
@@ -20,8 +19,7 @@ discard block |
||
20 | 19 | * |
21 | 20 | * @return object |
22 | 21 | */ |
23 | - public function createFromArray(array $array) |
|
24 | - { |
|
22 | + public function createFromArray(array $array) { |
|
25 | 23 | /** |
26 | 24 | * @var Entity $entity |
27 | 25 | */ |
@@ -51,8 +49,7 @@ discard block |
||
51 | 49 | /** |
52 | 50 | * @param string $entityClass |
53 | 51 | */ |
54 | - public function __construct($entityClass) |
|
55 | - { |
|
52 | + public function __construct($entityClass) { |
|
56 | 53 | $this->entityClass = $entityClass; |
57 | 54 | } |
58 | 55 | } |
@@ -10,75 +10,75 @@ |
||
10 | 10 | class EntityProperty |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * @var string Entity property type |
|
15 | - */ |
|
16 | - protected $type; |
|
13 | + /** |
|
14 | + * @var string Entity property type |
|
15 | + */ |
|
16 | + protected $type; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Format value according to entity property type |
|
20 | - * |
|
21 | - * @param mixed $value Value |
|
22 | - * |
|
23 | - * @return mixed Value |
|
24 | - */ |
|
25 | - public function formatValueForEntity($value) |
|
26 | - { |
|
27 | - switch ($this->type) { |
|
28 | - case 'string': |
|
29 | - $value = "$value"; |
|
30 | - break; |
|
18 | + /** |
|
19 | + * Format value according to entity property type |
|
20 | + * |
|
21 | + * @param mixed $value Value |
|
22 | + * |
|
23 | + * @return mixed Value |
|
24 | + */ |
|
25 | + public function formatValueForEntity($value) |
|
26 | + { |
|
27 | + switch ($this->type) { |
|
28 | + case 'string': |
|
29 | + $value = "$value"; |
|
30 | + break; |
|
31 | 31 | |
32 | - case 'integer': |
|
33 | - $value = (int) $value; |
|
34 | - break; |
|
32 | + case 'integer': |
|
33 | + $value = (int) $value; |
|
34 | + break; |
|
35 | 35 | |
36 | - case 'float': |
|
37 | - $value = (float) $value; |
|
38 | - break; |
|
36 | + case 'float': |
|
37 | + $value = (float) $value; |
|
38 | + break; |
|
39 | 39 | |
40 | - case 'Date': |
|
41 | - case 'DateTime': |
|
42 | - $value = new \DateTime($value); |
|
43 | - break; |
|
44 | - } |
|
40 | + case 'Date': |
|
41 | + case 'DateTime': |
|
42 | + $value = new \DateTime($value); |
|
43 | + break; |
|
44 | + } |
|
45 | 45 | |
46 | - return $value; |
|
47 | - } |
|
46 | + return $value; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Format value for insertion into the database |
|
51 | - * |
|
52 | - * @param mixed $value Value |
|
53 | - * |
|
54 | - * @return mixed Value |
|
55 | - */ |
|
56 | - public function formatValueForDatabase($value) |
|
57 | - { |
|
58 | - switch ($this->type) { |
|
59 | - case 'Date': |
|
60 | - /** |
|
61 | - * @var \DateTime $value |
|
62 | - */ |
|
63 | - $value = $value->format('Y-m-d'); |
|
64 | - break; |
|
49 | + /** |
|
50 | + * Format value for insertion into the database |
|
51 | + * |
|
52 | + * @param mixed $value Value |
|
53 | + * |
|
54 | + * @return mixed Value |
|
55 | + */ |
|
56 | + public function formatValueForDatabase($value) |
|
57 | + { |
|
58 | + switch ($this->type) { |
|
59 | + case 'Date': |
|
60 | + /** |
|
61 | + * @var \DateTime $value |
|
62 | + */ |
|
63 | + $value = $value->format('Y-m-d'); |
|
64 | + break; |
|
65 | 65 | |
66 | - case 'DateTime': |
|
67 | - /** |
|
68 | - * @var \DateTime $value |
|
69 | - */ |
|
70 | - $value = $value->format('Y-m-d H:i:s'); |
|
71 | - break; |
|
72 | - } |
|
66 | + case 'DateTime': |
|
67 | + /** |
|
68 | + * @var \DateTime $value |
|
69 | + */ |
|
70 | + $value = $value->format('Y-m-d H:i:s'); |
|
71 | + break; |
|
72 | + } |
|
73 | 73 | |
74 | - return $value; |
|
75 | - } |
|
74 | + return $value; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * @param string $type |
|
79 | - */ |
|
80 | - public function __construct($type) |
|
81 | - { |
|
82 | - $this->type = $type; |
|
83 | - } |
|
77 | + /** |
|
78 | + * @param string $type |
|
79 | + */ |
|
80 | + public function __construct($type) |
|
81 | + { |
|
82 | + $this->type = $type; |
|
83 | + } |
|
84 | 84 | } |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | * |
8 | 8 | * @package Zortje\MVC\Model\Table\Entity |
9 | 9 | */ |
10 | -class EntityProperty |
|
11 | -{ |
|
10 | +class EntityProperty { |
|
12 | 11 | |
13 | 12 | /** |
14 | 13 | * @var string Entity property type |
@@ -22,8 +21,7 @@ discard block |
||
22 | 21 | * |
23 | 22 | * @return mixed Value |
24 | 23 | */ |
25 | - public function formatValueForEntity($value) |
|
26 | - { |
|
24 | + public function formatValueForEntity($value) { |
|
27 | 25 | switch ($this->type) { |
28 | 26 | case 'string': |
29 | 27 | $value = "$value"; |
@@ -53,8 +51,7 @@ discard block |
||
53 | 51 | * |
54 | 52 | * @return mixed Value |
55 | 53 | */ |
56 | - public function formatValueForDatabase($value) |
|
57 | - { |
|
54 | + public function formatValueForDatabase($value) { |
|
58 | 55 | switch ($this->type) { |
59 | 56 | case 'Date': |
60 | 57 | /** |
@@ -77,8 +74,7 @@ discard block |
||
77 | 74 | /** |
78 | 75 | * @param string $type |
79 | 76 | */ |
80 | - public function __construct($type) |
|
81 | - { |
|
77 | + public function __construct($type) { |
|
82 | 78 | $this->type = $type; |
83 | 79 | } |
84 | 80 | } |
@@ -12,16 +12,16 @@ |
||
12 | 12 | class EntityClassNonexistentException extends Exception |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * {@inheritdoc} |
|
17 | - */ |
|
18 | - protected $template = 'Subclass %s defined entity class %s is nonexistent'; |
|
15 | + /** |
|
16 | + * {@inheritdoc} |
|
17 | + */ |
|
18 | + protected $template = 'Subclass %s defined entity class %s is nonexistent'; |
|
19 | 19 | |
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - public function __construct($message) |
|
24 | - { |
|
25 | - parent::__construct($message); |
|
26 | - } |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + public function __construct($message) |
|
24 | + { |
|
25 | + parent::__construct($message); |
|
26 | + } |
|
27 | 27 | } |
@@ -9,8 +9,7 @@ discard block |
||
9 | 9 | * |
10 | 10 | * @package Zortje\MVC\Model\Table\Entity\Exception |
11 | 11 | */ |
12 | -class EntityClassNonexistentException extends Exception |
|
13 | -{ |
|
12 | +class EntityClassNonexistentException extends Exception { |
|
14 | 13 | |
15 | 14 | /** |
16 | 15 | * {@inheritdoc} |
@@ -20,8 +19,7 @@ discard block |
||
20 | 19 | /** |
21 | 20 | * {@inheritdoc} |
22 | 21 | */ |
23 | - public function __construct($message) |
|
24 | - { |
|
22 | + public function __construct($message) { |
|
25 | 23 | parent::__construct($message); |
26 | 24 | } |
27 | 25 | } |
@@ -12,16 +12,16 @@ |
||
12 | 12 | class EntityClassNotDefinedException extends Exception |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * {@inheritdoc} |
|
17 | - */ |
|
18 | - protected $template = 'Subclass %s does not have a entity class defined'; |
|
15 | + /** |
|
16 | + * {@inheritdoc} |
|
17 | + */ |
|
18 | + protected $template = 'Subclass %s does not have a entity class defined'; |
|
19 | 19 | |
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - public function __construct($message) |
|
24 | - { |
|
25 | - parent::__construct($message); |
|
26 | - } |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + public function __construct($message) |
|
24 | + { |
|
25 | + parent::__construct($message); |
|
26 | + } |
|
27 | 27 | } |
@@ -9,8 +9,7 @@ discard block |
||
9 | 9 | * |
10 | 10 | * @package Zortje\MVC\Model\Table\Entity\Exception |
11 | 11 | */ |
12 | -class EntityClassNotDefinedException extends Exception |
|
13 | -{ |
|
12 | +class EntityClassNotDefinedException extends Exception { |
|
14 | 13 | |
15 | 14 | /** |
16 | 15 | * {@inheritdoc} |
@@ -20,8 +19,7 @@ discard block |
||
20 | 19 | /** |
21 | 20 | * {@inheritdoc} |
22 | 21 | */ |
23 | - public function __construct($message) |
|
24 | - { |
|
22 | + public function __construct($message) { |
|
25 | 23 | parent::__construct($message); |
26 | 24 | } |
27 | 25 | } |