@@ -115,47 +115,49 @@ private function encodeAsGroup(OperatorInterface $value): stdClass
115115 return $ this ->wrap ($ value , $ result );
116116 }
117117
118- private function encodeAsSearch (OperatorInterface $ value ): stdClass
118+ private function encodeAsObject (OperatorInterface $ value ): stdClass
119119 {
120- assert ($ value instanceof SearchStage || $ value instanceof SearchMetaStage);
121-
122120 $ result = new stdClass ();
123121 foreach (get_object_vars ($ value ) as $ key => $ val ) {
124122 // Skip optional arguments. If they have a default value, it is resolved by the server.
125123 if ($ val === Optional::Undefined) {
126124 continue ;
127125 }
128126
129- // Merge the operator properties into the result object
130- if ($ key === 'operator ' ) {
131- foreach (get_object_vars ($ this ->recursiveEncode ($ val )) as $ subkey => $ subval ) {
132- $ result ->{$ subkey } = $ subval ;
133- }
134-
135- continue ;
136- }
137-
138127 $ result ->{$ key } = $ this ->recursiveEncode ($ val );
139128 }
140129
141- return $ this ->wrap ($ value , $ result );
130+ return $ value ::ENCODE === Encode::FlatObject
131+ ? $ result
132+ : $ this ->wrap ($ value , $ result );
142133 }
143134
144- private function encodeAsObject (OperatorInterface $ value ): stdClass
135+ private function encodeAsSearch (OperatorInterface $ value ): stdClass
145136 {
137+ assert ($ value instanceof SearchStage || $ value instanceof SearchMetaStage);
138+
146139 $ result = new stdClass ();
147140 foreach (get_object_vars ($ value ) as $ key => $ val ) {
148141 // Skip optional arguments. If they have a default value, it is resolved by the server.
149142 if ($ val === Optional::Undefined) {
150143 continue ;
151144 }
152145
146+ // Merge the operator properties into the result object
147+ if ($ key === 'operator ' ) {
148+ $ val = $ this ->recursiveEncode ($ val );
149+ assert ($ val instanceof stdClass);
150+ foreach (get_object_vars ($ val ) as $ subkey => $ subval ) {
151+ $ result ->{$ subkey } = $ subval ;
152+ }
153+
154+ continue ;
155+ }
156+
153157 $ result ->{$ key } = $ this ->recursiveEncode ($ val );
154158 }
155159
156- return $ value ::ENCODE === Encode::FlatObject
157- ? $ result
158- : $ this ->wrap ($ value , $ result );
160+ return $ this ->wrap ($ value , $ result );
159161 }
160162
161163 /**
0 commit comments