1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use \Common\GlobalContainer; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class EntityContainer |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class EntityContainer extends V2PropertyContainer implements IEntityContainer { |
10
|
|
|
const ENTITY_DB_ID_INCLUDE = true; |
11
|
|
|
const ENTITY_DB_ID_EXCLUDE = false; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var EntityModel $model |
15
|
|
|
*/ |
16
|
|
|
protected $model; |
17
|
|
|
protected static $exceptionClass = 'EntityException'; |
18
|
|
|
protected static $modelClass = 'EntityModel'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Common\GlobalContainer $gc |
22
|
|
|
*/ |
23
|
|
|
protected $gc; |
24
|
|
|
/** |
25
|
|
|
* Link to DB which used by this EntityModel |
26
|
|
|
* |
27
|
|
|
* @var \db_mysql $dbStatic |
28
|
|
|
* deprecated - replace with container ID like 'db' or 'dbAuth' |
29
|
|
|
*/ |
30
|
|
|
protected static $dbStatic; |
31
|
|
|
/** |
32
|
|
|
* Service to work with rows |
33
|
|
|
* |
34
|
|
|
* @var \DbRowDirectOperator $rowOperator |
35
|
|
|
*/ |
36
|
|
|
protected static $rowOperator; |
37
|
|
|
/** |
38
|
|
|
* Name of table for this entity |
39
|
|
|
* |
40
|
|
|
* @var string $tableName |
41
|
|
|
*/ |
42
|
|
|
protected $tableName = '_table'; |
43
|
|
|
/** |
44
|
|
|
* Name of key field field in this table |
45
|
|
|
* |
46
|
|
|
* @var string $idField |
47
|
|
|
*/ |
48
|
|
|
protected $idField = 'id'; |
49
|
|
|
/** |
50
|
|
|
* Property list and description |
51
|
|
|
* |
52
|
|
|
* propertyName => array( |
53
|
|
|
* P_DB_FIELD => 'dbFieldName', - directly converts property to field and vice versa |
54
|
|
|
* ) |
55
|
|
|
* |
56
|
|
|
* @var array[] $properties |
57
|
|
|
*/ |
58
|
|
|
protected $properties = array(); |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* BuddyContainer constructor. |
63
|
|
|
* |
64
|
|
|
* @param GlobalContainer $gc |
65
|
|
|
*/ |
66
|
|
|
public function __construct($gc) { |
67
|
|
|
// TODO - remove. No dependenceon container - we should extract all needed info here |
68
|
|
|
$this->gc = $gc; |
69
|
|
|
$this->model = new static::$modelClass($gc); |
70
|
|
|
static::$dbStatic = $gc->db; |
|
|
|
|
71
|
|
|
static::$rowOperator = $gc->dbRowOperator; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return EntityModel |
76
|
|
|
*/ |
77
|
|
|
public function getModel() { |
78
|
|
|
return $this->model; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return \db_mysql |
83
|
|
|
*/ |
84
|
|
|
public function getDbStatic() { |
85
|
|
|
return static::$dbStatic; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function setTableName($value) { |
89
|
|
|
$this->tableName = $value; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getTableName() { |
93
|
|
|
return $this->tableName; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setIdField($value) { |
97
|
|
|
$this->idField = $value; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getIdFieldName() { |
101
|
|
|
return $this->idField; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function importRow($row) { |
105
|
|
|
$this->clear(); |
106
|
|
|
|
107
|
|
|
if (empty($row)) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
foreach ($this->properties as $propertyName => $propertyData) { |
|
|
|
|
112
|
|
|
if (is_callable($this->accessors[P_CONTAINER_IMPORTER][$propertyName])) { |
113
|
|
|
call_user_func_array($this->accessors[P_CONTAINER_IMPORTER][$propertyName], array(&$row)); |
114
|
|
|
} elseif (!empty($propertyData[P_DB_FIELD])) { |
115
|
|
|
$this->$propertyName = $row[$propertyData[P_DB_FIELD]]; |
116
|
|
|
} |
117
|
|
|
// Otherwise it's internal field - filled and used internally |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function exportRow($withDbId = self::ENTITY_DB_ID_INCLUDE) { |
122
|
|
|
$row = array(); |
123
|
|
View Code Duplication |
foreach ($this->properties as $propertyName => $propertyData) { |
|
|
|
|
124
|
|
|
if (is_callable($this->accessors[P_CONTAINER_EXPORTER][$propertyName])) { |
125
|
|
|
call_user_func_array($this->accessors[P_CONTAINER_EXPORTER][$propertyName], array(&$row)); |
|
|
|
|
126
|
|
|
} elseif (!empty($propertyData[P_DB_FIELD])) { |
127
|
|
|
$row[$propertyData[P_DB_FIELD]] = $this->$propertyName; |
128
|
|
|
} |
129
|
|
|
// Otherwise it's internal field - filled and used internally |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($withDbId == self::ENTITY_DB_ID_EXCLUDE) { |
133
|
|
|
unset($row[$this->getIdFieldName()]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $row; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function exportRowWithoutId() { |
143
|
|
|
return $this->exportRow(self::ENTITY_DB_ID_EXCLUDE); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function exportRowWithId() { |
150
|
|
|
return $this->exportRow(self::ENTITY_DB_ID_INCLUDE); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// TODO - load from self DB |
154
|
|
|
public function loadTry() { |
155
|
|
|
$row = static::$rowOperator->getById($this); |
156
|
|
|
|
157
|
|
|
if (empty($row)) { |
158
|
|
|
$this->dbId = 0; |
|
|
|
|
159
|
|
|
|
160
|
|
|
return false; |
161
|
|
|
} else { |
162
|
|
|
$this->importRow($row); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return true; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function isEmpty() { |
169
|
|
|
// TODO - empty container - only properties |
170
|
|
|
return empty($this->dbId); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function isNew() { |
174
|
|
|
return empty($this->dbId); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function insert(){ |
178
|
|
|
static::$rowOperator->insert($this); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function delete(){ |
182
|
|
|
static::$rowOperator->deleteById($this); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
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.