This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
forked from tyohan/MongoRecord
-
Notifications
You must be signed in to change notification settings - Fork 58
/
EMongoEmbeddedDocument.php
351 lines (321 loc) · 7.63 KB
/
EMongoEmbeddedDocument.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
<?php
/**
* EMongoEmbeddedDocument.php
*
* PHP version 5.2+
*
* @author Dariusz Górecki <[email protected]>
* @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
* @copyright 2011 CleverIT http://www.cleverit.com.pl
* @license http://www.yiiframework.com/license/ BSD license
* @version 1.3
* @category ext
* @package ext.YiiMongoDbSuite
* @since v1.0.8
*/
/**
* @since v1.0.8
*/
abstract class EMongoEmbeddedDocument extends CModel
{
private static $_attributes=array();
/**
* CMap of embedded documents
* @var CMap $_embedded
* @since v1.0.8
*/
protected $_embedded=null;
/**
* Cacheed values for embeddedDocuments() method vall
* @var array $_embeddedConfig
* @since v1.3.2
*/
protected static $_embeddedConfig = array();
/**
* Hold down owner pointer (if any)
*
* @var EMongoEmbeddedDocument $_owner
* @since v1.0.8
*/
protected $_owner=null;
/**
* Constructor.
* @param string $scenario name of the scenario that this model is used in.
* See {@link CModel::scenario} on how scenario is used by models.
* @see getScenario
* @since v1.0.8
*/
public function __construct($scenario='insert')
{
$this->setScenario($scenario);
$this->init();
$this->attachBehaviors($this->behaviors());
$this->afterConstruct();
$this->initEmbeddedDocuments();
}
/**
* Initializes this model.
* This method is invoked in the constructor right after {@link scenario} is set.
* You may override this method to provide code that is needed to initialize the model (e.g. setting
* initial property values.)
* @since 1.0.8
*/
public function init(){}
/**
* @since v1.0.8
*/
protected function initEmbeddedDocuments()
{
if(!$this->hasEmbeddedDocuments() || !$this->beforeEmbeddedDocsInit())
return false;
$this->_embedded = new CMap;
if(!isset(self::$_embeddedConfig[get_class($this)]))
self::$_embeddedConfig[get_class($this)] = $this->embeddedDocuments();
$this->afterEmbeddedDocsInit();
}
/**
* @since v1.0.8
*/
public function onBeforeEmbeddedDocsInit($event)
{
$this->raiseEvent('onBeforeEmbeddedDocsInit', $event);
}
/**
* @since v1.0.8
*/
public function onAfterEmbeddedDocsInit($event)
{
$this->raiseEvent('onAfterEmbeddedDocsInit', $event);
}
/**
* @since v1.0.8
*/
public function onBeforeToArray($event)
{
$this->raiseEvent('onBeforeToArray', $event);
}
/**
* @since v1.0.8
*/
public function onAfterToArray($event)
{
$this->raiseEvent('onAfterToArray', $event);
}
/**
* @since v1.0.8
*/
protected function beforeToArray()
{
$event = new CModelEvent($this);
$this->onBeforeToArray($event);
return $event->isValid;
}
/**
* @since v1.0.8
*/
protected function afterToArray()
{
$this->onAfterToArray(new CModelEvent($this));
}
/**
* @since v1.0.8
*/
protected function beforeEmbeddedDocsInit()
{
$event=new CModelEvent($this);
$this->onBeforeEmbeddedDocsInit($event);
return $event->isValid;
}
/**
* @since v1.0.8
*/
protected function afterEmbeddedDocsInit()
{
$this->onAfterEmbeddedDocsInit(new CModelEvent());
}
/**
* @since v1.0.8
*/
public function __get($name)
{
if($this->hasEmbeddedDocuments() && isset(self::$_embeddedConfig[get_class($this)][$name])) {
// Late creation of embedded documents on first access
if (is_null($this->_embedded->itemAt($name))) {
$docClassName = self::$_embeddedConfig[get_class($this)][$name];
$doc = new $docClassName($this->getScenario());
$doc->setOwner($this);
$this->_embedded->add($name, $doc);
}
return $this->_embedded->itemAt($name);
}
else
return parent::__get($name);
}
/**
* @since v1.0.8
*/
public function __set($name, $value)
{
if($this->hasEmbeddedDocuments() && isset(self::$_embeddedConfig[get_class($this)][$name]))
{
if(is_array($value)) {
// Late creation of embedded documents on first access
if (is_null($this->_embedded->itemAt($name))) {
$docClassName = self::$_embeddedConfig[get_class($this)][$name];
$doc = new $docClassName($this->getScenario());
$doc->setOwner($this);
$this->_embedded->add($name, $doc);
}
return $this->_embedded->itemAt($name)->attributes=$value;
}
else if($value instanceof EMongoEmbeddedDocument)
return $this->_embedded->add($name, $value);
}
else
parent::__set($name, $value);
}
/**
* @since v1.3.2
* @see CComponent::__isset()
*/
public function __isset($name) {
if($this->hasEmbeddedDocuments() && isset(self::$_embeddedConfig[get_class($this)][$name]))
{
return isset($this->_embedded[$name]);
}
else
return parent::__isset($name);
}
/**
* @since v1.0.8
*/
public function afterValidate()
{
if($this->hasEmbeddedDocuments())
foreach($this->_embedded as $doc)
{
if(!$doc->validate())
{
$this->addErrors($doc->getErrors());
}
}
}
/**
* @since v1.0.8
*/
public function embeddedDocuments()
{
return array();
}
/**
* @since v1.0.8
*/
public function hasEmbeddedDocuments()
{
if(isset(self::$_embeddedConfig[get_class($this)]))
return true;
return count($this->embeddedDocuments()) > 0;
}
/**
* Returns the list of attribute names.
* By default, this method returns all public properties of the class.
* You may override this method to change the default.
* @return array list of attribute names. Defaults to all public properties of the class.
* @since v1.0.8
*/
public function attributeNames()
{
$className=get_class($this);
if(!isset(self::$_attributes[$className]))
{
$class=new ReflectionClass(get_class($this));
$names=array();
foreach($class->getProperties() as $property)
{
$name=$property->getName();
if($property->isPublic() && !$property->isStatic())
$names[]=$name;
}
if($this->hasEmbeddedDocuments())
{
$names = array_merge($names, array_keys(self::$_embeddedConfig[get_class($this)]));
}
return self::$_attributes[$className]=$names;
}
else
return self::$_attributes[$className];
}
/**
* Returns the given object as an associative array
* Fires beforeToArray and afterToArray events
* @return array an associative array of the contents of this object
* @since v1.0.8
*/
public function toArray()
{
if($this->beforeToArray())
{
$arr = $this->_toArray();
$this->afterToArray();
return $arr;
}
else
return array();
}
/**
* This method does the actual convertion to an array
* Does not fire any events
* @return array an associative array of the contents of this object
* @since v1.3.4
*/
protected function _toArray()
{
$arr = array();
$class=new ReflectionClass(get_class($this));
foreach($class->getProperties() as $property)
{
$name=$property->getName();
if($property->isPublic() && !$property->isStatic())
$arr[$name] = $this->$name;
}
if($this->hasEmbeddedDocuments())
foreach($this->_embedded as $key=>$value)
$arr[$key]=$value->toArray();
return $arr;
}
/**
* Return owner of this document
* @return EMongoEmbeddedDocument
* @since v1.0.8
*/
public function getOwner()
{
if($this->_owner!==null)
return $this->_owner;
else
return null;
}
/**
* Set owner of this document
* @param EMongoEmbeddedDocument $owner
* @since v1.0.8
*/
public function setOwner(EMongoEmbeddedDocument $owner)
{
$this->_owner = $owner;
}
/**
* Override default seScenario method for populating to embedded records
* @see CModel::setScenario()
* @since v1.0.8
*/
public function setScenario($value)
{
if($this->hasEmbeddedDocuments() && $this->_embedded !== null)
{
foreach($this->_embedded as $doc)
$doc->setScenario($value);
}
parent::setScenario($value);
}
}