Skip to content

Commit

Permalink
Merge branch '5' into 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Jan 8, 2025
2 parents bf05e17 + 597d110 commit 6d59a7d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/ORM/Search/BasicSearchContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BasicSearchContext extends SearchContext
* the parameter name should have the dots replaced with double underscores,
* for example "Comments__Name" instead of the filter name "Comments.Name".
* @param array|bool|string $sort Field to sort on.
* @param array|null|string $limit
* @param int|array|null $limit
* @param SS_List $existingQuery
*/
public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null): SS_List
Expand All @@ -43,12 +43,16 @@ public function getQuery($searchParams, $sort = false, $limit = false, $existing
throw new InvalidArgumentException('getQuery requires a pre-existing SS_List list to be passed as $existingQuery.');
}

if ((count(func_get_args()) >= 3) && (!in_array(gettype($limit), ['array', 'NULL', 'string']))) {
if ((count(func_get_args()) >= 3) && (!in_array(gettype($limit), ['array', 'NULL', 'integer']))) {
Deprecation::notice(
'5.1.0',
'$limit should be type of array|string|null'
'$limit should be type of int|array|null'
);
$limit = null;
if (is_string($limit) && is_numeric($limit)) {
$limit = (int) $limit;
} else {
$limit = null;
}
}

$searchParams = $this->applySearchFilters($this->normaliseSearchParams($searchParams));
Expand All @@ -65,7 +69,14 @@ public function getQuery($searchParams, $sort = false, $limit = false, $existing
}

// Limit must be last so that ArrayList results don't have an applied limit before they can be filtered/sorted.
$result = $result->limit($limit);
if (is_array($limit)) {
$result = $result->limit(
isset($limit['limit']) ? $limit['limit'] : null,
isset($limit['start']) ? $limit['start'] : null
);
} else {
$result = $result->limit($limit);
}

return $result;
}
Expand Down
63 changes: 63 additions & 0 deletions tests/php/ORM/Search/BasicSearchContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,69 @@ public function testGetQuery(array $searchParams, array $expected)
$this->assertSame($expected, $results->column('Name'));
}

public static function provideGetQueryLimit(): array
{
return [
'no limit' => [
'limit' => null,
'expected' => [
'James',
'John',
'Jane',
'Hemi',
'Sara',
'MatchNothing',
],
],
'limit int' => [
'limit' => 3,
'expected' => [
'James',
'John',
'Jane',
],
],
'paginated limit' => [
'limit' => [
'limit' => 2,
'start' => 3,
],
'expected' => [
'Hemi',
'Sara',
],
],
'limit numeric string' => [
'limit' => '4',
'expected' => [
'James',
'John',
'Jane',
'Hemi',
],
],
'limit invalid string' => [
'limit' => 'blah',
'expected' => [
'James',
'John',
'Jane',
'Hemi',
'Sara',
'MatchNothing',
],
],
];
}

#[DataProvider('provideGetQueryLimit')]
public function testGetQueryLimit(mixed $limit, array $expected): void
{
$context = new BasicSearchContext(ArrayData::class);
$results = $context->getQuery([], limit: $limit, existingQuery: $this->getList());
$this->assertSame($expected, $results->column('Name'));
}

public function testGeneralSearch()
{
$list = $this->getList();
Expand Down

0 comments on commit 6d59a7d

Please sign in to comment.