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
/
EMongoSoftDocument.php
166 lines (153 loc) · 3.87 KB
/
EMongoSoftDocument.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
<?php
/**
* EMongoSoftDocument.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.3.4
*/
/**
* EmongoSoftDocument cass
* @since v1.3.4
*/
abstract class EMongoSoftDocument extends EMongoDocument
{
/**
* Array that holds initialized soft attributes
* @var array $softAttributes
* @since v1.3.4
*/
protected $softAttributes = array();
/**
* Adds soft attributes support to magic __get method
* @see EMongoEmbeddedDocument::__get()
* @since v1.3.4
*/
public function __get($name)
{
if(array_key_exists($name, $this->softAttributes)) // Use of array_key_exists is mandatory !!!
{
return $this->softAttributes[$name];
}
else
return parent::__get($name);
}
/**
* Adds soft attributes support to magic __set method
* @see EMongoEmbeddedDocument::__set()
* @since v1.3.4
*/
public function __set($name, $value)
{
if(array_key_exists($name, $this->softAttributes)) // Use of array_key_exists is mandatory !!!
{
$this->softAttributes[$name] = $value;
}
else
parent::__set($name, $value);
}
/**
* Adds soft attributes support to magic __isset method
* @see EMongoEmbeddedDocument::__isset()
* @since v1.3.4
*/
public function __isset($name)
{
if(array_key_exists($name, $this->softAttributes)) // Use of array_key_exists is mandatory !!!
return true;
else
return parent::__isset($name);
}
/**
* Adds soft attributes support to magic __unset method
* @see CComponent::__unset()
* @since v1.3.4
*/
public function __unset($name)
{
if(array_key_exists($name, $this->softAttributes)) // Use of array_key_exists is mandatory !!!
unset($this->softAttributes[$name]);
else
parent::__unset($name);
}
/**
* Initializes a soft attribute, before it can be used
* @param string $name attribute name
* @since v1.3.4
*/
public function initSoftAttribute($name)
{
if(!array_key_exists($name, $this->softAttributes))
$this->softAttributes[$name] = null;
}
/**
* Initializes a soft attributes, from given list, before they can be used
* @param mixed $attributes attribute names list
* @since v1.3.4
*/
public function initSoftAttributes($attributes)
{
foreach($attributes as $name)
$this->initSoftAttribute($name);
}
/**
* Return the list of attribute names of this model, with respect of initialized soft attributes
* @see EMongoEmbeddedDocument::attributeNames()
* @since v1.3.4
*/
public function attributeNames()
{
return array_merge(array_keys($this->softAttributes), parent::attributeNames());
}
/**
* Instantiate the model object from given document, with respect of soft attributes
* @see EMongoDocument::instantiate()
* @since v1.3.4
*/
protected function instantiate($attributes)
{
$class=get_class($this);
$model=new $class(null);
$model->initEmbeddedDocuments();
$model->initSoftAttributes(
array_diff(
array_keys($attributes),
parent::attributeNames()
)
);
$model->setAttributes($attributes, false);
return $model;
}
/**
* 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 = parent::_toArray();
foreach($this->softAttributes as $key => $value)
$arr[$key]=$value;
return $arr;
}
/**
* Return the actual list of soft attributes being used by this model
* @return array list of initialized soft attributes
* @since v1.3.4
*/
public function getSoftAttributeNames()
{
return array_diff(
array_keys($this->softAttributes),
parent::attributeNames()
);
}
}