|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class Entity |
|
5
|
|
|
* |
|
6
|
|
|
* @property int|float $dbId Buddy record DB ID |
|
7
|
|
|
*/ |
|
8
|
|
|
class Entity { |
|
9
|
|
|
public static $tableName = '_table'; |
|
10
|
|
|
/** |
|
11
|
|
|
* Name of key field field in this table |
|
12
|
|
|
* |
|
13
|
|
|
* @var string $idField |
|
14
|
|
|
*/ |
|
15
|
|
|
public static $idField = 'id'; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var PropertyHider |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $_container; |
|
20
|
|
|
protected static $_containerName = 'PropertyHiderInArray'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Property list |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected static $_properties = array(); |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var db_mysql|null $dbStatic |
|
31
|
|
|
*/ |
|
32
|
|
|
public static $dbStatic = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array $row |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $row = array(); |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var int|float|string $dbId |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $dbId = 0; |
|
44
|
|
|
|
|
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[static::$idField]); |
|
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 (!empty(static::$idField)) { |
|
98
|
|
|
$this->setDbId($row[static::$idField]); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Compiles object data into db row |
|
104
|
|
|
* |
|
105
|
|
|
* @param bool $withDbId - Should dbId too be returned. Usefull for INSERT statements |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getRow($withDbId = true) { |
|
110
|
|
|
$row = $this->row; |
|
111
|
|
|
if (!$withDbId) { |
|
112
|
|
|
unset($row[static::$idField]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $row; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function setDbId($value) { |
|
119
|
|
|
$this->dbId = $value; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return int|float|string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getDbId() { |
|
126
|
|
|
return $this->dbId; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
public function __get($name) { |
|
132
|
|
|
return $this->_container->$name; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function __set($name, $value) { |
|
136
|
|
|
$this->_container->$name = $value; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function __isset($name) { |
|
140
|
|
|
return isset($this->_container->$name); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function __unset($name) { |
|
144
|
|
|
unset($this->_container->$name); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
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.