1 | <?php |
||
22 | class EntityModel { |
||
23 | /** |
||
24 | * Service to work with rows |
||
25 | * |
||
26 | * ALL DB ACCESS SHOULD BE DONE VIA ROW OPERATOR! NO DIRECT ACCESS TO DB IS ALLOWED! |
||
27 | * |
||
28 | * @var \DbRowDirectOperator $rowOperator |
||
29 | */ |
||
30 | protected $rowOperator; |
||
31 | /** |
||
32 | * Name of table for this entity |
||
33 | * |
||
34 | * @var string $tableName |
||
35 | */ |
||
36 | protected $tableName = '_table'; |
||
37 | |||
38 | /** |
||
39 | * Name of exception class that would be thrown |
||
40 | * |
||
41 | * Uses for calling when you don't know which exact exception should be called |
||
42 | * On Entity\EntityModel's children should be used exception class name |
||
43 | * |
||
44 | * @var string $exceptionClass |
||
45 | */ |
||
46 | protected $exceptionClass = 'Entity\EntityException'; |
||
47 | protected $entityContainerClass = '\Entity\EntityContainer'; |
||
48 | |||
49 | /** |
||
50 | * Property list and description |
||
51 | * |
||
52 | * propertyName => array( |
||
53 | * P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa |
||
54 | * ) |
||
55 | * |
||
56 | * @var array[] $properties |
||
57 | */ |
||
58 | protected $properties = array(); |
||
59 | |||
60 | /** |
||
61 | * @var Accessors $accessors |
||
62 | */ |
||
63 | protected $accessors; |
||
64 | |||
65 | protected $newProperties = array(); |
||
66 | |||
67 | /** |
||
68 | * Entity\EntityModel constructor. |
||
69 | * |
||
70 | * @param \Common\GlobalContainer $gc |
||
71 | */ |
||
72 | public function __construct($gc) { |
||
81 | |||
82 | /** |
||
83 | * @param EntityContainer $that |
||
84 | * @param string $accessor |
||
85 | */ |
||
86 | protected function processRow($that, $accessor) { |
||
101 | |||
102 | /** |
||
103 | * Import DB row state into object properties |
||
104 | * |
||
105 | * @param EntityContainer $cEntity |
||
106 | * @param array $row |
||
107 | */ |
||
108 | public function importRow($cEntity, $row) { |
||
115 | |||
116 | /** |
||
117 | * Exports object properties to DB row state with ID |
||
118 | * |
||
119 | * @param EntityContainer $cEntity |
||
120 | */ |
||
121 | public function exportRow($cEntity) { |
||
125 | |||
126 | /** |
||
127 | * @param array $array |
||
128 | * |
||
129 | * @return EntityContainer |
||
130 | */ |
||
131 | public function fromArray($array) { |
||
137 | |||
138 | /** |
||
139 | * @return EntityContainer |
||
140 | */ |
||
141 | public function buildContainer() { |
||
149 | |||
150 | /** |
||
151 | * @param EntityContainer $cEntity |
||
152 | * |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function isEmpty($cEntity) { |
||
158 | |||
159 | /** |
||
160 | * @param EntityContainer $cEntity |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | // TODO - Loaded flag ????? |
||
165 | public function isNew($cEntity) { |
||
168 | |||
169 | // |
||
170 | // Save/load methods ================================================================================================= |
||
171 | |||
172 | /** |
||
173 | * @param array $filter |
||
174 | * |
||
175 | * @return EntityContainer|false |
||
176 | */ |
||
177 | public function load($filter) { |
||
183 | |||
184 | /** |
||
185 | * @param EntityContainer $cEntity |
||
186 | */ |
||
187 | protected function insert($cEntity) { |
||
191 | |||
192 | /** |
||
193 | * @param EntityContainer $cEntity |
||
194 | * |
||
195 | * @throws \Exception |
||
196 | */ |
||
197 | protected function update($cEntity) { |
||
204 | |||
205 | /** |
||
206 | * @param EntityContainer $cEntity |
||
207 | * |
||
208 | * @throws \Exception |
||
209 | */ |
||
210 | protected function delete($cEntity) { |
||
213 | |||
214 | /** |
||
215 | * Method is called when trying to save DB_RECORD_LOADED but unchanged container |
||
216 | * |
||
217 | * Generally in this case no need in DB operations |
||
218 | * If any entity require to save empty data (for updating timestamp for ex.) it should override this method |
||
219 | * |
||
220 | * @param EntityContainer $cEntity |
||
221 | * |
||
222 | * @throws \Exception |
||
223 | */ |
||
224 | protected function onSaveUnchanged($cEntity) { |
||
229 | |||
230 | /** |
||
231 | * Method is called when trying to save newly created DB_RECORD_NEW and Empty container |
||
232 | * |
||
233 | * If it is needed to really save empty container (for log purposes, for ex.) child should override this method |
||
234 | * |
||
235 | * @param EntityContainer $cEntity |
||
236 | * |
||
237 | * @throws \Exception |
||
238 | */ |
||
239 | protected function onSaveNew($cEntity) { |
||
244 | |||
245 | /** |
||
246 | * Saves entity to DB |
||
247 | * |
||
248 | * @param EntityContainer $cEntity |
||
249 | */ |
||
250 | protected function save($cEntity) { |
||
267 | |||
268 | |||
269 | // |
||
270 | // Protected properties accessors ==================================================================================== |
||
271 | |||
272 | /** |
||
273 | * @return \DbRowDirectOperator |
||
274 | */ |
||
275 | public function getRowOperator() { |
||
278 | |||
279 | /** |
||
280 | * @param string $value |
||
281 | */ |
||
282 | public function setTableName($value) { |
||
285 | |||
286 | /** |
||
287 | * Gets entity's table name |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | public function getTableName() { |
||
294 | |||
295 | /** |
||
296 | * @return array[] |
||
297 | */ |
||
298 | public function getProperties() { |
||
301 | |||
302 | /** |
||
303 | * @param $array |
||
304 | */ |
||
305 | public function extendProperties($array) { |
||
308 | |||
309 | /** |
||
310 | * @return Accessors |
||
311 | */ |
||
312 | public function getAccessors() { |
||
315 | |||
316 | } |
||
317 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.