|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Database; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\ConnectionManagement; |
|
6
|
|
|
use ByJG\AnyDataset\Repository\ArrayDatasetIterator; |
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use MongoClient; |
|
9
|
|
|
use MongoCollection; |
|
10
|
|
|
use MongoDate; |
|
11
|
|
|
use MongoDB; |
|
12
|
|
|
use stdClass; |
|
13
|
|
|
|
|
14
|
|
|
class MongoDbDriver implements NoSqlDriverInterface |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var MongoDB |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $dataset = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* @var MongoClient; |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $mongoClient = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Enter description here... |
|
30
|
|
|
* |
|
31
|
|
|
* @var ConnectionManagement |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $connectionManagement; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* |
|
37
|
|
|
* @var MongoCollection MongoDB collection |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $collection; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $collectionName; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Creates a new MongoDB connection. This class is managed from NoSqlDataset |
|
49
|
|
|
* |
|
50
|
|
|
* @param ConnectionManagement $connMngt |
|
51
|
|
|
* @param string $collection |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct($connMngt, $collection) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->connectionManagement = $connMngt; |
|
56
|
|
|
|
|
57
|
|
|
$hosts = $this->connectionManagement->getServer(); |
|
58
|
|
|
$port = $this->connectionManagement->getPort() == '' ? 27017 : $this->connectionManagement->getPort(); |
|
59
|
|
|
$database = $this->connectionManagement->getDatabase(); |
|
60
|
|
|
$username = $this->connectionManagement->getUsername(); |
|
61
|
|
|
$password = $this->connectionManagement->getPassword(); |
|
62
|
|
|
|
|
63
|
|
|
if ($username != '' && $password != '') { |
|
64
|
|
|
$auth = array('username' => $username, 'password' => $password, 'connect' => 'true'); |
|
65
|
|
|
} else { |
|
66
|
|
|
$auth = array('connect' => 'true'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$connectString = sprintf('mongodb://%s:%d', $hosts, $port); |
|
70
|
|
|
$this->mongoClient = new MongoClient($connectString, $auth); |
|
71
|
|
|
$this->dataset = new MongoDB($this->mongoClient, $database); |
|
72
|
|
|
|
|
73
|
|
|
$this->setCollection($collection); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Closes and destruct the MongoDB connection |
|
78
|
|
|
*/ |
|
79
|
|
|
public function __destruct() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->mongoClient->close(); |
|
82
|
|
|
$this->dataset = null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getCollection() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->collectionName; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Gets the instance of MongoDB; You do not need uses this directly. |
|
96
|
|
|
* If you have to, probably something is missing in this class |
|
97
|
|
|
* @return \MongoDB |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getDbConnection() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->dataset; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Return a IteratorInterface |
|
106
|
|
|
* |
|
107
|
|
|
* @param array $filter |
|
108
|
|
|
* @param array $fields |
|
109
|
|
|
* @return ArrayDatasetIterator |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getIterator($filter = null, $fields = null) |
|
112
|
|
|
{ |
|
113
|
|
|
if (is_null($filter)) { |
|
114
|
|
|
$filter = array(); |
|
115
|
|
|
} |
|
116
|
|
|
if (is_null($fields)) { |
|
117
|
|
|
$fields = array(); |
|
118
|
|
|
} |
|
119
|
|
|
$cursor = $this->collection->find($filter, $fields); |
|
120
|
|
|
$arrIt = iterator_to_array($cursor); |
|
121
|
|
|
|
|
122
|
|
|
return new ArrayDatasetIterator($arrIt); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Insert a document in the MongoDB |
|
127
|
|
|
* @param mixed $document |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
|
|
public function insert($document) |
|
131
|
|
|
{ |
|
132
|
|
|
if (is_array($document)) { |
|
133
|
|
|
$document['created_at'] = new MongoDate(); |
|
134
|
|
|
} elseif ($document instanceof stdClass) { |
|
135
|
|
|
$document->created_at = new MongoDate(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $this->collection->insert($document); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Defines the new Collection |
|
143
|
|
|
* @param string $collection |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setCollection($collection) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->collection = $this->dataset->selectCollection($collection); |
|
148
|
|
|
$this->collectionName = $collection; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Update a document based on your criteria |
|
153
|
|
|
* |
|
154
|
|
|
* Options for MongoDB is an array of: |
|
155
|
|
|
* |
|
156
|
|
|
* sort array Determines which document the operation will modify if the |
|
157
|
|
|
* query selects multiple documents. findAndModify will modify |
|
158
|
|
|
* the first document in the sort order specified by this argument. |
|
159
|
|
|
* remove boolean Optional if update field exists. When TRUE, removes the |
|
160
|
|
|
* selected document. The default is FALSE. |
|
161
|
|
|
* update array Optional if remove field exists. Performs an update of the |
|
162
|
|
|
* selected document. |
|
163
|
|
|
* new boolean Optional. When TRUE, returns the modified document rather than the original. |
|
164
|
|
|
* The findAndModify method ignores the new option for remove operations. |
|
165
|
|
|
* The default is FALSE. |
|
166
|
|
|
* upsert boolean ptional. Used in conjunction with the update field. When TRUE, |
|
167
|
|
|
* the findAndModify command creates a new document if the query |
|
168
|
|
|
* returns no documents. The default is false. In MongoDB 2.2, the findAndModify |
|
169
|
|
|
* command returns NULL when upsert is TRUE. |
|
170
|
|
|
* |
|
171
|
|
|
* @param mixed $document |
|
172
|
|
|
* @param array $filter |
|
173
|
|
|
* @param array $options See: |
|
174
|
|
|
* @return bool |
|
175
|
|
|
*/ |
|
176
|
|
|
public function update($document, $filter = null, $options = null) |
|
177
|
|
|
{ |
|
178
|
|
|
if (is_null($filter)) { |
|
179
|
|
|
throw new InvalidArgumentException('You need to set the filter for update, or pass an empty array for all fields'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$update = array(); |
|
183
|
|
|
if (is_array($document)) { |
|
184
|
|
|
$document['updated_at'] = new MongoDate(); |
|
185
|
|
|
} |
|
186
|
|
|
if ($document instanceof stdClass) { |
|
187
|
|
|
$document->updated_at = new MongoDate(); |
|
188
|
|
|
} |
|
189
|
|
|
foreach ($document as $key => $value) { |
|
|
|
|
|
|
190
|
|
|
if ($key[0] == '$') { |
|
191
|
|
|
$update[$key] = $value; |
|
192
|
|
|
} else { |
|
193
|
|
|
$update['$set'][$key] = $value; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
if (is_null($options)) { |
|
198
|
|
|
$options = array('new' => true); |
|
199
|
|
|
} |
|
200
|
|
|
return $this->collection->findAndModify($filter, $update, array(), $options); |
|
201
|
|
|
} |
|
202
|
|
|
/* |
|
|
|
|
|
|
203
|
|
|
public function getAttribute($name) |
|
204
|
|
|
{ |
|
205
|
|
|
$this->_db->getAttribute($name); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function setAttribute($name, $value) |
|
209
|
|
|
{ |
|
210
|
|
|
$this->_db->setAttribute ( $name, $value ); |
|
211
|
|
|
} |
|
212
|
|
|
*/ |
|
213
|
|
|
} |
|
214
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.