/
Entity.php
295 lines (268 loc) · 6.76 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
288
289
290
291
292
293
294
295
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\ORM;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
class Entity implements \ArrayAccess {
/**
* Holds all properties and their values for this entity
*
* @var array
*/
protected $_properties = [];
/**
* Holds the name of the class for the instance object
*
* @var string
*/
protected $_className;
/**
* Holds a cached list of methods that exist in the instanced class
*
* @var array
*/
protected static $_accessors = [];
/**
* Initializes the internal properties of this entity out of the
* keys in an array
*
* ### Example:
*
* ``$entity = new Entity(['id' => 1, 'name' => 'Andrew'])``
*
* @param array $properties hash of properties to set in this entity
* @param boolean $useSetters whether use internal setters for properties or not
* @return void
*/
public function __construct(array $properties = [], $useSetters = true) {
$this->_className = get_class($this);
$this->set($properties, $useSetters);
}
/**
* Magic getter to access properties that has be set in this entity
*
* @param string $property name of the property to access
* @return mixed
*/
public function &__get($property) {
return $this->get($property);
}
/**
* Magic setter to add or edit a property in this entity
*
* @param string $property the name of the property to set
* @param mixed $value the value to set to the property
* @return void
*/
public function __set($property, $value) {
$this->set([$property => $value]);
}
/**
* Returns whether this entity contains a property named $property
* regardless of if it is empty.
*
* @see \Cake\ORM\Entity::has()
* @param string $property
* @return boolean
*/
public function __isset($property) {
return $this->has($property);
}
/**
* Removes a property from this entity
*
* @param string $property
* @return void
*/
public function __unset($property) {
$this->unsetProperty($property);
}
/**
* Sets a single property inside this entity.
*
* ### Example:
*
* ``$entity->set('name', 'Andrew');``
*
* It is also possible to mass-assign multiple properties to this entity
* with one call by passing a hashed array as properties in the form of
* property => value pairs
*
* ## Example:
*
* {{
* $entity->set(['name' => 'andrew', 'id' => 1]);
* echo $entity->name // prints andrew
* echo $entity->id // prints 1
* }}
*
* Some times it is handy to bypass setter functions in this entity when assigning
* properties. You can achieve this by setting the third argument to false when
* assigning a single property or the second param when using an array of
* properties.
*
* ### Example:
*
* ``$entity->set('name', 'Andrew', false);``
*
* ``$entity->set(['name' => 'Andrew', 'id' => 1], false);``
*
* @param string|array $property the name of property to set or a list of
* properties with their respective values
* @param mixed|boolean $value the value to set to the property or a boolean
* signifying whether to use internal setter functions or not
* @param boolean $useSetters whether to use setter functions in this object
* or bypass them
* @return \Cake\ORM\Entity
*/
public function set($property, $value = true, $useSetters = true) {
if (is_string($property)) {
$property = [$property => $value];
} else {
$useSetters = $value;
}
if (!$useSetters) {
$this->_properties = $property + $this->_properties;
return $this;
}
foreach ($property as $p => $value) {
$setter = 'set' . Inflector::camelize($p);
if ($this->_methodExists($setter)) {
$value = $this->{$setter}($value);
}
$this->_properties[$p] = $value;
}
return $this;
}
/**
* Returns the value of a property by name
*
* @param string $property the name of the property to retrieve
* @return mixed
*/
public function &get($property) {
$method = 'get' . Inflector::camelize($property);
$value = null;
if (isset($this->_properties[$property])) {
$value =& $this->_properties[$property];
}
if ($this->_methodExists($method)) {
$value = $this->{$method}($value);
}
return $value;
}
/**
* Returns whether this entity contains a property named $property
* regardless of if it is empty.
*
* ### Example:
*
* {{{
* $entity = new Entity(['id' => 1, 'name' => null]);
* $entity->has('id'); // true
* $entity->has('name'); // false
* $entity->has('last_name'); // false
* }}}
*
* @param string $property
* @return boolean
*/
public function has($property) {
return $this->get($property) !== null;
}
/**
* Removes a property or list of properties from this entity
*
* ### Examples:
*
* ``$entity->unsetProperty('name');``
*
* ``$entity->unsetProperty(['name', 'last_name']);``
*
* @param string|array $property
* @return \Cake\ORM\
*/
public function unsetProperty($property) {
$property = (array)$property;
foreach ($property as $p) {
unset($this->_properties[$p]);
}
return $this;
}
/**
* Returns an array with all the properties that have been set
* to this entity
*
* @return array
*/
public function toArray() {
$result = [];
foreach ($this->_properties as $property => $value) {
$result[$property] = $this->get($property);
}
return $result;
}
/**
* Implements isset($entity);
*
* @param mixed $offset
* @return void
*/
public function offsetExists($offset) {
return $this->has($offset);
}
/**
* Implements $entity[$offset];
*
* @param mixed $offset
* @return void
*/
public function &offsetGet($offset) {
return $this->get($offset);
}
/**
* Implements $entity[$offset] = $value;
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value) {
$this->set([$offset => $value]);
}
/**
* Implements unset($result[$offset);
*
* @param mixed $offset
* @return void
*/
public function offsetUnset($offset) {
$this->unsetProperty($offset);
}
/**
* Determines whether a method exists in this class
*
* @param string $method the method to check for existence
* @return boolean true if method exists
*/
protected function _methodExists($method) {
if (empty(static::$_accessors[$this->_className])) {
static::$_accessors[$this->_className] = array_flip(get_class_methods($this));
}
return isset(static::$_accessors[$this->_className][$method]);
}
}