2222namespace MacFJA \RediSearch \Redis \Command ;
2323
2424use function assert ;
25+ use function count ;
2526use function is_array ;
2627use MacFJA \RediSearch \Exception \UnexpectedServerResponseException ;
2728use MacFJA \RediSearch \Redis \Command \AggregateCommand \ApplyOption ;
2829use MacFJA \RediSearch \Redis \Command \AggregateCommand \GroupByOption ;
2930use MacFJA \RediSearch \Redis \Command \AggregateCommand \LimitOption ;
3031use MacFJA \RediSearch \Redis \Command \AggregateCommand \SortByOption ;
3132use MacFJA \RediSearch \Redis \Command \AggregateCommand \WithCursor ;
33+ use MacFJA \RediSearch \Redis \Command \Option \CommandOption ;
3234use MacFJA \RediSearch \Redis \Command \Option \FlagOption ;
3335use MacFJA \RediSearch \Redis \Command \Option \NamedOption ;
3436use MacFJA \RediSearch \Redis \Command \Option \NamelessOption ;
@@ -45,6 +47,9 @@ class Aggregate extends AbstractCommand implements PaginatedCommand
4547{
4648 use ArrayResponseTrait;
4749
50+ /** @var CommandOption */
51+ private $ lastAdded ;
52+
4853 public function __construct (string $ rediSearchVersion = self ::MIN_IMPLEMENTED_VERSION )
4954 {
5055 parent ::__construct ([
@@ -64,6 +69,7 @@ public function __construct(string $rediSearchVersion = self::MIN_IMPLEMENTED_VE
6469 public function setIndex (string $ index ): self
6570 {
6671 $ this ->options ['index ' ]->setValue ($ index );
72+ $ this ->lastAdded = $ this ->options ['index ' ];
6773
6874 return $ this ;
6975 }
@@ -76,13 +82,15 @@ public function getIndex(): string
7682 public function setQuery (string $ query ): self
7783 {
7884 $ this ->options ['query ' ]->setValue ($ query );
85+ $ this ->lastAdded = $ this ->options ['query ' ];
7986
8087 return $ this ;
8188 }
8289
8390 public function setVerbatim (bool $ active = true ): self
8491 {
8592 $ this ->options ['verbatim ' ]->setActive ($ active );
93+ $ this ->lastAdded = $ this ->options ['verbatim ' ];
8694
8795 return $ this ;
8896 }
@@ -125,6 +133,8 @@ public function addApply(string $expression, string $alias): self
125133 ->setDataOfOption ('expression ' , $ expression )
126134 ->setDataOfOption ('alias ' , $ alias )
127135 ;
136+ $ apply ->setParent ($ this ->lastAdded );
137+ $ this ->lastAdded = $ apply ;
128138 $ this ->options ['apply ' ][] = $ apply ;
129139
130140 return $ this ;
@@ -133,13 +143,15 @@ public function addApply(string $expression, string $alias): self
133143 public function addGroupBy (GroupByOption $ group ): self
134144 {
135145 $ this ->options ['groupby ' ][] = $ group ;
146+ $ this ->lastAdded = $ group ;
136147
137148 return $ this ;
138149 }
139150
140151 public function setLoad (string ...$ field ): self
141152 {
142153 $ this ->options ['load ' ]->setArguments ($ field );
154+ $ this ->lastAdded = $ this ->options ['load ' ];
143155
144156 return $ this ;
145157 }
@@ -215,6 +227,27 @@ public function parseResponse($data)
215227 return new PaginatedResponse ($ this , $ totalCount , $ items );
216228 }
217229
230+ protected function sortArguments (array $ arguments ): array
231+ {
232+ /** @var array<ApplyOption> $applies */
233+ $ applies = array_filter ($ arguments , static function (CommandOption $ option ) {
234+ return $ option instanceof ApplyOption;
235+ });
236+ $ withoutApplies = array_values (array_filter ($ arguments , static function (CommandOption $ option ) {
237+ return !($ option instanceof ApplyOption);
238+ }));
239+ for ($ index = count ($ withoutApplies ); $ index > 0 ; --$ index ) {
240+ $ item = $ withoutApplies [$ index - 1 ];
241+ foreach ($ applies as $ apply ) {
242+ if ($ apply ->getParent () === $ item || (null === $ apply ->getParent () && 1 === $ index )) {
243+ array_splice ($ withoutApplies , $ index , 0 , [$ apply ]);
244+ }
245+ }
246+ }
247+
248+ return $ withoutApplies ;
249+ }
250+
218251 protected function getRequiredOptions (): array
219252 {
220253 return ['index ' , 'query ' ];
0 commit comments