4
4
5
5
namespace Doctrine \ODM \MongoDB \Aggregation ;
6
6
7
+ use Doctrine \ODM \MongoDB \Configuration ;
7
8
use Doctrine \ODM \MongoDB \DocumentManager ;
8
9
use Doctrine \ODM \MongoDB \Iterator \CachingIterator ;
9
10
use Doctrine \ODM \MongoDB \Iterator \HydratingIterator ;
15
16
use MongoDB \Collection ;
16
17
use MongoDB \Driver \CursorInterface ;
17
18
18
- use function array_key_first ;
19
19
use function array_merge ;
20
+ use function current ;
21
+ use function in_array ;
22
+ use function key ;
20
23
21
24
/** @phpstan-import-type PipelineExpression from Builder */
22
25
final class Aggregation implements IterableResult
@@ -68,7 +71,7 @@ private function prepareIterator(CursorInterface $cursor): Iterator
68
71
69
72
$ iterator = $ this ->rewindable ? new CachingIterator ($ cursor ) : new UnrewindableIterator ($ cursor );
70
73
71
- $ this ->assertSearchIndexExistsWhenAggregationResultsIsEmpty ($ iterator );
74
+ $ this ->assertSearchIndexExistsForEmptyResult ($ iterator );
72
75
73
76
return $ iterator ;
74
77
}
@@ -78,32 +81,30 @@ private function prepareIterator(CursorInterface $cursor): Iterator
78
81
* this assertion can be removed.
79
82
*
80
83
* @see https://jira.mongodb.org/browse/SERVER-110974
84
+ * @see Configuration::setAssertSearchIndexExistsForEmptyResult()
81
85
*
82
- * @param Iterator<object > $iterator
86
+ * @param CachingIterator<mixed>|UnrewindableIterator<mixed > $iterator
83
87
*/
84
- private function assertSearchIndexExistsWhenAggregationResultsIsEmpty ( Iterator $ iterator ): void
88
+ private function assertSearchIndexExistsForEmptyResult ( CachingIterator | UnrewindableIterator $ iterator ): void
85
89
{
86
- if ($ iterator ->current () !== false ) {
90
+ // The iterator is always rewinded
91
+ if ($ iterator ->current ()) {
87
92
return ; // Results not empty
88
93
}
89
94
90
- if (! $ this ->dm ->getConfiguration ()->assertSearchIndexExistsWhenAggregationResultsIsEmpty ()) {
95
+ if (! $ this ->dm ->getConfiguration ()->assertSearchIndexExistsForEmptyResult ()) {
91
96
return ; // Feature disabled
92
97
}
93
98
94
99
// Search stages must be the first stage in the pipeline
95
- $ indexName = match (array_key_first ($ this ->pipeline [0 ])) {
96
- '$search ' => $ this ->pipeline [0 ]['$search ' ]->index ?? null ,
97
- '$searchMeta ' => $ this ->pipeline [0 ]['$searchMeta ' ]->index ?? null ,
98
- '$vectorSearch ' => $ this ->pipeline [0 ]['$vectorSearch ' ]->index ?? null ,
99
- default => null ,
100
- };
101
-
102
- if ($ indexName === null ) {
103
- return ; // Not a search aggregation or index not specified
100
+ $ stage = $ this ->pipeline [0 ] ?? null ;
101
+ if (! $ stage || ! in_array (key ($ stage ), ['$search ' , '$searchMeta ' , '$vectorSearch ' ], true )) {
102
+ return ; // Not a search aggregation
104
103
}
105
104
106
- $ index = $ this ->collection ->listSearchIndexes (['filter ' => ['name ' => $ indexName ]])->current ();
105
+ // @phpcs:ignore SlevomatCodingStandard.PHP.UselessParentheses
106
+ $ indexName = ((object ) current ($ stage ))->index ?? 'default ' ;
107
+ $ index = $ this ->collection ->listSearchIndexes (['filter ' => ['name ' => $ indexName ]])->current ();
107
108
if ($ index ) {
108
109
return ; // Index exists
109
110
}
0 commit comments