1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Respect\Structural\Driver\MongoDb; |
4
|
|
|
|
5
|
|
|
use MongoDB\BSON\ObjectID; |
6
|
|
|
use MongoDB\Client as MongoDBClient; |
7
|
|
|
use Respect\Data\Collections\Collection; |
8
|
|
|
|
9
|
|
|
class MongoDbDriver extends AbstractDriver |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var MongoDBClient |
13
|
|
|
*/ |
14
|
|
|
private $connection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Driver constructor. |
18
|
|
|
* |
19
|
|
|
* @param MongoDBClient $connection |
20
|
|
|
* @param string $database |
21
|
|
|
*/ |
22
|
5 |
|
public function __construct(MongoDBClient $connection, $database) |
23
|
|
|
{ |
24
|
5 |
|
$this->connection = $connection; |
25
|
5 |
|
$this->database = $connection->selectDatabase($database); |
|
|
|
|
26
|
5 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return \MongoDB\Database |
30
|
|
|
*/ |
31
|
|
|
public function getDatabase() |
32
|
|
|
{ |
33
|
|
|
return $this->database; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param int|string $id |
38
|
|
|
* |
39
|
|
|
* @return ObjectID |
40
|
|
|
*/ |
41
|
2 |
|
public function createObjectId($id) |
42
|
|
|
{ |
43
|
2 |
|
return new ObjectID($id); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return MongoDBClient |
48
|
|
|
*/ |
49
|
|
|
public function getConnection() |
50
|
|
|
{ |
51
|
|
|
return $this->connection; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param \Iterator $cursor |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function fetch(\Iterator $cursor) |
60
|
|
|
{ |
61
|
|
|
$data = null; |
62
|
|
|
if ($cursor->valid()) { |
63
|
|
|
$data = $cursor->current(); |
64
|
|
|
$cursor->next(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $data; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $collection |
72
|
|
|
* @param array $query |
73
|
|
|
* |
74
|
|
|
* @return \Iterator |
75
|
|
|
*/ |
76
|
|
|
public function find($collection, array $query = []) |
77
|
|
|
{ |
78
|
|
|
$cursor = $this->getDatabase()->selectCollection($collection)->find($query); |
|
|
|
|
79
|
|
|
$iterator = new \IteratorIterator($cursor); |
80
|
|
|
$iterator->rewind(); |
81
|
|
|
|
82
|
|
|
return $iterator; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Collection $collection |
87
|
|
|
* @param $document |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function insert($collection, $document) |
92
|
|
|
{ |
93
|
|
|
$result = $this->getDatabase()->selectCollection($collection)->insertOne($document); |
|
|
|
|
94
|
|
|
$document->_id = $result->getInsertedId(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function update($collection, $criteria, $document) |
98
|
|
|
{ |
99
|
|
|
$this->getDatabase()->selectCollection($collection)->updateOne($criteria, ['$set' => $document]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $collection |
104
|
|
|
* @param array $criteria |
105
|
|
|
*/ |
106
|
|
|
public function remove($collection, $criteria) |
107
|
|
|
{ |
108
|
|
|
$this->getDatabase()->selectCollection($collection)->deleteOne($criteria); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: