1
|
|
|
<?php |
2
|
|
|
namespace Entity; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class Entity\EntityModel |
6
|
|
|
* |
7
|
|
|
* This class have only one instance - i.e. is a service |
8
|
|
|
* Describes persistent entity - which can be loaded from/stored to storage |
9
|
|
|
* |
10
|
|
|
* |
11
|
|
|
* Introduces linked models and export/import operations |
12
|
|
|
* |
13
|
|
|
* Importer is a callable like |
14
|
|
|
* function ($that, &$row[, $propertyName[, $fieldName]]) {} |
15
|
|
|
* |
16
|
|
|
* Exporter is a callable like |
17
|
|
|
* function ($that, &$row[, $propertyName[, $fieldName]]) {} |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
class EntityModel { |
22
|
|
|
/** |
23
|
|
|
* Service to work with rows |
24
|
|
|
* |
25
|
|
|
* ALL DB ACCESS SHOULD BE DONE VIA ROW OPERATOR! NO DIRECT ACCESS TO DB IS ALLOWED! |
26
|
|
|
* |
27
|
|
|
* @var \DbRowDirectOperator $rowOperator |
28
|
|
|
*/ |
29
|
|
|
protected $rowOperator; |
30
|
|
|
/** |
31
|
|
|
* Name of table for this entity |
32
|
|
|
* |
33
|
|
|
* @var string $tableName |
34
|
|
|
*/ |
35
|
|
|
protected $tableName = '_table'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Name of key field field in this table |
39
|
|
|
* |
40
|
|
|
* @var string $idFieldName |
41
|
|
|
*/ |
42
|
|
|
// TODO - remove |
43
|
|
|
protected $idFieldName = 'id'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Name of exception class that would be thrown |
47
|
|
|
* |
48
|
|
|
* Uses for calling when you don't know which exact exception should be called |
49
|
|
|
* On Entity\EntityModel's children should be used exception class name |
50
|
|
|
* |
51
|
|
|
* @var string $exceptionClass |
52
|
|
|
*/ |
53
|
|
|
protected $exceptionClass = 'Entity\EntityException'; |
54
|
|
|
protected $entityContainerClass = '\Entity\EntityContainer'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Property list and description |
58
|
|
|
* |
59
|
|
|
* propertyName => array( |
60
|
|
|
* P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa |
61
|
|
|
* ) |
62
|
|
|
* |
63
|
|
|
* @var array[] $properties |
64
|
|
|
*/ |
65
|
|
|
protected $properties = array(); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var \Common\Accessors $accessors |
69
|
|
|
*/ |
70
|
|
|
protected $accessors; |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Entity\EntityModel constructor. |
75
|
|
|
* |
76
|
|
|
* @param \Common\GlobalContainer $gc |
77
|
|
|
*/ |
78
|
|
|
public function __construct($gc) { |
79
|
|
|
// Here own rowOperator can be made - if needed to operate other, non-global, DB |
80
|
|
|
$this->rowOperator = $gc->dbGlobalRowOperator; |
|
|
|
|
81
|
|
|
$this->accessors = new \Common\Accessors(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param EntityContainer $that |
86
|
|
|
* @param string $accessor |
87
|
|
|
*/ |
88
|
|
|
protected function processRow($that, $accessor) { |
89
|
|
|
foreach ($this->properties as $propertyName => $propertyData) { |
90
|
|
|
$fieldName = !empty($propertyData[P_DB_FIELD]) ? $propertyData[P_DB_FIELD] : ''; |
91
|
|
|
if ($this->accessors->haveAccessor($propertyName, $accessor)) { |
92
|
|
|
$this->accessors->invokeAccessor($propertyName, $accessor, array($that, $propertyName, $fieldName)); |
93
|
|
|
} elseif ($fieldName) { |
94
|
|
|
if ($accessor == P_CONTAINER_IMPORT) { |
95
|
|
|
$that->$propertyName = isset($that->row[$fieldName]) ? $that->row[$fieldName] : null; |
96
|
|
|
} else { |
97
|
|
|
$that->row += array($fieldName => $that->$propertyName); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
// Otherwise it's internal field - filled and used internally |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Import DB row state into object properties |
106
|
|
|
* |
107
|
|
|
* @param EntityContainer $cEntity |
108
|
|
|
* @param array $row |
109
|
|
|
*/ |
110
|
|
|
public function importRow($cEntity, $row) { |
111
|
|
|
$cEntity->clear(); |
112
|
|
|
$cEntity->row = $row; |
113
|
|
|
|
114
|
|
|
if (is_array($row) && !empty($row)) { |
115
|
|
|
$this->processRow($cEntity, P_CONTAINER_IMPORT); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array $array |
121
|
|
|
* |
122
|
|
|
* @return EntityContainer |
123
|
|
|
*/ |
124
|
|
|
public function fromArray($array) { |
125
|
|
|
/** |
126
|
|
|
* @var EntityContainer $cEntity |
127
|
|
|
*/ |
128
|
|
|
$cEntity = $this->buildContainer(); |
129
|
|
|
$this->importRow($cEntity, $array); |
130
|
|
|
|
131
|
|
|
return $cEntity; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Exports object properties to DB row state with ID |
136
|
|
|
* |
137
|
|
|
* @param EntityContainer $cEntity |
138
|
|
|
*/ |
139
|
|
|
public function exportRow($cEntity) { |
140
|
|
|
$cEntity->row = array(); |
141
|
|
|
$this->processRow($cEntity, P_CONTAINER_EXPORT); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return EntityContainer |
146
|
|
|
*/ |
147
|
|
|
public function buildContainer() { |
148
|
|
|
/** |
149
|
|
|
* @var EntityContainer $container |
150
|
|
|
*/ |
151
|
|
|
$container = new $this->entityContainerClass($this); |
152
|
|
|
|
153
|
|
|
return $container; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param EntityContainer $cEntity |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function isEmpty($cEntity) { |
163
|
|
|
return $cEntity->isEmpty(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param EntityContainer $cEntity |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
// TODO - Loaded flag ????? |
|
|
|
|
172
|
|
|
public function isNew($cEntity) { |
173
|
|
|
return $cEntity->isEmpty(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
// /** |
178
|
|
|
// * Clears only properties which declared in $properties array |
179
|
|
|
// * |
180
|
|
|
// * @param EntityContainer $cEntity |
181
|
|
|
// */ |
182
|
|
|
// public function clearProperties($cEntity) { |
183
|
|
|
// foreach ($this->properties as $propertyName => $propertyData) { |
184
|
|
|
// unset($cEntity->$propertyName); |
185
|
|
|
// } |
186
|
|
|
// } |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return \DbRowDirectOperator |
191
|
|
|
*/ |
192
|
|
|
public function getRowOperator() { |
193
|
|
|
return $this->rowOperator; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $value |
198
|
|
|
*/ |
199
|
|
|
public function setTableName($value) { |
200
|
|
|
$this->tableName = $value; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Gets entity's table name |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
public function getTableName() { |
209
|
|
|
return $this->tableName; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// /** |
213
|
|
|
// * @param string $value |
214
|
|
|
// */ |
215
|
|
|
// public function setIdFieldName($value) { |
216
|
|
|
// $this->idField = $value; |
217
|
|
|
// } |
218
|
|
|
// /** |
219
|
|
|
// * Gets entity's DB ID field name (which is unique within entity set) |
220
|
|
|
// * |
221
|
|
|
// * @return string |
222
|
|
|
// */ |
223
|
|
|
// public function getIdFieldName() { |
224
|
|
|
// return $this->idField; |
225
|
|
|
// } |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return array[] |
229
|
|
|
*/ |
230
|
|
|
public function getProperties() { |
231
|
|
|
return $this->properties; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param $array |
236
|
|
|
*/ |
237
|
|
|
public function extendProperties($array) { |
238
|
|
|
$this->properties += $array; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return \Common\Accessors |
243
|
|
|
*/ |
244
|
|
|
public function getAccessors() { |
245
|
|
|
return $this->accessors; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
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.