|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
MIT License |
|
4
|
|
|
Copyright (c) 2010 - 2018 Peter Petermann |
|
5
|
|
|
|
|
6
|
|
|
Permission is hereby granted, free of charge, to any person |
|
7
|
|
|
obtaining a copy of this software and associated documentation |
|
8
|
|
|
files (the "Software"), to deal in the Software without |
|
9
|
|
|
restriction, including without limitation the rights to use, |
|
10
|
|
|
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
copies of the Software, and to permit persons to whom the |
|
12
|
|
|
Software is furnished to do so, subject to the following |
|
13
|
|
|
conditions: |
|
14
|
|
|
|
|
15
|
|
|
The above copyright notice and this permission notice shall be |
|
16
|
|
|
included in all copies or substantial portions of the Software. |
|
17
|
|
|
|
|
18
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
19
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
20
|
|
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
21
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
22
|
|
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
23
|
|
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
24
|
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
25
|
|
|
OTHER DEALINGS IN THE SOFTWARE. |
|
26
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
|
|
namespace King23\Mongo; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Base class to handle objects stored in MongoDB |
|
32
|
|
|
* |
|
33
|
|
|
* @throws Exception |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract class MongoObject implements \IteratorAggregate, \ArrayAccess |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var \MongoCollection collection used by instance |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $myCollection; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var String |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $myCollectionName; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array container to store the data of this object |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $myData = null; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var MongoServiceInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $myFactory; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* constructor, meant to setup the object, should be called by derived classes |
|
59
|
|
|
* |
|
60
|
|
|
* @param MongoServiceInterface $factory |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(MongoServiceInterface $factory) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->myFactory = $factory; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* sets the collection name - this will call |
|
69
|
|
|
* __wakeup to ensure the right collection is loaded, |
|
70
|
|
|
* use with care. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $collectioName |
|
73
|
|
|
* @throws \MongoException |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setCollection($collectioName) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->myCollectionName = $collectioName; |
|
78
|
|
|
$this->__wakeup(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* load data from array |
|
83
|
|
|
* |
|
84
|
|
|
* @param $data |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
public function loadFromArray(array $data) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->myData = $data; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* remove the instance from the mongodb - this will not kill the object in local space however |
|
94
|
|
|
* |
|
95
|
|
|
* @return void |
|
96
|
|
|
* @throws \MongoCursorException |
|
97
|
|
|
* @throws \MongoCursorTimeoutException |
|
98
|
|
|
*/ |
|
99
|
|
|
public function delete() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->myCollection->remove(['_id' => $this->myData['_id']]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* save the object in the mongodb, will insert on new object, or update if _id is set |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
* @throws \MongoCursorException |
|
109
|
|
|
* @throws \MongoCursorTimeoutException |
|
110
|
|
|
* @throws \MongoException |
|
111
|
|
|
*/ |
|
112
|
|
|
public function save() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->myCollection->save($this->myData); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* refreshes object from database (all changes be lost!) |
|
119
|
|
|
* |
|
120
|
|
|
* @return void |
|
121
|
|
|
*/ |
|
122
|
|
|
public function refresh() |
|
123
|
|
|
{ |
|
124
|
|
|
if ($data = $this->myCollection->findOne(['_id' => $this->myData['_id']])) { |
|
125
|
|
|
$this->myData = $data; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
// --------------------------- Iterator Fun |
|
129
|
|
|
/** |
|
130
|
|
|
* @return \ArrayIterator |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getIterator() |
|
133
|
|
|
{ |
|
134
|
|
|
return new \ArrayIterator($this->myData); |
|
135
|
|
|
} |
|
136
|
|
|
// --------------------------- ARRAY ACCESS |
|
137
|
|
|
/** |
|
138
|
|
|
* @param string $offset |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public function offsetExists($offset) |
|
142
|
|
|
{ |
|
143
|
|
|
return isset($this->myData[$offset]); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param string $offset |
|
148
|
|
|
* @return mixed |
|
149
|
|
|
*/ |
|
150
|
|
|
public function offsetGet($offset) |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->myData[$offset]; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $offset |
|
157
|
|
|
*/ |
|
158
|
|
|
public function offsetUnset($offset) |
|
159
|
|
|
{ |
|
160
|
|
|
unset($this->myData[$offset]); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param $offset |
|
165
|
|
|
* @param $value |
|
166
|
|
|
* @return mixed |
|
167
|
|
|
*/ |
|
168
|
|
|
public function offsetSet($offset, $value) |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->myData[$offset] = $value; |
|
171
|
|
|
} |
|
172
|
|
|
// --------------------------- OBJECT STYLE ACCESS |
|
173
|
|
|
/** |
|
174
|
|
|
* @param string $name |
|
175
|
|
|
* @param mixed $value |
|
176
|
|
|
* @return void |
|
177
|
|
|
*/ |
|
178
|
|
|
public function __set($name, $value) |
|
179
|
|
|
{ |
|
180
|
|
|
$this->myData[$name] = $value; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param string $name |
|
185
|
|
|
* @return mixed |
|
186
|
|
|
*/ |
|
187
|
|
|
public function __get($name) |
|
188
|
|
|
{ |
|
189
|
|
|
if (isset($this->myData[$name])) { |
|
190
|
|
|
return $this->myData[$name]; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return null; |
|
194
|
|
|
} |
|
195
|
|
|
// --------------------- unserialize |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Magic wakeup method, will reconnect object on unserialze |
|
199
|
|
|
* |
|
200
|
|
|
* @throws \MongoException |
|
201
|
|
|
* @throws \Exception |
|
202
|
|
|
* @return void |
|
203
|
|
|
*/ |
|
204
|
|
|
public function __wakeup() |
|
205
|
|
|
{ |
|
206
|
|
|
$this->myCollection = $this->myFactory->getDB()->selectCollection($this->myCollectionName); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|