|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class Entity |
|
5
|
|
|
* |
|
6
|
|
|
* @property int|float $dbId Buddy record DB ID |
|
7
|
|
|
*/ |
|
8
|
|
|
class Entity { |
|
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 db_mysql|null $dbStatic |
|
38
|
|
|
*/ |
|
39
|
|
|
public static $dbStatic = null; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array $row |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $row = array(); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Buddy\Buddy constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Pimple\GlobalContainer $c |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct($c) { |
|
52
|
|
|
empty(static::$dbStatic) && !empty($c->db) ? static::$dbStatic = $c->db : false; |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$this->_container = new static::$_containerName(); |
|
55
|
|
|
$this->_container->setProperties(static::$_properties); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getTableName() { |
|
59
|
|
|
return static::$tableName; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getIdFieldName() { |
|
63
|
|
|
return static::$idField; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function load($buddyId) { |
|
67
|
|
|
classSupernova::$gc->dbRowOperator->getById($this, $buddyId); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// TODO - move to reader ???????? |
|
|
|
|
|
|
71
|
|
|
public function delete() { |
|
72
|
|
|
return classSupernova::$gc->dbRowOperator->deleteById($this); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return int|string |
|
77
|
|
|
*/ |
|
78
|
|
|
// TODO - move to reader ???????? |
|
|
|
|
|
|
79
|
|
|
public function insert() { |
|
80
|
|
|
return classSupernova::$gc->dbRowOperator->insert($this); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function isEmpty() { |
|
84
|
|
|
return empty($this->row); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function isNew() { |
|
88
|
|
|
return empty($this->row[$this->getIdFieldName()]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param array $row |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setRow($row) { |
|
95
|
|
|
// $this->row = $row; |
|
|
|
|
|
|
96
|
|
|
// TODO - $row can be empty |
|
97
|
|
|
if ($this->getIdFieldName() != '') { |
|
98
|
|
|
$this->dbId = $row[$this->getIdFieldName()]; |
|
99
|
|
|
unset($row[$this->getIdFieldName()]); |
|
100
|
|
|
} |
|
101
|
|
|
foreach($row as $fieldName => $fieldValue) { |
|
102
|
|
|
$this->$fieldName = $fieldValue; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Compiles object data into db row |
|
108
|
|
|
* |
|
109
|
|
|
* @param bool $withDbId - Should dbId too be returned. Useful for INSERT statements |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getRow($withDbId = true) { |
|
114
|
|
|
// $row = $this->row; |
|
|
|
|
|
|
115
|
|
|
$row = array(); |
|
116
|
|
|
foreach($this->_container->getProperties() as $fieldName => $cork) { |
|
117
|
|
|
$row[$fieldName] = $this->$fieldName; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if (!$withDbId) { |
|
121
|
|
|
unset($row[$this->getIdFieldName()]); |
|
122
|
|
|
unset($row['dbId']); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $row; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function __get($name) { |
|
129
|
|
|
return $this->_container->$name; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function __set($name, $value) { |
|
133
|
|
|
$this->_container->$name = $value; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function __isset($name) { |
|
137
|
|
|
return isset($this->_container->$name); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function __unset($name) { |
|
141
|
|
|
unset($this->_container->$name); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
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.