1 | <?php |
||
46 | abstract class Row implements RowInterface, \JsonSerializable, \ArrayAccess |
||
47 | { |
||
48 | use Container\Container; |
||
49 | use Container\ArrayAccess; |
||
50 | use Container\JsonSerialize; |
||
51 | use Container\MagicAccess; |
||
52 | use Traits\TableProperty; |
||
53 | use Traits\RowRelations; |
||
54 | |||
55 | /** |
||
56 | * @var Table instance of Table class |
||
57 | */ |
||
58 | protected $table; |
||
59 | |||
60 | /** |
||
61 | * This is set to a copy of $data when the data is fetched from |
||
62 | * a database, specified as a new tuple in the constructor, or |
||
63 | * when dirty data is posted to the database with save(). |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $clean = []; |
||
68 | |||
69 | /** |
||
70 | * Create Row instance |
||
71 | * |
||
72 | * @param array $data |
||
73 | */ |
||
74 | 17 | public function __construct(array $data = []) |
|
85 | |||
86 | /** |
||
87 | * List of required for serialization properties |
||
88 | * |
||
89 | * @return string[] |
||
90 | */ |
||
91 | public function __sleep() |
||
95 | |||
96 | /** |
||
97 | * Cast to string as class name |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public function __toString() |
||
105 | |||
106 | /** |
||
107 | * Magic method for var_dump() |
||
108 | * |
||
109 | * @return array |
||
110 | * @throws TableNotFoundException |
||
111 | * @see var_dump() |
||
112 | */ |
||
113 | public function __debugInfo() |
||
121 | |||
122 | /** |
||
123 | * Validate input data |
||
124 | * |
||
125 | * @param array $data |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function validate($data) : bool |
||
133 | |||
134 | /** |
||
135 | * Assert input data |
||
136 | * |
||
137 | * @param array $data |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | public function assert($data) : void |
||
145 | |||
146 | /** |
||
147 | * Saves the properties to the database. |
||
148 | * |
||
149 | * This performs an intelligent insert/update, and reloads the |
||
150 | * properties with fresh data from the table on success. |
||
151 | * |
||
152 | * @return mixed The primary key value(s), as an associative array if the |
||
153 | * key is compound, or a scalar if the key is single-column |
||
154 | * @throws DbException |
||
155 | * @throws InvalidPrimaryKeyException |
||
156 | * @throws TableNotFoundException |
||
157 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
158 | */ |
||
159 | 2 | public function save() |
|
177 | |||
178 | /** |
||
179 | * Insert row to Db |
||
180 | * |
||
181 | * @return mixed The primary key value(s), as an associative array if the |
||
182 | * key is compound, or a scalar if the key is single-column |
||
183 | * @throws DbException |
||
184 | * @throws InvalidPrimaryKeyException |
||
185 | * @throws TableNotFoundException |
||
186 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
187 | */ |
||
188 | 1 | protected function doInsert() |
|
235 | |||
236 | /** |
||
237 | * Update row |
||
238 | * |
||
239 | * @return integer The number of rows updated |
||
240 | * @throws DbException |
||
241 | * @throws InvalidPrimaryKeyException |
||
242 | * @throws TableNotFoundException |
||
243 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
244 | */ |
||
245 | 1 | protected function doUpdate() : int |
|
298 | |||
299 | /** |
||
300 | * Delete existing row |
||
301 | * |
||
302 | * @return bool Removed or not |
||
303 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
304 | * @throws DbException |
||
305 | * @throws InvalidPrimaryKeyException |
||
306 | * @throws TableNotFoundException |
||
307 | */ |
||
308 | 1 | public function delete() : bool |
|
335 | |||
336 | /** |
||
337 | * Retrieves an associative array of primary keys, if it exists |
||
338 | * |
||
339 | * @return array |
||
340 | * @throws InvalidPrimaryKeyException |
||
341 | * @throws TableNotFoundException |
||
342 | */ |
||
343 | 3 | protected function getPrimaryKey() : array |
|
349 | |||
350 | /** |
||
351 | * Refreshes properties from the database |
||
352 | * |
||
353 | * @return void |
||
354 | */ |
||
355 | public function refresh() : void |
||
360 | |||
361 | /** |
||
362 | * After read data from Db |
||
363 | * |
||
364 | * @return void |
||
365 | */ |
||
366 | 17 | protected function afterRead() : void |
|
369 | |||
370 | /** |
||
371 | * Allows pre-insert and pre-update logic to be applied to row. |
||
372 | * Subclasses may override this method |
||
373 | * |
||
374 | * @return void |
||
375 | */ |
||
376 | protected function beforeSave() : void |
||
379 | |||
380 | /** |
||
381 | * Allows post-insert and post-update logic to be applied to row. |
||
382 | * Subclasses may override this method |
||
383 | * |
||
384 | * @return void |
||
385 | */ |
||
386 | 2 | protected function afterSave() : void |
|
389 | |||
390 | /** |
||
391 | * Allows pre-insert logic to be applied to row. |
||
392 | * Subclasses may override this method |
||
393 | * |
||
394 | * @return void |
||
395 | */ |
||
396 | 1 | protected function beforeInsert() : void |
|
399 | |||
400 | /** |
||
401 | * Allows post-insert logic to be applied to row. |
||
402 | * Subclasses may override this method |
||
403 | * |
||
404 | * @return void |
||
405 | */ |
||
406 | 1 | protected function afterInsert() : void |
|
409 | |||
410 | /** |
||
411 | * Allows pre-update logic to be applied to row. |
||
412 | * Subclasses may override this method |
||
413 | * |
||
414 | * @return void |
||
415 | */ |
||
416 | 1 | protected function beforeUpdate() : void |
|
419 | |||
420 | /** |
||
421 | * Allows post-update logic to be applied to row. |
||
422 | * Subclasses may override this method |
||
423 | * |
||
424 | * @return void |
||
425 | */ |
||
426 | 1 | protected function afterUpdate() : void |
|
429 | |||
430 | /** |
||
431 | * Allows pre-delete logic to be applied to row. |
||
432 | * Subclasses may override this method |
||
433 | * |
||
434 | * @return void |
||
435 | */ |
||
436 | 1 | protected function beforeDelete() : void |
|
439 | |||
440 | /** |
||
441 | * Allows post-delete logic to be applied to row. |
||
442 | * Subclasses may override this method |
||
443 | * |
||
444 | * @return void |
||
445 | */ |
||
446 | 1 | protected function afterDelete() : void |
|
449 | } |
||
450 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: