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