17
17
18
18
namespace MongoDB \Operation ;
19
19
20
- use MongoDB \Driver \Command ;
21
- use MongoDB \Driver \ReadConcern ;
22
- use MongoDB \Driver \ReadPreference ;
23
20
use MongoDB \Driver \Server ;
24
- use MongoDB \Driver \Session ;
25
21
use MongoDB \Driver \Exception \RuntimeException as DriverRuntimeException ;
26
22
use MongoDB \Exception \InvalidArgumentException ;
27
23
use MongoDB \Exception \UnexpectedValueException ;
36
32
*/
37
33
class EstimatedDocumentCount implements Executable, Explainable
38
34
{
39
- private static $ wireVersionForCollation = 5 ;
40
- private static $ wireVersionForReadConcern = 4 ;
41
-
42
35
private $ databaseName ;
43
36
private $ collectionName ;
44
37
private $ options ;
38
+ private $ count ;
45
39
46
40
/**
47
41
* Constructs a count command.
@@ -69,29 +63,11 @@ class EstimatedDocumentCount implements Executable, Explainable
69
63
*/
70
64
public function __construct ($ databaseName , $ collectionName , array $ options = [])
71
65
{
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
-
92
66
$ this ->databaseName = (string ) $ databaseName ;
93
67
$ 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 ();
95
71
}
96
72
97
73
/**
@@ -106,73 +82,19 @@ public function __construct($databaseName, $collectionName, array $options = [])
106
82
*/
107
83
public function execute (Server $ server )
108
84
{
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 );
131
86
}
132
87
133
88
public function getCommandDocument (Server $ server )
134
89
{
135
- return $ this ->createCommandDocument ( );
90
+ return $ this ->count -> getCommandDocument ( $ server );
136
91
}
137
92
138
93
/**
139
- * Create the count command document.
140
- *
141
- * @return array
94
+ * @return Count
142
95
*/
143
- private function createCommandDocument ()
96
+ private function createCount ()
144
97
{
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 );
177
99
}
178
100
}
0 commit comments