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
|
|
|
* |
24
|
|
|
* @param \MongoClient $connection |
25
|
|
|
* @param string $database |
26
|
|
|
*/ |
27
|
4 |
|
public function __construct(\MongoClient $connection, $database) |
28
|
|
|
{ |
29
|
4 |
|
$this->connection = $connection; |
30
|
4 |
|
$this->database = $connection->{$database}; |
31
|
4 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return \MongoDB |
35
|
|
|
*/ |
36
|
|
|
public function getDatabase() |
37
|
|
|
{ |
38
|
|
|
return $this->database; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getConnection() |
42
|
|
|
{ |
43
|
|
|
return $this->connection; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param \Iterator $cursor |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function fetch(\Iterator $cursor) |
52
|
|
|
{ |
53
|
|
|
$cursor->next(); |
54
|
|
|
|
55
|
|
|
return $cursor->current(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $collection |
60
|
|
|
* @param array $query |
61
|
|
|
* |
62
|
|
|
* @return \Iterator |
63
|
|
|
*/ |
64
|
|
|
public function find($collection, array $query = []) |
65
|
|
|
{ |
66
|
|
|
return $this->getDatabase()->{$collection}->find($query); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Collection $collection |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
3 |
|
public function generateQuery(Collection $collection) |
75
|
|
|
{ |
76
|
3 |
|
return $this->parseConditions($collection); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Collection $collection |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
3 |
|
protected function parseConditions(Collection $collection) |
85
|
|
|
{ |
86
|
3 |
|
$allCollections = CollectionIterator::recursive($collection); |
87
|
3 |
|
$allCollections = iterator_to_array($allCollections); |
88
|
3 |
|
$allCollections = array_slice($allCollections, 1); |
89
|
|
|
|
90
|
3 |
|
$condition = $this->getConditionArray($collection); |
91
|
|
|
|
92
|
3 |
|
foreach ($allCollections as $coll) { |
93
|
1 |
|
$condition += $this->getConditionArray($coll, true); |
94
|
3 |
|
} |
95
|
|
|
|
96
|
3 |
|
return $condition; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Collection $collection |
101
|
|
|
* @param bool|false $prefix |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
3 |
|
protected function getConditionArray(Collection $collection, $prefix = false) |
106
|
|
|
{ |
107
|
3 |
|
$condition = $collection->getCondition(); |
108
|
|
|
|
109
|
3 |
|
if (!is_array($condition)) { |
110
|
2 |
|
$condition = ['_id' => $this->createMongoId($condition)]; |
111
|
2 |
|
} |
112
|
|
|
|
113
|
3 |
|
if ($prefix) { |
114
|
1 |
|
$condition = static::prefixArrayKeys($condition, $collection->getName() . '.'); |
115
|
1 |
|
} |
116
|
|
|
|
117
|
3 |
|
return $condition; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $array |
122
|
|
|
* @param string $prefix |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
1 |
|
protected static function prefixArrayKeys(array $array, $prefix) |
127
|
|
|
{ |
128
|
1 |
|
$new = []; |
129
|
|
|
|
130
|
1 |
|
foreach ($array as $key => $value) { |
131
|
1 |
|
$new["{$prefix}{$key}"] = $value; |
132
|
1 |
|
} |
133
|
|
|
|
134
|
1 |
|
return $new; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param int|string $id |
139
|
|
|
* |
140
|
|
|
* @return \MongoId|\MongoInt32 |
141
|
|
|
*/ |
142
|
2 |
|
protected function createMongoId($id) |
143
|
|
|
{ |
144
|
2 |
|
if (is_int($id)) { |
145
|
1 |
|
return new \MongoInt32($id); |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
return new \MongoId($id); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param Collection $collection |
153
|
|
|
* @param $document |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function insert($collection, $document) |
158
|
|
|
{ |
159
|
|
|
$this->getDatabase()->{$collection}->insert($document); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function update($collection, $criteria, $document) |
163
|
|
|
{ |
164
|
|
|
$this->getDatabase()->{$collection}->update($criteria, $document); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function remove($collection, $criteria) |
168
|
|
|
{ |
169
|
|
|
$this->getDatabase()->{$collection}->remove($criteria); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|