|
6 | 6 | use MongoDB\Client;
|
7 | 7 | use MongoDB\Collection;
|
8 | 8 | use MongoDB\Database;
|
| 9 | +use MongoDB\Driver\Manager; |
9 | 10 | use MongoDB\Driver\ReadConcern;
|
10 | 11 | use MongoDB\Driver\ReadPreference;
|
11 |
| -use MongoDB\Driver\Session; |
| 12 | +use MongoDB\Driver\Server; |
12 | 13 | use MongoDB\Driver\WriteConcern;
|
13 | 14 | use stdClass;
|
14 | 15 | use function array_diff_key;
|
@@ -101,41 +102,76 @@ public function createEntities(array $entities)
|
101 | 102 | }
|
102 | 103 | }
|
103 | 104 |
|
104 |
| - public function getEntityMap() : EntityMap |
| 105 | + public static function createReadConcern(stdClass $o) : ReadConcern |
105 | 106 | {
|
106 |
| - return $this->entityMap; |
107 |
| - } |
| 107 | + self::assertHasOnlyKeys($o, ['level']); |
108 | 108 |
|
109 |
| - public function getInternalClient() : Client |
110 |
| - { |
111 |
| - return $this->internalClient; |
| 109 | + $level = $o->level ?? null; |
| 110 | + assertInternalType('string', $level); |
| 111 | + |
| 112 | + return new ReadConcern($level); |
112 | 113 | }
|
113 | 114 |
|
114 |
| - public function prepareOperationArguments(array $args) : array |
| 115 | + public static function createReadPreference(stdClass $o) : ReadPreference |
115 | 116 | {
|
116 |
| - if (array_key_exists('readConcern', $args)) { |
117 |
| - assertInternalType('object', $args['readConcern']); |
118 |
| - $args['readConcern'] = self::prepareReadConcern($args['readConcern']); |
| 117 | + self::assertHasOnlyKeys($o, ['mode', 'tagSets', 'maxStalenessSeconds', 'hedge']); |
| 118 | + |
| 119 | + $mode = $o->mode ?? null; |
| 120 | + $tagSets = $o->tagSets ?? null; |
| 121 | + $maxStalenessSeconds = $o->maxStalenessSeconds ?? null; |
| 122 | + $hedge = $o->hedge ?? null; |
| 123 | + |
| 124 | + assertInternalType('string', $mode); |
| 125 | + |
| 126 | + if (isset($tagSets)) { |
| 127 | + assertInternalType('array', $tagSets); |
| 128 | + assertContains('object', $tagSets); |
119 | 129 | }
|
120 | 130 |
|
121 |
| - if (array_key_exists('readPreference', $args)) { |
122 |
| - assertInternalType('object', $args['readPreference']); |
123 |
| - $args['readPreference'] = self::prepareReadPreference($args['readPreference']); |
| 131 | + $options = []; |
| 132 | + |
| 133 | + if (isset($maxStalenessSeconds)) { |
| 134 | + assertInternalType('int', $maxStalenessSeconds); |
| 135 | + $options['maxStalenessSeconds'] = $maxStalenessSeconds; |
124 | 136 | }
|
125 | 137 |
|
126 |
| - if (array_key_exists('session', $args)) { |
127 |
| - assertInternalType('string', $args['session']); |
128 |
| - $session = $this->entityMap[$args['session']]; |
129 |
| - assertInstanceOf(Session::class, $session); |
130 |
| - $args['session'] = $session; |
| 138 | + if (isset($hedge)) { |
| 139 | + assertInternalType('object', $hedge); |
| 140 | + $options['hedge'] = $hedge; |
131 | 141 | }
|
132 | 142 |
|
133 |
| - if (array_key_exists('writeConcern', $args)) { |
134 |
| - assertInternalType('object', $args['writeConcern']); |
135 |
| - $args['writeConcern'] = self::prepareWriteConcern($args['writeConcern']); |
| 143 | + return new ReadPreference($mode, $tagSets, $options); |
| 144 | + } |
| 145 | + |
| 146 | + public static function createWriteConcern(stdClass $o) : WriteConcern |
| 147 | + { |
| 148 | + self::assertHasOnlyKeys($o, ['w', 'wtimeoutMS', 'journal']); |
| 149 | + |
| 150 | + $w = $o->w ?? -2; /* MONGOC_WRITE_CONCERN_W_DEFAULT */ |
| 151 | + $wtimeoutMS = $o->wtimeoutMS ?? 0; |
| 152 | + $journal = $o->journal ?? null; |
| 153 | + |
| 154 | + assertThat($w, logicalOr(isType('int'), isType('string'))); |
| 155 | + assertInternalType('int', $wtimeoutMS); |
| 156 | + |
| 157 | + $args = [$w, $wtimeoutMS]; |
| 158 | + |
| 159 | + if (isset($journal)) { |
| 160 | + assertInternalType('bool', $journal); |
| 161 | + $args[] = $journal; |
136 | 162 | }
|
137 | 163 |
|
138 |
| - return $args; |
| 164 | + return new WriteConcern(...$args); |
| 165 | + } |
| 166 | + |
| 167 | + public function getEntityMap() : EntityMap |
| 168 | + { |
| 169 | + return $this->entityMap; |
| 170 | + } |
| 171 | + |
| 172 | + public function getInternalClient() : Client |
| 173 | + { |
| 174 | + return $this->internalClient; |
139 | 175 | }
|
140 | 176 |
|
141 | 177 | public function startEventObservers()
|
@@ -267,68 +303,6 @@ private static function prepareCollectionOrDatabaseOptions(array $options) : arr
|
267 | 303 | return $options;
|
268 | 304 | }
|
269 | 305 |
|
270 |
| - private static function createReadConcern(stdClass $o) : ReadConcern |
271 |
| - { |
272 |
| - self::assertHasOnlyKeys($o, ['level']); |
273 |
| - |
274 |
| - $level = $o->level ?? null; |
275 |
| - assertInternalType('string', $level); |
276 |
| - |
277 |
| - return new ReadConcern($level); |
278 |
| - } |
279 |
| - |
280 |
| - private static function createReadPreference(stdClass $o) : ReadPreference |
281 |
| - { |
282 |
| - self::assertHasOnlyKeys($o, ['mode', 'tagSets', 'maxStalenessSeconds', 'hedge']); |
283 |
| - |
284 |
| - $mode = $o->mode ?? null; |
285 |
| - $tagSets = $o->tagSets ?? null; |
286 |
| - $maxStalenessSeconds = $o->maxStalenessSeconds ?? null; |
287 |
| - $hedge = $o->hedge ?? null; |
288 |
| - |
289 |
| - assertInternalType('string', $mode); |
290 |
| - |
291 |
| - if (isset($tagSets)) { |
292 |
| - assertInternalType('array', $tagSets); |
293 |
| - assertContains('object', $tagSets); |
294 |
| - } |
295 |
| - |
296 |
| - $options = []; |
297 |
| - |
298 |
| - if (isset($maxStalenessSeconds)) { |
299 |
| - assertInternalType('int', $maxStalenessSeconds); |
300 |
| - $options['maxStalenessSeconds'] = $maxStalenessSeconds; |
301 |
| - } |
302 |
| - |
303 |
| - if (isset($hedge)) { |
304 |
| - assertInternalType('object', $hedge); |
305 |
| - $options['hedge'] = $hedge; |
306 |
| - } |
307 |
| - |
308 |
| - return new ReadPreference($mode, $tagSets, $options); |
309 |
| - } |
310 |
| - |
311 |
| - private static function createWriteConcern(stdClass $o) : WriteConcern |
312 |
| - { |
313 |
| - self::assertHasOnlyKeys($o, ['w', 'wtimeoutMS', 'journal']); |
314 |
| - |
315 |
| - $w = $o->w ?? -2; /* MONGOC_WRITE_CONCERN_W_DEFAULT */ |
316 |
| - $wtimeoutMS = $o->wtimeoutMS ?? 0; |
317 |
| - $journal = $o->journal ?? null; |
318 |
| - |
319 |
| - assertThat($w, logicalOr(isType('int'), isType('string'))); |
320 |
| - assertInternalType('int', $wtimeoutMS); |
321 |
| - |
322 |
| - $args = [$w, $wtimeoutMS]; |
323 |
| - |
324 |
| - if (isset($journal)) { |
325 |
| - assertInternalType('bool', $journal); |
326 |
| - $args[] = $journal; |
327 |
| - } |
328 |
| - |
329 |
| - return new WriteConcern(...$args); |
330 |
| - } |
331 |
| - |
332 | 306 | /**
|
333 | 307 | * Removes mongos hosts beyond the first if the URI refers to a sharded
|
334 | 308 | * cluster. Otherwise, the URI is returned as-is.
|
|
0 commit comments