My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
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
<?php
/**
* @package Morph
* @author Jonathan Moss <xirisr@gmail.com>
* @copyright Jonathan Moss 2009
*/

/**
* A base class for creating model objects for storing in MongoDB.
*
* @package Morph
*/
class Morph_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 current state of this object
*
* one of Morph_Enum::STATE_*
* @var string
*/
protected $State;

/**
* 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
* @return Morph_Object
*/
public function __construct($id = null)
{
$this->State = Morph_Enum::STATE_NEW;
$this->Id = $id;
$this->propertySet = new Morph_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 = Morph_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->State;
}

/**
* Adds a new property to this object
*
* @param Morph_Property_Generic $property
* @return Morph_Object
*/
protected function addProperty(Morph_Property_Generic $property)
{
$this->propertySet[$property->getName()] = $property;
return $this;
}

/**
* Sets the property data for this object
*
* @param $data
* @param $state
* @return Morph_Object
*/
public function __setData(array $data, $state = Morph_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);
}
$this->State = $state;
return $this;
}

/**
* Gets the property data for this object
*
* @return array
*/
public function __getData()
{
$data = array();
if (!is_null($this->Id)) {
$data['_id'] = $this->Id;
}
$data['_ns'] = get_class($this);
foreach($this->propertySet as $property) {
$data[$property->getName()] = $property->__getRawValue();
}
return $data;
}

// ********************** //
// MAGIC ACCESS FUNCTIONS //
// ********************** //

/**
*
* @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;
}

/**
*
* @param $propertyName
* @param $propertyValue
* @return Morph_Object
*/
public function __set($propertyName, $propertyValue)
{
if (array_key_exists($propertyName, $this->propertySet)) {
$this->propertySet[$propertyName]->setValue($propertyValue);
if ($this->State == Morph_Enum::STATE_CLEAN) {
$this->State = Morph_Enum::STATE_DIRTY;
}
}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;
}

// ********************* //
// PERSISTANCE FUNCTIONS //
// ********************* //

/**
* Saves this object
*
* @return Morph_Object
*/
public function save()
{
return Morph_Storage::instance()->save($this);
}

/**
* Attempts to load the current object with data from the document id specified
*
* @param mixed $id
* @return Morph_Object
*/
public function loadById($id)
{
return Morph_Storage::instance()->fetchById($this, $id);
}

/**
* Fetch multiple objects by their ids
*
* @param array $ids
* @return Morph_Iterator
*/
public function findByIds(array $ids)
{
return Morph_Storage::instance()->fetchByIds($this, $ids);
}

/**
* Find objects by query
*
* @param Morph_Query $query
* @return Morph_Iterator
*/
public function findByQuery(Morph_Query $query)
{
return Morph_Storage::instance()->findByQuery($this, $query);
}

/**
* Finds one object by query
*
* @param Morph_Query $query
* @return Morph_Object
*/
public function findOneByQuery(Morph_Query $query)
{
return Morph_Storage::instance()->findOneByQuery($this, $query);
}

/**
* Deletes this object from the database
*
* @return boolean
*/
public function delete()
{
return Morph_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()
{
$string = "";
$string .= "Id: " . $this->id() . "\n";
$string .= "State: " . $this->state() . "\n";
foreach ($this->propertySet as $property) {
$string .= (string)$property . "\n";
}
return $string;
}

/**
* 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 Morph_Object):
if ($depth > 0) {
$content[$value->getName()] = $value->__toArray($depth);
}
break;
case ($value instanceof Morph_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;
}
}

Change log

r142 by Xirisr on Jan 26, 2011   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r138 by XirisR on Mar 28, 2010   Diff
fixing typo in variable name
preventing findByIds from working
r132 by XirisR on Mar 2, 2010   Diff
adding delete method
r127 by XirisR on Feb 9, 2010   Diff
correcting typo and adding
Morph_Object::findOneByQuery()
All revisions of this file

File info

Size: 8685 bytes, 333 lines
Powered by Google Project Hosting