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
|
|
|
|
9
|
|
|
class MongoDbDriver extends AbstractDriver |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var MongoDBClient |
13
|
|
|
*/ |
14
|
|
|
private $connection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Database |
18
|
|
|
*/ |
19
|
|
|
private $database; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Driver constructor. |
23
|
|
|
* |
24
|
|
|
* @param MongoDBClient $connection |
25
|
|
|
* @param string $database |
26
|
|
|
*/ |
27
|
12 |
|
public function __construct(MongoDBClient $connection, $database) |
28
|
|
|
{ |
29
|
12 |
|
$this->connection = $connection; |
30
|
12 |
|
$this->database = $connection->selectDatabase($database); |
31
|
12 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return \MongoDB\Database |
35
|
|
|
*/ |
36
|
5 |
|
public function getDatabase() |
37
|
|
|
{ |
38
|
5 |
|
return $this->database; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param int|string $id |
43
|
|
|
* |
44
|
|
|
* @return ObjectID |
45
|
|
|
*/ |
46
|
2 |
|
public function createObjectId($id) |
47
|
|
|
{ |
48
|
2 |
|
return new ObjectID($id); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return MongoDBClient |
53
|
|
|
*/ |
54
|
1 |
|
public function getConnection() |
55
|
|
|
{ |
56
|
1 |
|
return $this->connection; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param \Iterator $cursor |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
1 |
|
public function fetch(\Iterator $cursor) |
65
|
|
|
{ |
66
|
1 |
|
$data = null; |
67
|
1 |
|
if ($cursor->valid()) { |
68
|
1 |
|
$data = $cursor->current(); |
69
|
1 |
|
$cursor->next(); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
1 |
|
return $data; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $collection |
77
|
|
|
* @param array $query |
78
|
|
|
* |
79
|
|
|
* @return \Iterator |
80
|
|
|
*/ |
81
|
2 |
|
public function find($collection, array $query = []) |
82
|
|
|
{ |
83
|
2 |
|
$cursor = $this->getDatabase()->selectCollection($collection)->find($query); |
84
|
2 |
|
$iterator = new \ArrayIterator($cursor); |
85
|
2 |
|
$iterator->rewind(); |
86
|
|
|
|
87
|
2 |
|
return $iterator; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $collection |
92
|
|
|
* @param $document |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
1 |
|
public function insert($collection, $document) |
97
|
|
|
{ |
98
|
1 |
|
$result = $this->getDatabase()->selectCollection($collection)->insertOne($document); |
99
|
1 |
|
$document->_id = $result->getInsertedId(); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
1 |
|
public function update($collection, $criteria, $document) |
103
|
|
|
{ |
104
|
1 |
|
$this->getDatabase()->selectCollection($collection)->updateOne($criteria, ['$set' => $document]); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $collection |
109
|
|
|
* @param array $criteria |
110
|
|
|
*/ |
111
|
1 |
|
public function remove($collection, $criteria) |
112
|
|
|
{ |
113
|
1 |
|
$this->getDatabase()->selectCollection($collection)->deleteOne($criteria); |
114
|
1 |
|
} |
115
|
|
|
} |
116
|
|
|
|