/
Object.php
390 lines (355 loc) · 10.4 KB
/
Object.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* @package Morph
* @author Jonathan Moss <xirisr@gmail.com>
* @copyright Jonathan Moss 2009
*/
namespace morph;
/**
* A base class for creating model objects for storing in MongoDB.
*
* @package Morph
*/
class Object
{
/**
* The name of the collection this object is stored within
* @var string
*/
protected $collection;
/**
* The unique id for this object
* @var string
*/
protected $id;
/**
* The data associated with this object
* @var \morph\PropertySet
*/
protected $propertySet;
/**
* The collections of validators for propertys
* @var array
*/
protected $validators;
/**
* @param string $id If supplied this will be the id used to reference this object
*/
public function __construct($id = null)
{
$this->id = $id;
$this->propertySet = new PropertySet();
$this->validators = array();
}
// *********************** //
// GETTER/SETTER FUNCTIONS //
// *********************** //
/**
* Returns the name of the collection this object should be stored in
*
* @param string $collection
* @return string
*/
public function collection($collection = null)
{
if (!\is_null($collection)) {
$this->collection = $collection;
}elseif (\is_null($this->collection)) {
//defaults collection name to class name
$this->collection = Utils::collectionName($this);
}
return $this->collection;
}
/**
* Returns the Id of this object
* @return string
*/
public function id()
{
return $this->id;
}
/**
* Returns the state of this object
*
* Will be one of Morph_Enum::STATE_*
*
* @return string
*/
public function state()
{
return $this->propertySet->getState();
}
/**
* Adds a new property to this object
*
* @param \morph\property\Generic $property
* @param string $alias (Default: null)
* @return \morph\Object
*/
protected function addProperty(\morph\property\Generic $property, $alias = null)
{
$this->propertySet[$property->getName()] = $property;
if (null !== $alias) {
$this->propertySet->setStorageName($property->getName(), $alias);
}
return $this;
}
/**
* Sets the property data for this object
*
* @param array $data
* @param string $state (Default: Enum::STATE_DIRTY)
* @return \morph\Object
*/
public function __setData(array $data, $state = Enum::STATE_DIRTY)
{
if (\array_key_exists('_id', $data)) {
$this->id = $data['_id'];
unset($data['_id']);
}
if (\array_key_exists('_ns', $data)) {
unset($data['_ns']);
}
foreach ($data as $propertyName => $value) {
$this->propertySet->__setRawPropertyValue($propertyName, $value, $state);
}
return $this;
}
/**
* Gets the property data for this object
*
* @param boolean $whereNew Whether or not to only get dirty/new properties.
* @return array
*/
public function __getData($whereNew = false)
{
$data = array();
if (!is_null($this->id)) {
$data['_id'] = $this->id;
}
$data['_ns'] = \get_class($this);
foreach ($this->propertySet as $property) {
$storageName = $this->propertySet->getStorageName($property->getName());
if (!$whereNew) {
$data[$storageName] = $property->__getRawValue();
} else {
$state = $property->getState();
if ($state === \morph\Enum::STATE_DIRTY || $state === \morph\Enum::STATE_NEW) {
$data[$storageName] = $property->__getRawValue();
}
}
}
return $data;
}
/**
* Returns the PropertySet for this object
*
* @return \morph\PropertySet
*/
public function __getPropertySet()
{
return $this->propertySet;
}
// ********************** //
// MAGIC ACCESS FUNCTIONS //
// ********************** //
/**
* Gets a property by name if it exists. Issues an E_USER_WARNING if it does not exist.
*
* @param $propertyName
* @return mixed
*/
public function __get($propertyName)
{
$value = null;
if (\array_key_exists($propertyName, $this->propertySet)) {
$value = $this->propertySet[$propertyName]->getValue();
}else{
\trigger_error("The property $propertyName was not found in object of class " . \get_class($this), E_USER_WARNING);
}
return $value;
}
/**
* Sets a property by name and value
*
* @param string $propertyName
* @param string $propertyValue
* @return \morph\Object
*/
public function __set($propertyName, $propertyValue)
{
if (\array_key_exists($propertyName, $this->propertySet)) {
$this->propertySet[$propertyName]->setValue($propertyValue);
}else{
$this->addProperty(new \morph\property\Generic($propertyName, $propertyValue));
\trigger_error("The property $propertyName was not found in object of class " . \get_class($this) . ' but I have added it as a generic property type', E_USER_WARNING);
}
return $this;
}
/**
* Clones the current object
*/
public function __clone()
{
$this->id = null;
}
// ********************* //
// PERSISTANCE FUNCTIONS //
// ********************* //
/**
* Saves this object
*
* @param array $options Support the same options as MongoCollection::save()
* @return \morph\Object
*/
public function save(array $options = array())
{
return Storage::instance()->save($this, $options);
}
/**
* Attempts to load the current object with data from the document id specified
*
* By default Morph sets the id to be an instance of MongoId(). When searching you need
* to ensure you do the same by wrapping your id string in a MongoId object
*
* @param mixed $id
* @return Morph_Object
*/
public function loadById($id)
{
return Storage::instance()->fetchById($this, $id);
}
/**
* Fetch multiple objects by their ids
*
* By default Morph sets the id to be an instance of MongoId(). When searching you need
* to ensure you do the same by wrapping your id string in a MongoId object
*
* @param array $ids
* @return Morph_Iterator
*/
public function findByIds(array $ids)
{
return Storage::instance()->fetchByIds($this, $ids);
}
/**
* Find objects by query
*
* @param IQuery $query
* @return \morph\Iterator
*/
public function findByQuery(IQuery $query)
{
return Storage::instance()->findByQuery($this, $query);
}
/**
* Deletes objects by query
*
* @param IQuery $query
* @param boolean $safe
* @return boolean
*/
public function deleteByQuery(IQuery $query, $safe = null)
{
return Storage::instance()->findByQuery($this, $query, $safe);
}
/**
* Finds one object by query
*
* @param IQuery $query
* @return Object
*/
public function findOneByQuery(IQuery $query)
{
return Storage::instance()->findOneByQuery($this, $query);
}
/**
* Returns all entries for the current document
*
* @return \morph\Iterator
*/
public function fetchAll()
{
return Storage::instance()->findByQuery($this);
}
/**
* Deletes this object from the database
*
* @return boolean
*/
public function delete()
{
return Storage::instance()->delete($this);
}
// ***************** //
// UTILITY FUNCTIONS //
// ***************** //
/**
* Converts this object to a string
*
* Useful for debugging output but not much else
*
* @return string
*/
public function __toString()
{
// create the array that we will be encoding and returning
// also put inside the array the mongodb ID and the 'state'
$data = array(
'Id' => $this->id(),
'State' => $this->state()
);
// iterate through all the properties this object has and print them out
foreach ($this->propertySet as $name => $property) {
$data[$name] = (string)$property;
}
return \json_encode($data);
}
/**
* Returns the content of this object as an array suitable for merging with a template
*
* @param $depth The maximum depth to traverse the tree
* @return array
*/
public function __toArray($depth = 1)
{
$content = array();
if (!empty($this->id)) {
$content['_id'] = $this->id;
}
--$depth;
foreach ($this->propertySet as $property) {
$value = $property->getValue();
switch (true) {
case ($property instanceof \morph\property\ComposeOne):
$content[$value->getName()] = $value->__toArray($depth);
break;
case ($property instanceof morph\Property\ComposeMany):
$valueContents = array();
foreach ($value as $object) {
$valueContents[] = $object->__toArray($depth);
}
$content[$property->getName()] = $valueContents;
break;
case ($value instanceof Object):
if ($depth > 0) {
$content[$value->getName()] = $value->__toArray($depth);
}
break;
case ($value instanceof Collection):
if ($depth > 0) {
$valueContents = array();
foreach ($value as $object) {
$valueContents[] = $object->__toArray($depth);
}
$content[$property->getName()] = $valueContents;
}
break;
default:
$content[$property->getName()] = $value;
break;
}
}
return $content;
}
}