@@ -59,6 +59,24 @@ class Aggregate implements Executable
59
59
*/
60
60
public function __construct ($ databaseName , $ collectionName , array $ pipeline , array $ options = array ())
61
61
{
62
+ if (empty ($ pipeline )) {
63
+ throw new InvalidArgumentException ('$pipeline is empty ' );
64
+ }
65
+
66
+ $ expectedIndex = 0 ;
67
+
68
+ foreach ($ pipeline as $ i => $ operation ) {
69
+ if ($ i !== $ expectedIndex ) {
70
+ throw new InvalidArgumentException (sprintf ('$pipeline is not a list (unexpected index: "%s") ' , $ i ));
71
+ }
72
+
73
+ if ( ! is_array ($ operation ) && ! is_object ($ operation )) {
74
+ throw new InvalidArgumentTypeException (sprintf ('$pipeline[%d] ' , $ i ), $ operation , 'array or object ' );
75
+ }
76
+
77
+ $ expectedIndex += 1 ;
78
+ }
79
+
62
80
$ options += array (
63
81
'allowDiskUse ' => false ,
64
82
'useCursor ' => true ,
@@ -84,20 +102,6 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
84
102
throw new InvalidArgumentException ('"batchSize" option should not be used if "useCursor" is false ' );
85
103
}
86
104
87
- $ expectedIndex = 0 ;
88
-
89
- foreach ($ pipeline as $ i => $ op ) {
90
- if ($ i !== $ expectedIndex ) {
91
- throw new InvalidArgumentException (sprintf ('$pipeline is not a list (unexpected index: "%s") ' , $ i ));
92
- }
93
-
94
- if ( ! is_array ($ op ) && ! is_object ($ op )) {
95
- throw new InvalidArgumentTypeException (sprintf ('$pipeline[%d] ' , $ i ), $ op , 'array or object ' );
96
- }
97
-
98
- $ expectedIndex += 1 ;
99
- }
100
-
101
105
$ this ->databaseName = (string ) $ databaseName ;
102
106
$ this ->collectionName = (string ) $ collectionName ;
103
107
$ this ->pipeline = $ pipeline ;
0 commit comments