|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class Entity |
|
5
|
|
|
* |
|
6
|
|
|
* @property int|float $dbId Buddy record DB ID |
|
7
|
|
|
*/ |
|
8
|
|
|
class Entity implements \Common\IMagicProperties { |
|
9
|
|
|
/** |
|
10
|
|
|
* Name of table for this entity |
|
11
|
|
|
* |
|
12
|
|
|
* @var string $tableName |
|
13
|
|
|
*/ |
|
14
|
|
|
protected static $tableName = '_table'; |
|
15
|
|
|
/** |
|
16
|
|
|
* Name of key field field in this table |
|
17
|
|
|
* |
|
18
|
|
|
* @var string $idField |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static $idField = 'id'; |
|
21
|
|
|
/** |
|
22
|
|
|
* Container for property values |
|
23
|
|
|
* |
|
24
|
|
|
* @var PropertyHider $_container |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $_container; |
|
27
|
|
|
protected static $_containerName = 'PropertyHiderInArray'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Property list |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected static $_properties = array(); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected static $_propertyToField = array(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var db_mysql|null $dbStatic |
|
43
|
|
|
*/ |
|
44
|
|
|
public static $dbStatic = null; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Fills property-to-field table which used to generate result array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function fillPropertyToField() { |
|
50
|
|
|
if (!empty(static::$_propertyToField)) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Filling property-to-filed relation array |
|
55
|
|
|
foreach (static::$_properties as $propertyName => &$propertyData) { |
|
56
|
|
|
// Property is mapped 1-to-1 to field |
|
57
|
|
|
if (!empty($propertyData[P_DB_FIELD])) { |
|
58
|
|
|
$fieldName = $propertyData[P_DB_FIELD]; |
|
59
|
|
|
/** |
|
60
|
|
|
* @param static $that |
|
61
|
|
|
*/ |
|
62
|
|
|
// Alas! No bindTo() method in 5.3 closures! So we should use what we have |
|
63
|
|
|
$propertyData[P_DB_ROW_EXPORT] = function ($that, &$row) use ($propertyName, $fieldName) { |
|
64
|
|
|
$row[$fieldName] = $that->$propertyName; |
|
65
|
|
|
}; |
|
66
|
|
|
$propertyData[P_DB_ROW_IMPORT] = function ($that, &$row) use ($propertyName, $fieldName) { |
|
67
|
|
|
// TODO: Here should be some conversions to property type |
|
68
|
|
|
$that->$propertyName = $row[$fieldName]; |
|
69
|
|
|
}; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Entity constructor. |
|
77
|
|
|
* |
|
78
|
|
|
* @param \Pimple\GlobalContainer $gc |
|
79
|
|
|
*/ |
|
80
|
|
|
public function __construct($gc) { |
|
81
|
|
|
empty(static::$dbStatic) && !empty($gc->db) ? static::$dbStatic = $gc->db : false; |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
$this->_container = new static::$_containerName(); |
|
84
|
|
|
$this->_container->setProperties(static::$_properties); |
|
85
|
|
|
|
|
86
|
|
|
$this->fillPropertyToField(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
public function getTableName() { |
|
91
|
|
|
return static::$tableName; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getIdFieldName() { |
|
95
|
|
|
return static::$idField; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function load($recordId) { |
|
99
|
|
|
classSupernova::$gc->dbRowOperator->getById($this, $recordId); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// TODO - move to reader ???????? |
|
|
|
|
|
|
103
|
|
|
public function delete() { |
|
104
|
|
|
return classSupernova::$gc->dbRowOperator->deleteById($this); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return int|string |
|
109
|
|
|
*/ |
|
110
|
|
|
// TODO - move to reader ???????? |
|
|
|
|
|
|
111
|
|
|
public function insert() { |
|
112
|
|
|
return classSupernova::$gc->dbRowOperator->insert($this); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function isContainerEmpty() { |
|
116
|
|
|
return $this->_container->isContainerEmpty(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function isNew() { |
|
120
|
|
|
return $this->getIdFieldName() != 0; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Invoke row transformation operation on object |
|
125
|
|
|
* |
|
126
|
|
|
* Uses in to save/load data from DB row into/from object |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $row |
|
129
|
|
|
* @param string $operation |
|
130
|
|
|
* @param string $desc |
|
131
|
|
|
* |
|
132
|
|
|
* @throws Exception |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function rowInvokeOperation(&$row, $operation, $desc) { |
|
135
|
|
|
foreach (static::$_properties as $propertyName => $propertyData) { |
|
136
|
|
|
if (is_callable($propertyData[$operation])) { |
|
137
|
|
|
// Some black magic here |
|
138
|
|
|
// Closure is a class - so have __invoke() magic method |
|
139
|
|
|
// It means we can invoke it by directly call __invoke() |
|
140
|
|
|
$propertyData[$operation]->__invoke($this, $row); |
|
141
|
|
|
// TODO - however for a sake of uniformity may be we should consider use call_user_func |
|
142
|
|
|
// call_user_func($propertyData[P_DB_ROW_EXPORT], $this); |
|
143
|
|
|
} else { |
|
144
|
|
|
throw new \Exception('There is no valid DB row ' . $desc . ' for ' . get_called_class() . '::' . $propertyName); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Import DB row state into object properties |
|
151
|
|
|
* |
|
152
|
|
|
* @param array $row |
|
153
|
|
|
*/ |
|
154
|
|
|
public function importDbRow($row) { |
|
155
|
|
|
$this->rowInvokeOperation($row, P_DB_ROW_IMPORT, 'IMPORTER'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Export data from object properties into DB row for further use |
|
160
|
|
|
* |
|
161
|
|
|
* @param bool $withDbId - Should dbId too be returned. Useful for INSERT statements |
|
162
|
|
|
* |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
public function exportDbRow($withDbId = true) { |
|
166
|
|
|
$row = array(); |
|
167
|
|
|
|
|
168
|
|
|
$this->rowInvokeOperation($row, P_DB_ROW_EXPORT, 'EXPORTER'); |
|
169
|
|
|
|
|
170
|
|
|
if (!$withDbId) { |
|
171
|
|
|
unset($row[$this->getIdFieldName()]); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $row; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
public function __get($name) { |
|
179
|
|
|
return $this->_container->$name; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function __set($name, $value) { |
|
183
|
|
|
$this->_container->$name = $value; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function __isset($name) { |
|
187
|
|
|
return isset($this->_container->$name); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function __unset($name) { |
|
191
|
|
|
unset($this->_container->$name); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.