1
|
|
|
<?php |
2
|
|
|
namespace Entity; |
3
|
|
|
|
4
|
|
|
use \Common\Accessors; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class EntityModel |
8
|
|
|
* |
9
|
|
|
* This class have only one instance - i.e. is a service |
10
|
|
|
* Describes persistent entity - which can be loaded from/stored to storage |
11
|
|
|
* |
12
|
|
|
* |
13
|
|
|
* Introduces linked models and export/import operations |
14
|
|
|
* |
15
|
|
|
* Importer is a callable like |
16
|
|
|
* function ($that, &$row[, $propertyName[, $fieldName]]) {} |
17
|
|
|
* |
18
|
|
|
* Exporter is a callable like |
19
|
|
|
* function ($that, &$row[, $propertyName[, $fieldName]]) {} |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
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) { |
74
|
|
|
// Here own rowOperator can be made - if needed to operate other, non-global, DB |
75
|
|
|
$this->rowOperator = $gc->dbGlobalRowOperator; |
|
|
|
|
76
|
|
|
$this->accessors = new Accessors(); |
77
|
|
|
|
78
|
|
|
if(property_exists($this, 'newProperties') && !empty($this->newProperties)) { |
79
|
|
|
$this->extendProperties($this->newProperties); |
80
|
|
|
} |
81
|
|
|
} |
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) { |
110
|
|
|
$cEntity->clear(); |
111
|
|
|
if (is_array($row) && !empty($row)) { |
112
|
|
|
$cEntity->row = $row; |
113
|
|
|
$this->processRow($cEntity, P_CONTAINER_IMPORT); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Exports object properties to DB row state with ID |
119
|
|
|
* |
120
|
|
|
* @param EntityContainer $cEntity |
121
|
|
|
*/ |
122
|
|
|
public function exportRow($cEntity) { |
123
|
|
|
$cEntity->row = array(); |
124
|
|
|
$this->processRow($cEntity, P_CONTAINER_EXPORT); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array $array |
130
|
|
|
* |
131
|
|
|
* @return EntityContainer |
132
|
|
|
*/ |
133
|
|
|
public function fromArray($array) { |
134
|
|
|
$cEntity = $this->buildContainer(); |
135
|
|
|
$this->importRow($cEntity, $array); |
136
|
|
|
|
137
|
|
|
return $cEntity; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return EntityContainer |
143
|
|
|
*/ |
144
|
|
|
public function buildContainer() { |
145
|
|
|
/** |
146
|
|
|
* @var EntityContainer $container |
147
|
|
|
*/ |
148
|
|
|
$container = new $this->entityContainerClass($this); |
149
|
|
|
|
150
|
|
|
return $container; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param EntityContainer $cEntity |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function isEmpty($cEntity) { |
160
|
|
|
return $cEntity->isEmpty(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param EntityContainer $cEntity |
165
|
|
|
* |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
// TODO - Loaded flag ????? |
|
|
|
|
169
|
|
|
public function isNew($cEntity) { |
170
|
|
|
return $cEntity->isEmpty(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return \DbRowDirectOperator |
176
|
|
|
*/ |
177
|
|
|
public function getRowOperator() { |
178
|
|
|
return $this->rowOperator; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $value |
183
|
|
|
*/ |
184
|
|
|
public function setTableName($value) { |
185
|
|
|
$this->tableName = $value; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Gets entity's table name |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
public function getTableName() { |
194
|
|
|
return $this->tableName; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return array[] |
199
|
|
|
*/ |
200
|
|
|
public function getProperties() { |
201
|
|
|
return $this->properties; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param $array |
206
|
|
|
*/ |
207
|
|
|
public function extendProperties($array) { |
208
|
|
|
$this->properties += $array; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return Accessors |
213
|
|
|
*/ |
214
|
|
|
public function getAccessors() { |
215
|
|
|
return $this->accessors; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
protected function delete(EntityContainer $cEntity) { |
219
|
|
|
throw new \Exception(__CLASS__ . '::delete() in ' . get_called_class() . 'is not yet implemented'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
protected function insert(EntityContainer $cEntity) { |
223
|
|
|
$this->rowOperator->insert($this, $this->exportRow($cEntity)); |
|
|
|
|
224
|
|
|
// TODO - re-read record |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
protected function update(EntityContainer $cEntity) { |
|
|
|
|
228
|
|
|
// TODO - separate real changes from internal ones |
229
|
|
|
// Generate changeset row |
230
|
|
|
// Foreach all rows. If there is change and no delta - then put delta. Otherwise put change |
231
|
|
|
// If row not empty - update |
232
|
|
|
throw new \Exception(__CLASS__ . '::update() in ' . get_called_class() . 'is not yet implemented'); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
protected function unchanged(EntityContainer $cEntity){ |
|
|
|
|
236
|
|
|
// TODO - or just save nothing ????? |
237
|
|
|
// throw new \Exception('EntityModel isNotEmpty, have dbId and not CHANGED! It can\'t be!'); |
238
|
|
|
throw new \Exception(__CLASS__ . '::unchanged() in ' . get_called_class() . 'is not yet implemented'); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
protected function emptyAction(EntityContainer $cEntity) { |
|
|
|
|
242
|
|
|
// Just created container and doesn't use it |
243
|
|
|
// throw new \Exception('EntityModel isEmpty but not loaded! It can\'t be!'); |
244
|
|
|
throw new \Exception(__CLASS__ . '::emptyAction() in ' . get_called_class() . 'is not yet implemented'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
protected function save(EntityContainer $cEntity) { |
248
|
|
|
if ($this->isEmpty($cEntity)) { |
249
|
|
|
if ($cEntity->isLoaded) { |
|
|
|
|
250
|
|
|
$this->delete($cEntity); |
251
|
|
|
} else { |
252
|
|
|
$this->emptyAction($cEntity); |
253
|
|
|
} |
254
|
|
|
} else { |
255
|
|
|
if (empty($cEntity->dbId)) { |
|
|
|
|
256
|
|
|
$this->insert($cEntity); |
257
|
|
|
} elseif (method_exists($cEntity, 'isChanged') && $cEntity->isChanged()) { |
258
|
|
|
$this->update($cEntity); |
259
|
|
|
} else { |
260
|
|
|
$this->unchanged($cEntity); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} |
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.