1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class Entity |
5
|
|
|
* |
6
|
|
|
* @property int|float $dbId Buddy record DB ID |
7
|
|
|
*/ |
8
|
|
|
class Entity implements \Common\IMagicProperties, \Common\IEntity { |
9
|
|
|
const ENTITY_DB_ID_INCLUDE = true; |
10
|
|
|
const ENTITY_DB_ID_EXCLUDE = true; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Link to DB which used by this Entity |
14
|
|
|
* |
15
|
|
|
* @var db_mysql $dbStatic |
16
|
|
|
* deprecated - replace with container ID like 'db' or 'dbAuth' |
17
|
|
|
*/ |
18
|
|
|
protected static $dbStatic; |
19
|
|
|
/** |
20
|
|
|
* Name of table for this entity |
21
|
|
|
* |
22
|
|
|
* @var string $tableName |
23
|
|
|
*/ |
24
|
|
|
protected static $tableName = '_table'; |
25
|
|
|
/** |
26
|
|
|
* Name of key field field in this table |
27
|
|
|
* |
28
|
|
|
* @var string $idField |
29
|
|
|
*/ |
30
|
|
|
protected static $idField = 'id'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Container for property values |
34
|
|
|
* |
35
|
|
|
* @var \Common\IPropertyContainer $_container |
36
|
|
|
*/ |
37
|
|
|
protected $_container; |
38
|
|
|
protected static $_containerName = 'V2PropertyContainer'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Property list and description |
42
|
|
|
* |
43
|
|
|
* propertyName => array( |
44
|
|
|
* P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa |
45
|
|
|
* ) |
46
|
|
|
* |
47
|
|
|
* @var array[] |
48
|
|
|
*/ |
49
|
|
|
protected static $_properties = array(); |
50
|
|
|
|
51
|
|
|
// /** |
|
|
|
|
52
|
|
|
// * @var array |
53
|
|
|
// */ |
54
|
|
|
// protected static $_propertyToField = array(); |
55
|
|
|
// protected static $_propertyImportExportIsBind = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Service to work with rows |
59
|
|
|
* |
60
|
|
|
* @var \DbRowDirectOperator $rowOperator |
61
|
|
|
*/ |
62
|
|
|
protected static $rowOperator; |
63
|
|
|
|
64
|
|
|
// protected function bindFieldExport(&$propertyData, $propertyName) { |
|
|
|
|
65
|
|
|
// if (!empty($propertyData[P_DB_ROW_EXPORT])) { |
66
|
|
|
// return; |
67
|
|
|
// } |
68
|
|
|
// |
69
|
|
|
// // Last resort - binding export function to DB field name |
70
|
|
|
// if (!empty($propertyData[P_DB_FIELD])) { |
71
|
|
|
// // Property is mapped 1-to-1 to field |
72
|
|
|
// /** |
73
|
|
|
// * @param static $that |
74
|
|
|
// */ |
75
|
|
|
// // Alas! No bindTo() method in 5.3 closures! So we should use what we have |
76
|
|
|
// // Also no auto bound of $this until 5.4 |
77
|
|
|
// $propertyData[P_DB_ROW_EXPORT] = function ($that, &$row) use ($propertyName, $propertyData) { |
78
|
|
|
// $row[$propertyData[P_DB_FIELD]] = $that->$propertyName; |
79
|
|
|
// }; |
80
|
|
|
// } |
81
|
|
|
// } |
82
|
|
|
|
83
|
|
|
// protected function bindFieldImport(&$propertyData, $propertyName) { |
|
|
|
|
84
|
|
|
// if (!empty($propertyData[P_DB_ROW_IMPORT])) { |
85
|
|
|
// return; |
86
|
|
|
// } |
87
|
|
|
// |
88
|
|
|
// // Last resort - binding import function to DB field name |
89
|
|
|
// if (!empty($propertyData[P_DB_ROW_METHOD_IMPORT_V2])) { |
90
|
|
|
//// $funcName = $propertyData[P_DB_ROW_METHOD_IMPORT_V2]; |
91
|
|
|
// $propertyData[P_DB_ROW_IMPORT] = array($this, $propertyData[P_DB_ROW_METHOD_IMPORT_V2]); |
92
|
|
|
//// function ($that, &$row) use ($propertyName, $propertyData, $funcName) { |
93
|
|
|
//// $that->$propertyName = \Common\Types::castAs( |
94
|
|
|
//// !empty($propertyData[P_DB_TYPE]) ? $propertyData[P_DB_TYPE] : TYPE_EMPTY, |
95
|
|
|
//// $row[$propertyData[P_DB_FIELD]] |
96
|
|
|
//// ); |
97
|
|
|
//// }; |
98
|
|
|
// } elseif (!empty($propertyData[P_DB_FIELD])) { |
99
|
|
|
// // Property is mapped 1-to-1 to field |
100
|
|
|
// /** |
101
|
|
|
// * @param static $that |
102
|
|
|
// */ |
103
|
|
|
// // Alas! No bindTo() method in 5.3 closures! So we should use what we have |
104
|
|
|
// // Also no auto bound of $this until 5.4 |
105
|
|
|
// $propertyData[P_DB_ROW_IMPORT] = function ($that, &$row) use ($propertyName, $propertyData) { |
106
|
|
|
// $that->$propertyName = \Common\Types::castAs( |
107
|
|
|
// !empty($propertyData[P_DB_TYPE]) ? $propertyData[P_DB_TYPE] : TYPE_EMPTY, |
108
|
|
|
// $row[$propertyData[P_DB_FIELD]] |
109
|
|
|
// ); |
110
|
|
|
// }; |
111
|
|
|
// } |
112
|
|
|
// } |
113
|
|
|
|
114
|
|
|
// /** |
|
|
|
|
115
|
|
|
// * Fills property-to-field table which used to generate result array |
116
|
|
|
// */ |
117
|
|
|
// protected function fillPropertyToField() { |
118
|
|
|
// if (static::$_propertyImportExportIsBind) { |
119
|
|
|
// return; |
120
|
|
|
// } |
121
|
|
|
// |
122
|
|
|
// // Filling property-to-field relation array |
123
|
|
|
// foreach (static::$_properties as $propertyName => &$propertyData) { |
124
|
|
|
// $this->bindFieldExport($propertyData, $propertyName); |
125
|
|
|
// $this->bindFieldImport($propertyData, $propertyName); |
126
|
|
|
// } |
127
|
|
|
// |
128
|
|
|
// static::$_propertyImportExportIsBind = true; |
129
|
|
|
// } |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Entity constructor. |
133
|
|
|
* |
134
|
|
|
* @param \Common\GlobalContainer $gc |
135
|
|
|
*/ |
136
|
|
|
public function __construct($gc) { |
137
|
|
|
empty(static::$dbStatic) && !empty($gc->db) ? static::$dbStatic = $gc->db : false; |
|
|
|
|
138
|
|
|
static::$rowOperator = $gc->dbRowOperator; |
|
|
|
|
139
|
|
|
|
140
|
|
|
$this->_container = new static::$_containerName(); |
141
|
|
|
$this->_container->setProperties(static::$_properties); |
142
|
|
|
|
143
|
|
|
// $this->fillPropertyToField(); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
// /** |
148
|
|
|
// * Invoke row transformation operation on object |
149
|
|
|
// * |
150
|
|
|
// * Uses in to save/load data from DB row into/from object |
151
|
|
|
// * |
152
|
|
|
// * @param array $row |
153
|
|
|
// * @param string $operation |
154
|
|
|
// * @param string $desc |
155
|
|
|
// * |
156
|
|
|
// * @throws Exception |
157
|
|
|
// * |
158
|
|
|
// * @deprecated |
159
|
|
|
// */ |
160
|
|
|
// protected function rowInvokeOperation(&$row, $operation, $desc) { |
161
|
|
|
// foreach (static::$_properties as $propertyName => $propertyData) { |
162
|
|
|
// if (is_callable($propertyData[$operation])) { |
163
|
|
|
// // Some black magic here |
164
|
|
|
// // Closure is a class - so have __invoke() magic method |
165
|
|
|
// // It means we can invoke it by directly call __invoke() |
166
|
|
|
// // $propertyData[$operation]->__invoke($this, $row); |
167
|
|
|
// // However for a sake of uniformity may be we should consider use call_user_func |
168
|
|
|
// call_user_func($propertyData[$operation], $this, $row); |
169
|
|
|
// } else { |
170
|
|
|
// throw new \Exception('There is no valid DB [' . $operation . '] row operation for ' . get_called_class() . '::' . $propertyName); |
171
|
|
|
// } |
172
|
|
|
// } |
173
|
|
|
// } |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Import DB row state into object properties |
177
|
|
|
* |
178
|
|
|
* @param array $row |
179
|
|
|
*/ |
180
|
|
|
public function importRow($row) { |
181
|
|
|
$this->_container->importRow($row); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Export data from object properties into DB row for further use |
186
|
|
|
* |
187
|
|
|
* @param bool $withDbId - Should dbId too be returned. Useful for INSERT statements |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
protected function exportDbRow($withDbId = self::ENTITY_DB_ID_INCLUDE) { |
192
|
|
|
$row = $this->_container->exportRow(); |
193
|
|
|
|
194
|
|
|
if ($withDbId == self::ENTITY_DB_ID_EXCLUDE) { |
195
|
|
|
unset($row[$this->getIdFieldName()]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $row; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
public function exportRowWithoutId() { |
205
|
|
|
return $this->exportDbRow(self::ENTITY_DB_ID_EXCLUDE); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
|
|
public function exportRowWithId() { |
212
|
|
|
return $this->exportDbRow(self::ENTITY_DB_ID_INCLUDE); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Gets entity's table name |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public function getTableName() { |
221
|
|
|
return static::$tableName; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public function getIdFieldName() { |
228
|
|
|
return static::$idField; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return bool |
233
|
|
|
*/ |
234
|
|
|
public function isContainerEmpty() { |
235
|
|
|
return $this->_container->isEmpty(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return bool |
240
|
|
|
*/ |
241
|
|
|
public function isNew() { |
242
|
|
|
return empty($this->dbId); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return db_mysql |
247
|
|
|
*/ |
248
|
|
|
public function getDbStatic() { |
249
|
|
|
return static::$dbStatic; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function __get($name) { |
253
|
|
|
return $this->_container->$name; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function __set($name, $value) { |
257
|
|
|
$this->_container->$name = $value; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function __isset($name) { |
261
|
|
|
return isset($this->_container->$name); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function __unset($name) { |
265
|
|
|
unset($this->_container->$name); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
} |
269
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.