-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.php
340 lines (338 loc) · 9.75 KB
/
model.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
<?php
namespace ru\olamedia\kanon\mongo;
class model{
// Ready for database data array (original)
protected $_data = [];
// Ready for database data array (modified)
protected $_dirty = [];
protected $_references = [];
public function __construct(array $data = []){
$defaults = typeManager::getDefaults(\get_called_class());
if (\is_array($defaults)){
$data = \array_replace($defaults, $data);
}
$exists = isset($data['_id']) && (null !== $data['_id']);
if ($exists){
$this->_data = $exists?$data:[];
$this->_dirty = $exists?[]:$data;
}else{
foreach ($data as $k => $v){
$this->$k = $v;
}
}
}
// @final
public function exists(){
return isset($this->_data['_id']) && (null !== $this->_data['_id']);
}
public function update($data = []){
foreach ($data as $k => $v){
$this->$k = $v;
}
}
private static function _getPropertyType($name){
return typeManager::get(\get_called_class(), $name);
}
private static function stripNamespace($className){
$path = \explode('\\', $className);
return \end($path);
}
public function hasTypes(){
return typeManager::has(\get_called_class());
}
public function getTypes(){
return typeManager::getAll(\get_called_class());
}
public function getId(){
if (isset($this->_data['_id'])){
return new \MongoId($this->_data['_id']);
}
return null;
}
public static function getById($id){
if ($id && \strlen($id) == 24){
$id = new \MongoId($id);
}else{
return null;
}
$className = \get_called_class();
return $className::findOne([
"_id" => $id
]);
}
public static function id($id){
if ($id && \strlen($id) == 24){
$id = new \MongoId($id);
}else{
return null;
}
$className = \get_called_class();
return $className::findOne([
"_id" => $id
]);
}
// @final
public static function select(array $criteria = [], array $fields = []){
$className = \get_called_class();
if (typeManager::has($className)){
$criteria['_type'] = self::stripNamespace($className);
}
return collection::forClass($className)->find($criteria, $fields);
}
// @final
public static function find(array $criteria = [], array $fields = []){
$className = \get_called_class();
if (typeManager::has($className)){
$criteria['_type'] = self::stripNamespace($className);
}
$collection = collection::forClass($className)->select();
return new result($className, $collection->find($criteria, $fields));
}
// @final
public static function findOne(array $criteria = [], array $fields = []){
$className = \get_called_class();
if (typeManager::has($className)){
$criteria['_type'] = self::stripNamespace($className);
}
$collection = collection::forClass($className)->select();
$result = $collection->findOne($criteria, $fields);
return new $className($result);
}
// @final
public static function one(array $criteria = [], array $fields = []){
$className = \get_called_class();
if (typeManager::has($className)){
$criteria['_type'] = self::stripNamespace($className);
}
$collection = collection::forClass($className)->select();
$result = $collection->findOne($criteria, $fields);
return new $className($result);
}
// @final
public function delete(array $options = []){
$this->preDelete();
if ($this->exists()){
$deleted = collection::forClass(\get_called_class())->remove(array(
"_id" => $this->getId()
), $options);
if ($deleted){
unset($this->_data['$id']);
}
}
$this->postDelete();
return !$this->exists();
}
// @final
public function preDelete(){
}
// @final
public function postDelete(){
}
// @final
final public function save(array $options = []){
$this->_packReferences();
if ($this->exists() && empty($this->_dirty)) return true;
$this->preSave();
$collection = collection::forClass(\get_called_class());
if ($this->exists()){
$this->preUpdate();
$success = $collection->update(array(
'_id' => $this->getId()
), array(
'$set' => $this->_dirty
), $options);
$this->_dirty = [];
$this->postUpdate();
}else{
$this->preInsert();
//var_dump($this);
$collection->select()->insert($this->_data, $options);
//$this->_data['_id'] = (string) $this->_data['_id'];
//var_dump($this);
$this->_dirty = [];
$this->postInsert();
}
$this->postSave();
return $success;
}
// @final
public function preSave(){
}
// @final
public function postSave(){
}
// @final
public function preUpdate(){
}
// @final
public function postUpdate(){
}
// @final
public function preInsert(){
}
// @final
public function postInsert(){
}
private function _getData($name){
return isset($this->_data[$name])?$this->_data[$name]:null;
}
// References
private function _packReferences(){
foreach ($this->_references as $name => $ref){
// getReference triggers save() on the underlying model
$this->_dirty[$name] = $ref->getReference();
}
}
public function makeRef(){
$className =\get_called_class();
//$ref = \MongoDBRef::create($this->collectionName(), $this->getId(), $this->dbName());
return collection::forClass($className)->select()->createDBRef($this->_data);//->createReference($this->getId());
}
private function _makeReference($model){
$model = get_called_class();
$ref = \MongoDBRef::create($this->collectionName(), $this->getId(), $this->dbName());
return $ref;
}
private function _loadReference($name, $type){
if (!isset($this->_references[$name])){
$this->_references[$name] = null;
$model = $type['model'];
$referenceType = $type['type'];
if (\class_exists($model)){
$value = $this->_getData($name);
if ($referenceType == 'reference'){
if (\MongoDBRef::isRef($value)){
$this->_references[$name] = new reference($model,$value['$id']);
}else{
$this->_references[$name] = new reference($model,null);
}
}elseif ($referenceType == 'references'){
$this->_references[$name] = new referenceSet($model);
if (!empty($value)){
foreach ($value as $item){
$this->_references[$name][] = $item['$id'];
}
}
}
}
}
if (is_object($this->_references[$name])){
if ($this->_references[$name] instanceof reference){
return $this->_references[$name]->get();
}
}
return $this->_references[$name];
}
private function _getReferenceInternal($name){
if (!isset($this->_references[$name])){
$this->_references[$name] = new reference(\get_called_class(), null);
$value = $this->_getData($name);
if (\MongoDBRef::isRef($value)){
$this->_references[$name]->setId($value['$id']);
}
}
return $this->_references[$name];
}
private function _getReferencesInternal($name){
if (!isset($this->_references[$name])){
$type = self::_getPropertyType($name);
$model = $type['model'];
$this->_references[$name] = new referenceSet($model);
$value = $this->_getData($name);
if (!empty($value)){
foreach ($value as $item){
$this->_references[$name][] = $item['$id'];
}
}
}
return $this->_references[$name];
}
private function _setReference($name, $value, $type){
$model = $type['model'];
$referenceType = $type['type'];
if ($referenceType == 'reference'){
$reference = $this->_getReferenceInternal($name);
if ($value instanceof $model || $value == null){
$reference->setModel($value);
$this->_dirty[$name] = $reference;
}
}elseif ($referenceType == 'references'){
$references = $this->_getReferencesInternal($name);
if (is_array($value)){
foreach ($value as $item){
if ($item instanceof $model){
$references[] = $item;
}
}
}elseif (null === $value){
$references->clear();
$this->_dirty[$name] = $references;
}
}
}
// Convert value to MongoDB representation
private function _internalValue($name, $value, $type = null){
if ('_id' === $name){
// \MongoId
if (!($value instanceof \MongoId)){
return new \MongoId($value);
}else{
return $value;
}
}
if (null != $type){
$valueType = $type['type'];
if ('string' == $valueType){
$value = \strval($value);
}elseif ('boolean' == $valueType || 'bool' == $valueType){
$value = !!($value);
}elseif ('integer' == $valueType || 'int' == $valueType){
$value = \intval($value);
}elseif ('float' == $valueType || 'double' == $valueType){
$value = \doubleval($value);
}elseif ('timestamp' == $valueType){
// MongoTimestamp is used by sharding.
// If you're not looking to write sharding tools, what you probably want is MongoDate.
if (!($value instanceof \MongoTimestamp)){
$value = new \MongoTimestamp($value);
}
}elseif ('datetime' == $valueType || 'time' == $valueType || 'date' == $valueType){
if (!($value instanceof \MongoDate)){
$value = new \MongoDate($value); // FIXME parse float into usec
}
}elseif (\is_array($value)){
// check if is ref
if (\MongoDBRef::isRef($value)){
// which class??
}
}
}
$value = $this->processValue($name, $value);
return $value;
}
public function processValue($name, $value){
return $value;
}
// Magic methods
public function __get($name){
$type = typeManager::get(\get_called_class(), $name);
if (null != $type && isset($type['type']) && ($type['type'] == 'reference' || $type['type'] == 'references')){
return $this->_loadReference($name, $type);
}else if (\array_key_exists($name, $this->_data)){
return $this->_internalValue($name, $this->_data[$name], $type);
}
return null;
}
public function __set($name, $value){
$type = typeManager::get(\get_called_class(), $name);
if (null != $type && isset($type['type']) && ($type['type'] == 'reference' || $type['type'] == 'references')){
$value = $this->_setReference($name, $value, $type);
}
$value = $this->_internalValue($name, $value, $type);
if (isset($this->_data[$name]) && $this->_dirty[$name] === $value){
}else{
$this->_data[$name] = $value;
$this->_dirty[$name] = $value;
}
}
}