1414use Kreait \Firebase \Database \Query \Filter \StartAfter ;
1515use Kreait \Firebase \Database \Query \Filter \StartAt ;
1616use Kreait \Firebase \Database \Query \Sorter ;
17+ use Kreait \Firebase \Database \Query \Sorter \Noop ;
1718use Kreait \Firebase \Database \Query \Sorter \OrderByChild ;
1819use Kreait \Firebase \Database \Query \Sorter \OrderByKey ;
1920use Kreait \Firebase \Database \Query \Sorter \OrderByValue ;
3435 * {@see getSnapshot()} or {@see getValue()} method. You will only receive
3536 * Snapshots for the subset of the data that matches your query.
3637 */
37- class Query implements Stringable
38+ final readonly class Query implements Stringable
3839{
39- /**
40- * @var Filter[]
41- */
42- private array $ filters = [];
43-
44- private ?Sorter $ sorter = null ;
45-
4640 /**
4741 * @internal
42+ *
43+ * @param list<Filter> $filters
4844 */
49- public function __construct (private readonly Reference $ reference , private readonly ApiClient $ apiClient )
45+ public function __construct (
46+ private Reference $ reference ,
47+ private ApiClient $ apiClient ,
48+ private array $ filters ,
49+ private Sorter $ sorter ,
50+ )
5051 {
5152 }
5253
@@ -91,9 +92,7 @@ public function getSnapshot(): Snapshot
9192 );
9293 }
9394
94- if ($ this ->sorter !== null ) {
95- $ value = $ this ->sorter ->modifyValue ($ value );
96- }
95+ $ value = $ this ->sorter ->modifyValue ($ value );
9796
9897 foreach ($ this ->filters as $ filter ) {
9998 $ value = $ filter ->modifyValue ($ value );
@@ -253,10 +252,7 @@ public function shallow(): self
253252 public function getUri (): UriInterface
254253 {
255254 $ uri = $ this ->reference ->getUri ();
256-
257- if ($ this ->sorter !== null ) {
258- $ uri = $ this ->sorter ->modifyUri ($ uri );
259- }
255+ $ uri = $ this ->sorter ->modifyUri ($ uri );
260256
261257 foreach ($ this ->filters as $ filter ) {
262258 $ uri = $ filter ->modifyUri ($ uri );
@@ -267,21 +263,25 @@ public function getUri(): UriInterface
267263
268264 private function withAddedFilter (Filter $ filter ): self
269265 {
270- $ query = clone $ this ;
271- $ query ->filters [] = $ filter ;
272-
273- return $ query ;
266+ return new self (
267+ $ this ->reference ,
268+ $ this ->apiClient ,
269+ [...$ this ->filters , $ filter ],
270+ $ this ->sorter
271+ );
274272 }
275273
276274 private function withSorter (Sorter $ sorter ): self
277275 {
278- if ($ this ->sorter !== null ) {
276+ if (!( $ this ->sorter instanceof Noop) ) {
279277 throw new UnsupportedQuery ($ this , 'This query is already ordered. ' );
280278 }
281279
282- $ query = clone $ this ;
283- $ query ->sorter = $ sorter ;
284-
285- return $ query ;
280+ return new self (
281+ $ this ->reference ,
282+ $ this ->apiClient ,
283+ $ this ->filters ,
284+ $ sorter
285+ );
286286 }
287287}
0 commit comments