-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.php
287 lines (259 loc) · 6.52 KB
/
Entity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
* @file Entity.php
*
* Entity
*
* Abstract class provides a base for building model objects.
*
* Inheriting classes provide table name and column information and override getters and setters.
*
* copyright (c) 2003-2014 Frank Hellenkamp [[email protected]]
*/
namespace Depage\Entity;
abstract class Entity implements \JsonSerializable
{
// {{{ variables
/**
* Fields
*
* Array of table fields indexed on the column name.
* Values provide the PDO data type for binding to markers.
*
* @var array
*/
protected static $fields = array();
/**
* @brief initialized
**/
protected $initialized = false;
/**
* Data Array
*
* The data array accessed via the magic get / set functions.
*
* @var array
*/
protected $data = array();
/**
* Types Array
*
* The Types of the fields. Optional, add to enable strict type testing when
* setting fields
*
* @var array
*/
protected $types = array();
/**
* Dirty Data
*
* This array tracks which properties are dirty for saving.
* Bool array value indicates column state.
*
* @var array
*/
protected $dirty = array();
// }}}
// {{{ __constructor()
/**
* Constructor
*
* @return void
*/
public function __construct()
{
if (count($this->data) === 0) {
// new empty object with no data -> set defaults
foreach (static::$fields as $key => $value) {
$this->data[$key] = $value;
}
$this->dirty = array_fill_keys(array_keys(static::$fields), true);
} else {
// object initiated through pdo fetch, so data is already set
$this->dirty = array_fill_keys(array_keys(static::$fields), false);
}
$this->initialized = true;
}
// }}}
// {{{ __get()
/**
* Get
*
* Gets the propery from the data array if it exists.
*
* @param string $property
*
* @return mixed
*/
public function __get($key)
{
$getter = "get" . ucfirst($key);
if (method_exists($this, $getter)) {
return $this->$getter();
}
if (array_key_exists($key, $this->data)) {
return $this->data[$key];
}
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $key .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_ERROR);
return null;
}
// }}}
// {{{ __set()
/**
* Set
*
* Sets the data and dirty arrays if the data property exists and the data has changed.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function __set($key, $val)
{
$setter = "set" . ucfirst($key);
if ($this->initialized && method_exists($this, $setter)) {
return $this->$setter($val);
}
if (array_key_exists($key, $this->data) || !$this->initialized) {
// add value if property exists and is not primary
if (!in_array($key, static::$primary) || !$this->initialized) {
$this->dirty[$key] = (isset($this->dirty[$key]) && $this->dirty[$key] == true) || (
(isset($this->data[$key]) && $this->data[$key] != $val)
|| !isset($this->data[$key])
);
$this->data[$key] = $val;
}
return $this;
}
$trace = debug_backtrace();
trigger_error(
'Undefined property via __set(): ' . $key .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return $this;
}
// }}}
// {{{ __call()
/**
* Call
*
* Allows to set and get variables via setVarname and getVarname methods without
* declaring them explicitly
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function __call($name, $arguments)
{
$prefix = substr($name, 0, 3);
$key = lcFirst(substr($name, 3));
if ($prefix == "set") {
if (array_key_exists($key, static::$fields)) {
$this->$key = $arguments[0];
}
return $this;
} else if ($prefix == "get") {
if (array_key_exists($key, static::$fields)) {
return $this->$key;
}
}
$trace = debug_backtrace();
trigger_error(
'Undefined method via __call(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return false;
}
// }}}
// {{{ __isset()
/**
* IsSet
*
* Checks that the property exists.
*
* @param string $key
*
* @return mixed
*/
public function __isset($key)
{
$getter = "get" . ucfirst($key);
if (method_exists($this, $getter)) {
return true;
}
return (isset($this->data[$key]));
}
// }}}
// {{{ getFields()
/**
* @brief get field names that are defined in schema
*
* @param string $prefix = ""
* @return array of field names
**/
protected static function getFields($prefix = "")
{
$fields = array_keys(static::$fields);
if ($prefix !== "") {
$fields = array_map(function($val) use ($prefix) {
return $prefix . "." . $val;
}, $fields);
}
return $fields;
}
// }}}
// {{{ setData()
/**
* @brief setData
*
* Sets object data with data array instead of setting properties explicitly
*
* @param mixed $data
* @return void
**/
public function setData($data)
{
foreach ($data as $key => $value) {
$this->$key = $value;
}
return $this;
}
// }}}
// {{{ __sleep()
/**
* allows Depage\Db\Pdo-object to be serialized
*/
public function __sleep()
{
return array(
'initialized',
'data',
'types',
'dirty',
);
}
// }}}
// {{{ jsonSerialize()
/**
* @brief jsonSerialize
*
* @param mixed
* @return void
**/
public function jsonSerialize():mixed
{
return $this->data;
}
// }}}
}
// vim:set ft=php sw=4 sts=4 fdm=marker et :