1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Respect\Structural\Driver\MongoDb; |
4
|
|
|
|
5
|
|
|
use Respect\Structural\Driver\Exception as DriverException; |
6
|
|
|
use MongoDB\Client as MongoDBClient; |
7
|
|
|
use Respect\Data\Collections\Collection; |
8
|
|
|
use Respect\Structural\Driver as BaseDriver; |
9
|
|
|
|
10
|
|
|
class Driver implements BaseDriver |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var MongoDBClient |
14
|
|
|
*/ |
15
|
|
|
private $connection; |
16
|
|
|
|
17
|
2 |
|
protected function __construct(BaseDriver $connection) |
18
|
|
|
{ |
19
|
2 |
|
$this->connection = $connection; |
|
|
|
|
20
|
2 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $database |
24
|
|
|
* @param string $server |
25
|
|
|
* @param array $options |
26
|
|
|
* @param array $driverOptions |
27
|
|
|
* |
28
|
|
|
* @return Driver |
29
|
|
|
* |
30
|
|
|
* @throws DriverException |
31
|
|
|
*/ |
32
|
1 |
|
public static function factoryLegacy( |
33
|
|
|
$database, |
34
|
|
|
$server = 'mongodb://localhost:27017', |
35
|
|
|
array $options = ['connect' => false], |
36
|
|
|
array $driverOptions = [] |
37
|
|
|
) { |
38
|
1 |
|
if (!extension_loaded('mongo')) { |
39
|
|
|
throw DriverException::extensionNotLoaded('mongo'); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
$client = new \MongoClient($server, $options, $driverOptions); |
43
|
1 |
|
$driver = new MongoDriver($client, $database); |
44
|
|
|
|
45
|
1 |
|
return new self($driver); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $database |
50
|
|
|
* @param string $uri |
51
|
|
|
* @param array $uriOptions |
52
|
|
|
* @param array $driverOptions |
53
|
|
|
* |
54
|
|
|
* @return Driver |
55
|
|
|
* |
56
|
|
|
* @throws DriverException |
57
|
|
|
*/ |
58
|
1 |
|
public static function factory( |
59
|
|
|
$database, |
60
|
|
|
$uri = 'mongodb://localhost:27017', |
61
|
|
|
array $uriOptions = [], |
62
|
|
|
array $driverOptions = [] |
63
|
|
|
) { |
64
|
1 |
|
if (!extension_loaded('mongodb')) { |
65
|
|
|
throw DriverException::extensionNotLoaded('mongodb'); |
66
|
|
|
} |
67
|
1 |
|
$client = new \MongoDB\Client($uri, $uriOptions, $driverOptions); |
68
|
1 |
|
$driver = new MongoDbDriver($client, $database); |
69
|
|
|
|
70
|
1 |
|
return new self($driver); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return Driver |
75
|
|
|
*/ |
76
|
2 |
|
public function getConnection() |
77
|
|
|
{ |
78
|
2 |
|
return $this->connection; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \Iterator $cursor |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
1 |
|
public function fetch(\Iterator $cursor) |
87
|
|
|
{ |
88
|
1 |
|
return $this->getConnection()->fetch($cursor); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $collection |
93
|
|
|
* @param array $query |
94
|
|
|
* |
95
|
|
|
* @return \Iterator |
96
|
|
|
*/ |
97
|
1 |
|
public function find($collection, array $query = []) |
98
|
|
|
{ |
99
|
1 |
|
return $this->getConnection()->find($collection, $query); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Collection $collection |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
1 |
|
public function generateQuery(Collection $collection) |
108
|
|
|
{ |
109
|
1 |
|
return $this->getConnection()->generateQuery($collection); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Collection $collection |
114
|
|
|
* @param $document |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
1 |
|
public function insert($collection, $document) |
119
|
|
|
{ |
120
|
1 |
|
$this->getConnection()->insert($collection, $document); |
|
|
|
|
121
|
1 |
|
} |
122
|
|
|
|
123
|
1 |
|
public function update($collection, $criteria, $document) |
124
|
|
|
{ |
125
|
1 |
|
$this->getConnection()->update($collection, $criteria, $document); |
|
|
|
|
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $collection |
130
|
|
|
* @param array $criteria |
131
|
|
|
*/ |
132
|
1 |
|
public function remove($collection, $criteria) |
133
|
|
|
{ |
134
|
1 |
|
$this->getConnection()->remove($collection, $criteria); |
|
|
|
|
135
|
1 |
|
} |
136
|
|
|
} |
137
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..