Skip to content

Commit 53c674f

Browse files
authored
Merge pull request #791 from utopia-php/get-by-type-reference
2 parents b5c16ca + 47d35fc commit 53c674f

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

src/Database/Query.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,21 +824,39 @@ public static function and(array $queries): self
824824
*
825825
* @param array<Query> $queries
826826
* @param array<string> $types
827+
* @param bool $clone
827828
* @return array<Query>
828829
*/
829-
public static function getByType(array $queries, array $types): array
830+
public static function getByType(array $queries, array $types, bool $clone = true): array
830831
{
831832
$filtered = [];
832833

833834
foreach ($queries as $query) {
834835
if (\in_array($query->getMethod(), $types, true)) {
835-
$filtered[] = clone $query;
836+
$filtered[] = $clone ? clone $query : $query;
836837
}
837838
}
838839

839840
return $filtered;
840841
}
841842

843+
/**
844+
* @param array<Query> $queries
845+
* @param bool $clone
846+
* @return array<Query>
847+
*/
848+
public static function getCursorQueries(array $queries, bool $clone = true): array
849+
{
850+
return self::getByType(
851+
$queries,
852+
[
853+
Query::TYPE_CURSOR_AFTER,
854+
Query::TYPE_CURSOR_BEFORE,
855+
],
856+
$clone
857+
);
858+
}
859+
842860
/**
843861
* Iterates through queries are groups them by type
844862
*

tests/unit/Validator/QueryTest.php

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,68 @@ public function testQueryGetByType(): void
238238
{
239239
$queries = [
240240
Query::equal('key', ['value']),
241-
Query::select(['attr1', 'attr2']),
242241
Query::cursorBefore(new Document([])),
243242
Query::cursorAfter(new Document([])),
244243
];
245244

246-
$queries = Query::getByType($queries, [Query::TYPE_CURSOR_AFTER, Query::TYPE_CURSOR_BEFORE]);
247-
$this->assertCount(2, $queries);
248-
foreach ($queries as $query) {
245+
$queries1 = Query::getByType($queries, [Query::TYPE_CURSOR_AFTER, Query::TYPE_CURSOR_BEFORE]);
246+
247+
$this->assertCount(2, $queries1);
248+
foreach ($queries1 as $query) {
249249
$this->assertEquals(true, in_array($query->getMethod(), [Query::TYPE_CURSOR_AFTER, Query::TYPE_CURSOR_BEFORE]));
250250
}
251+
252+
$cursor = reset($queries1);
253+
254+
$this->assertInstanceOf(Query::class, $cursor);
255+
256+
$cursor->setValue(new Document(['$id' => 'hello1']));
257+
258+
$query1 = $queries[1];
259+
260+
$this->assertEquals(Query::TYPE_CURSOR_BEFORE, $query1->getMethod());
261+
$this->assertInstanceOf(Document::class, $query1->getValue());
262+
$this->assertTrue($query1->getValue()->isEmpty()); // Cursor Document is not updated
263+
264+
/**
265+
* Using reference $queries2 => $queries
266+
*/
267+
$queries2 = Query::getByType($queries, [Query::TYPE_CURSOR_AFTER, Query::TYPE_CURSOR_BEFORE], false);
268+
269+
$cursor = reset($queries2);
270+
$this->assertInstanceOf(Query::class, $cursor);
271+
272+
$cursor->setValue(new Document(['$id' => 'hello1']));
273+
274+
$query2 = $queries[1];
275+
276+
$this->assertCount(2, $queries2);
277+
$this->assertEquals(Query::TYPE_CURSOR_BEFORE, $query2->getMethod());
278+
$this->assertInstanceOf(Document::class, $query2->getValue());
279+
$this->assertEquals('hello1', $query2->getValue()->getId()); // Cursor Document is updated
280+
281+
/**
282+
* Using getCursorQueries
283+
*/
284+
$queries = [
285+
Query::equal('key', ['value']),
286+
Query::cursorBefore(new Document([])),
287+
Query::cursorAfter(new Document([])),
288+
];
289+
290+
$queries3 = Query::getCursorQueries($queries, false);
291+
292+
$cursor = reset($queries3); // Same as writing $cursor = $queries3[0];
293+
$this->assertInstanceOf(Query::class, $cursor);
294+
295+
$cursor->setValue(new Document(['$id' => 'hello3']));
296+
297+
$query3 = $queries[1];
298+
299+
$this->assertCount(2, $queries3);
300+
$this->assertEquals(Query::TYPE_CURSOR_BEFORE, $query3->getMethod());
301+
$this->assertInstanceOf(Document::class, $query3->getValue());
302+
$this->assertEquals('hello3', $query3->getValue()->getId()); // Cursor Document is updated
251303
}
252304

253305
/**

0 commit comments

Comments
 (0)