1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Respect\Structural\Driver\Mongo; |
4
|
|
|
|
5
|
|
|
use Respect\Data\CollectionIterator; |
6
|
|
|
use Respect\Data\Collections\Collection; |
7
|
|
|
use Respect\Structural\Driver as BaseDriver; |
8
|
|
|
|
9
|
|
|
class Driver implements BaseDriver |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \MongoClient |
13
|
|
|
*/ |
14
|
|
|
private $connection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \MongoDB |
18
|
|
|
*/ |
19
|
|
|
private $database; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Driver constructor. |
23
|
|
|
* @param \MongoClient $connection |
24
|
|
|
* @param string $database |
25
|
|
|
*/ |
26
|
4 |
|
public function __construct(\MongoClient $connection, $database) |
27
|
|
|
{ |
28
|
4 |
|
$this->connection = $connection; |
29
|
4 |
|
$this->database = $connection->{$database}; |
30
|
4 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return \MongoDB |
34
|
|
|
*/ |
35
|
|
|
public function getDatabase() |
36
|
|
|
{ |
37
|
|
|
return $this->database; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getConnection() |
41
|
|
|
{ |
42
|
|
|
return $this->connection; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param \Iterator $cursor |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function fetch(\Iterator $cursor) |
50
|
|
|
{ |
51
|
|
|
$cursor->next(); |
52
|
|
|
return $cursor->current(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $collection |
57
|
|
|
* @param array $query |
58
|
|
|
* @return \Iterator |
59
|
|
|
*/ |
60
|
|
|
public function find($collection, array $query = array()) |
61
|
|
|
{ |
62
|
|
|
return $this->getDatabase()->{$collection}->find($query); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Collection $collection |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
3 |
|
public function generateQuery(Collection $collection) |
70
|
|
|
{ |
71
|
3 |
|
return $this->parseConditions($collection); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Collection $collection |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
3 |
|
protected function parseConditions(Collection $collection) |
79
|
|
|
{ |
80
|
3 |
|
$allCollections = CollectionIterator::recursive($collection); |
81
|
3 |
|
$allCollections = iterator_to_array($allCollections); |
82
|
3 |
|
$allCollections = array_slice($allCollections, 1); |
83
|
|
|
|
84
|
3 |
|
$condition = $this->getConditionArray($collection); |
85
|
|
|
|
86
|
3 |
|
foreach ($allCollections as $name => $coll) |
|
|
|
|
87
|
3 |
|
$condition += $this->getConditionArray($coll, true); |
88
|
|
|
|
89
|
3 |
|
return $condition; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Collection $collection |
94
|
|
|
* @param bool|false $prefix |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
3 |
|
protected function getConditionArray(Collection $collection, $prefix = false) |
98
|
|
|
{ |
99
|
3 |
|
$condition = $collection->getCondition(); |
100
|
|
|
|
101
|
3 |
|
if (!is_array($condition)) { |
102
|
2 |
|
$condition = array('_id' => $this->createMongoId($condition)); |
103
|
2 |
|
} |
104
|
|
|
|
105
|
|
|
if ($prefix) |
|
|
|
|
106
|
3 |
|
$condition = static::prefixArrayKeys($condition, $collection->getName() . "."); |
107
|
|
|
|
108
|
3 |
|
return $condition; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $array |
113
|
|
|
* @param string $prefix |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
1 |
|
protected static function prefixArrayKeys(array $array, $prefix) |
117
|
|
|
{ |
118
|
1 |
|
$new = array(); |
119
|
|
|
|
120
|
1 |
|
foreach ($array as $key => $value) |
|
|
|
|
121
|
1 |
|
$new["{$prefix}{$key}"] = $value; |
122
|
|
|
|
123
|
1 |
|
return $new; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param int|string $id |
128
|
|
|
* @return \MongoId|\MongoInt32 |
129
|
|
|
*/ |
130
|
2 |
|
protected function createMongoId($id) |
131
|
|
|
{ |
132
|
2 |
|
if (is_int($id)) { |
133
|
1 |
|
return new \MongoInt32($id); |
134
|
|
|
} |
135
|
1 |
|
return new \MongoId($id); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param Collection $collection |
140
|
|
|
* @param $document |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
public function insert($collection, $document) |
144
|
|
|
{ |
145
|
|
|
$this->getDatabase()->{$collection}->insert($document); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function update($collection, $criteria, $document) |
149
|
|
|
{ |
150
|
|
|
$this->getDatabase()->{$collection}->update($criteria, $document); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function remove($collection, $criteria) |
154
|
|
|
{ |
155
|
|
|
$this->getDatabase()->{$collection}->remove($criteria); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Adding braces to control structures avoids accidental mistakes as your code changes: