Skip to content

Commit a6ac94d

Browse files
committed
Merge pull request #656
2 parents e98829b + 2b1c1ec commit a6ac94d

File tree

2 files changed

+25
-190
lines changed

2 files changed

+25
-190
lines changed

src/Operation/CountDocuments.php

Lines changed: 16 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717

1818
namespace MongoDB\Operation;
1919

20-
use MongoDB\Driver\Command;
21-
use MongoDB\Driver\ReadConcern;
22-
use MongoDB\Driver\ReadPreference;
2320
use MongoDB\Driver\Server;
24-
use MongoDB\Driver\Session;
2521
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2622
use MongoDB\Exception\InvalidArgumentException;
2723
use MongoDB\Exception\UnexpectedValueException;
@@ -36,13 +32,12 @@
3632
*/
3733
class CountDocuments implements Executable
3834
{
39-
private static $wireVersionForCollation = 5;
40-
private static $wireVersionForReadConcern = 4;
41-
4235
private $databaseName;
4336
private $collectionName;
4437
private $filter;
45-
private $options;
38+
private $aggregateOptions;
39+
private $countOptions;
40+
private $aggregate;
4641

4742
/**
4843
* Constructs an aggregate command for counting documents
@@ -89,46 +84,22 @@ public function __construct($databaseName, $collectionName, $filter, array $opti
8984
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
9085
}
9186

92-
if (isset($options['collation']) && ! is_array($options['collation']) && ! is_object($options['collation'])) {
93-
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
94-
}
95-
96-
if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) {
97-
throw InvalidArgumentException::invalidType('"hint" option', $options['hint'], 'string or array or object');
98-
}
99-
10087
if (isset($options['limit']) && ! is_integer($options['limit'])) {
10188
throw InvalidArgumentException::invalidType('"limit" option', $options['limit'], 'integer');
10289
}
10390

104-
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
105-
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
106-
}
107-
108-
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
109-
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], ReadConcern::class);
110-
}
111-
112-
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
113-
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], ReadPreference::class);
114-
}
115-
116-
if (isset($options['session']) && ! $options['session'] instanceof Session) {
117-
throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
118-
}
119-
12091
if (isset($options['skip']) && ! is_integer($options['skip'])) {
12192
throw InvalidArgumentException::invalidType('"skip" option', $options['skip'], 'integer');
12293
}
12394

124-
if (isset($options['readConcern']) && $options['readConcern']->isDefault()) {
125-
unset($options['readConcern']);
126-
}
127-
12895
$this->databaseName = (string) $databaseName;
12996
$this->collectionName = (string) $collectionName;
13097
$this->filter = $filter;
131-
$this->options = $options;
98+
99+
$this->aggregateOptions = array_intersect_key($options, ['collation' => 1, 'hint' => 1, 'maxTimeMS' => 1, 'readConcern' => 1, 'readPreference' => 1, 'session' => 1]);
100+
$this->countOptions = array_intersect_key($options, ['limit' => 1, 'skip' => 1]);
101+
102+
$this->aggregate = $this->createAggregate();
132103
}
133104

134105
/**
@@ -143,20 +114,7 @@ public function __construct($databaseName, $collectionName, $filter, array $opti
143114
*/
144115
public function execute(Server $server)
145116
{
146-
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
147-
throw UnsupportedException::collationNotSupported();
148-
}
149-
150-
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
151-
throw UnsupportedException::readConcernNotSupported();
152-
}
153-
154-
$inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
155-
if ($inTransaction && isset($this->options['readConcern'])) {
156-
throw UnsupportedException::readConcernNotSupportedInTransaction();
157-
}
158-
159-
$cursor = $server->executeReadCommand($this->databaseName, new Command($this->createCommandDocument()), $this->createOptions());
117+
$cursor = $this->aggregate->execute($server);
160118
$allResults = $cursor->toArray();
161119

162120
/* If there are no documents to count, the aggregation pipeline has no items to group, and
@@ -174,69 +132,24 @@ public function execute(Server $server)
174132
}
175133

176134
/**
177-
* Create the count command document.
178-
*
179-
* @return array
135+
* @return Aggregate
180136
*/
181-
private function createCommandDocument()
137+
private function createAggregate()
182138
{
183139
$pipeline = [
184140
['$match' => (object) $this->filter]
185141
];
186142

187-
if (isset($this->options['skip'])) {
188-
$pipeline[] = ['$skip' => $this->options['skip']];
143+
if (isset($this->countOptions['skip'])) {
144+
$pipeline[] = ['$skip' => $this->countOptions['skip']];
189145
}
190146

191-
if (isset($this->options['limit'])) {
192-
$pipeline[] = ['$limit' => $this->options['limit']];
147+
if (isset($this->countOptions['limit'])) {
148+
$pipeline[] = ['$limit' => $this->countOptions['limit']];
193149
}
194150

195151
$pipeline[] = ['$group' => ['_id' => 1, 'n' => ['$sum' => 1]]];
196152

197-
$cmd = [
198-
'aggregate' => $this->collectionName,
199-
'pipeline' => $pipeline,
200-
'cursor' => (object) [],
201-
];
202-
203-
if (isset($this->options['collation'])) {
204-
$cmd['collation'] = (object) $this->options['collation'];
205-
}
206-
207-
if (isset($this->options['hint'])) {
208-
$cmd['hint'] = is_array($this->options['hint']) ? (object) $this->options['hint'] : $this->options['hint'];
209-
}
210-
211-
if (isset($this->options['maxTimeMS'])) {
212-
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
213-
}
214-
215-
return $cmd;
216-
}
217-
218-
/**
219-
* Create options for executing the command.
220-
*
221-
* @see http://php.net/manual/en/mongodb-driver-server.executereadcommand.php
222-
* @return array
223-
*/
224-
private function createOptions()
225-
{
226-
$options = [];
227-
228-
if (isset($this->options['readConcern'])) {
229-
$options['readConcern'] = $this->options['readConcern'];
230-
}
231-
232-
if (isset($this->options['readPreference'])) {
233-
$options['readPreference'] = $this->options['readPreference'];
234-
}
235-
236-
if (isset($this->options['session'])) {
237-
$options['session'] = $this->options['session'];
238-
}
239-
240-
return $options;
153+
return new Aggregate($this->databaseName, $this->collectionName, $pipeline, $this->aggregateOptions);
241154
}
242155
}

src/Operation/EstimatedDocumentCount.php

Lines changed: 9 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717

1818
namespace MongoDB\Operation;
1919

20-
use MongoDB\Driver\Command;
21-
use MongoDB\Driver\ReadConcern;
22-
use MongoDB\Driver\ReadPreference;
2320
use MongoDB\Driver\Server;
24-
use MongoDB\Driver\Session;
2521
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2622
use MongoDB\Exception\InvalidArgumentException;
2723
use MongoDB\Exception\UnexpectedValueException;
@@ -36,12 +32,10 @@
3632
*/
3733
class EstimatedDocumentCount implements Executable, Explainable
3834
{
39-
private static $wireVersionForCollation = 5;
40-
private static $wireVersionForReadConcern = 4;
41-
4235
private $databaseName;
4336
private $collectionName;
4437
private $options;
38+
private $count;
4539

4640
/**
4741
* Constructs a count command.
@@ -69,29 +63,11 @@ class EstimatedDocumentCount implements Executable, Explainable
6963
*/
7064
public function __construct($databaseName, $collectionName, array $options = [])
7165
{
72-
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
73-
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
74-
}
75-
76-
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
77-
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], ReadConcern::class);
78-
}
79-
80-
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
81-
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], ReadPreference::class);
82-
}
83-
84-
if (isset($options['session']) && ! $options['session'] instanceof Session) {
85-
throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
86-
}
87-
88-
if (isset($options['readConcern']) && $options['readConcern']->isDefault()) {
89-
unset($options['readConcern']);
90-
}
91-
9266
$this->databaseName = (string) $databaseName;
9367
$this->collectionName = (string) $collectionName;
94-
$this->options = $options;
68+
$this->options = array_intersect_key($options, ['maxTimeMS' => 1, 'readConcern' => 1, 'readPreference' => 1, 'session' => 1]);
69+
70+
$this->count = $this->createCount();
9571
}
9672

9773
/**
@@ -106,73 +82,19 @@ public function __construct($databaseName, $collectionName, array $options = [])
10682
*/
10783
public function execute(Server $server)
10884
{
109-
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
110-
throw UnsupportedException::collationNotSupported();
111-
}
112-
113-
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
114-
throw UnsupportedException::readConcernNotSupported();
115-
}
116-
117-
$inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
118-
if ($inTransaction && isset($this->options['readConcern'])) {
119-
throw UnsupportedException::readConcernNotSupportedInTransaction();
120-
}
121-
122-
$cursor = $server->executeReadCommand($this->databaseName, new Command($this->createCommandDocument()), $this->createOptions());
123-
$result = current($cursor->toArray());
124-
125-
// Older server versions may return a float
126-
if ( ! isset($result->n) || ! (is_integer($result->n) || is_float($result->n))) {
127-
throw new UnexpectedValueException('count command did not return a numeric "n" value');
128-
}
129-
130-
return (integer) $result->n;
85+
return $this->count->execute($server);
13186
}
13287

13388
public function getCommandDocument(Server $server)
13489
{
135-
return $this->createCommandDocument();
90+
return $this->count->getCommandDocument($server);
13691
}
13792

13893
/**
139-
* Create the count command document.
140-
*
141-
* @return array
94+
* @return Count
14295
*/
143-
private function createCommandDocument()
96+
private function createCount()
14497
{
145-
$cmd = ['count' => $this->collectionName];
146-
147-
if (isset($this->options['maxTimeMS'])) {
148-
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
149-
}
150-
151-
return $cmd;
152-
}
153-
154-
/**
155-
* Create options for executing the command.
156-
*
157-
* @see http://php.net/manual/en/mongodb-driver-server.executereadcommand.php
158-
* @return array
159-
*/
160-
private function createOptions()
161-
{
162-
$options = [];
163-
164-
if (isset($this->options['readConcern'])) {
165-
$options['readConcern'] = $this->options['readConcern'];
166-
}
167-
168-
if (isset($this->options['readPreference'])) {
169-
$options['readPreference'] = $this->options['readPreference'];
170-
}
171-
172-
if (isset($this->options['session'])) {
173-
$options['session'] = $this->options['session'];
174-
}
175-
176-
return $options;
98+
return new Count($this->databaseName, $this->collectionName, [], $this->options);
17799
}
178100
}

0 commit comments

Comments
 (0)