1 | <?php |
||
23 | class EntityModel { |
||
24 | /** |
||
25 | * Service to work with rows |
||
26 | * |
||
27 | * ALL DB ACCESS SHOULD BE DONE VIA ROW OPERATOR! NO DIRECT ACCESS TO DB IS ALLOWED! |
||
28 | * |
||
29 | * @var \DbRowDirectOperator $rowOperator |
||
30 | */ |
||
31 | protected $rowOperator; |
||
32 | /** |
||
33 | * Name of table for this entity |
||
34 | * |
||
35 | * @var string $tableName |
||
36 | */ |
||
37 | protected $tableName = '_table'; |
||
38 | |||
39 | /** |
||
40 | * Name of exception class that would be thrown |
||
41 | * |
||
42 | * Uses for calling when you don't know which exact exception should be called |
||
43 | * On Entity\EntityModel's children should be used exception class name |
||
44 | * |
||
45 | * @var string $exceptionClass |
||
46 | */ |
||
47 | protected $exceptionClass = 'Entity\EntityException'; |
||
48 | protected $entityContainerClass = '\Entity\EntityContainer'; |
||
49 | |||
50 | /** |
||
51 | * Property list and description |
||
52 | * |
||
53 | * propertyName => array( |
||
54 | * P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa |
||
55 | * ) |
||
56 | * |
||
57 | * @var array[] $properties |
||
58 | */ |
||
59 | protected $properties = array(); |
||
60 | |||
61 | /** |
||
62 | * @var Accessors $accessors |
||
63 | */ |
||
64 | protected $accessors; |
||
65 | |||
66 | protected $newProperties = array(); |
||
67 | |||
68 | /** |
||
69 | * Entity\EntityModel constructor. |
||
70 | * |
||
71 | * @param \Common\GlobalContainer $gc |
||
72 | */ |
||
73 | public function __construct($gc) { |
||
82 | |||
83 | /** |
||
84 | * @param EntityContainer $that |
||
85 | * @param string $accessor |
||
86 | */ |
||
87 | protected function processRow($that, $accessor) { |
||
88 | foreach ($this->properties as $propertyName => $propertyData) { |
||
89 | $fieldName = !empty($propertyData[P_DB_FIELD]) ? $propertyData[P_DB_FIELD] : ''; |
||
90 | if ($this->accessors->exists($accessor, $propertyName)) { |
||
91 | $this->accessors->execute($accessor, $propertyName, array($that, $propertyName, $fieldName)); |
||
92 | } elseif ($fieldName) { |
||
93 | if ($accessor == P_CONTAINER_IMPORT) { |
||
94 | $that->$propertyName = isset($that->row[$fieldName]) ? $that->row[$fieldName] : null; |
||
95 | } else { |
||
96 | $that->row += array($fieldName => $that->$propertyName); |
||
97 | } |
||
98 | } |
||
99 | // Otherwise it's internal field - filled and used internally |
||
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Import DB row state into object properties |
||
105 | * |
||
106 | * @param EntityContainer $cEntity |
||
107 | * @param array $row |
||
108 | */ |
||
109 | public function importRow($cEntity, $row) { |
||
116 | |||
117 | /** |
||
118 | * Exports object properties to DB row state with ID |
||
119 | * |
||
120 | * @param EntityContainer $cEntity |
||
121 | */ |
||
122 | public function exportRow($cEntity) { |
||
126 | |||
127 | |||
128 | /** |
||
129 | * @param array $array |
||
130 | * |
||
131 | * @return EntityContainer |
||
132 | */ |
||
133 | public function fromArray($array) { |
||
139 | |||
140 | |||
141 | /** |
||
142 | * @return EntityContainer |
||
143 | */ |
||
144 | public function buildContainer() { |
||
152 | |||
153 | |||
154 | /** |
||
155 | * @param EntityContainer $cEntity |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function isEmpty($cEntity) { |
||
162 | |||
163 | /** |
||
164 | * @param EntityContainer $cEntity |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | // TODO - Loaded flag ????? |
||
169 | public function isNew($cEntity) { |
||
172 | |||
173 | |||
174 | /** |
||
175 | * @return \DbRowDirectOperator |
||
176 | */ |
||
177 | public function getRowOperator() { |
||
180 | |||
181 | /** |
||
182 | * @param string $value |
||
183 | */ |
||
184 | public function setTableName($value) { |
||
187 | |||
188 | /** |
||
189 | * Gets entity's table name |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | public function getTableName() { |
||
196 | |||
197 | /** |
||
198 | * @return array[] |
||
199 | */ |
||
200 | public function getProperties() { |
||
203 | |||
204 | /** |
||
205 | * @param $array |
||
206 | */ |
||
207 | public function extendProperties($array) { |
||
210 | |||
211 | /** |
||
212 | * @return Accessors |
||
213 | */ |
||
214 | public function getAccessors() { |
||
217 | |||
218 | protected function delete(EntityContainer $cEntity) { |
||
221 | |||
222 | protected function insert(EntityContainer $cEntity) { |
||
226 | |||
227 | protected function update(EntityContainer $cEntity) { |
||
234 | |||
235 | protected function unchanged(EntityContainer $cEntity){ |
||
240 | |||
241 | protected function emptyAction(EntityContainer $cEntity) { |
||
246 | |||
247 | protected function save(EntityContainer $cEntity) { |
||
264 | |||
265 | } |
||
266 |
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.