Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ $criteria = Criteria::default()
->withFilterGroup($g1)
->withOrderBy('surname')
->withOrderType('asc')
->withPageLimit(10)
->withPageOffset(5);
->withPageLimit(25)
->withPageOffset(50);

$users = $repository->match($criteria);

Expand All @@ -45,8 +45,8 @@ $criteria = Criteria::default()
->addFilterIn('country', ['es', 'fr']))
->withOrderBy('surname')
->withOrderType('asc')
->withPageLimit(10)
->withPageOffset(5);
->withPageLimit(25)
->withPageOffset(50);

// In SQL, we may have something like:
// WHERE status = 1 AND followers >= 700 AND country in ('es', 'fr')
Expand All @@ -66,6 +66,5 @@ $criteria = Criteria::default()
->withFilterGroup(FilterGroup::create()->addFilterContains('content', $term))
->withOrderBy('created_at')
->withOrderType(Order::TYPE_ASC)
->withPageLimit(10)
->withPageOffset(5);
->withPageNumber(3);
```
11 changes: 10 additions & 1 deletion src/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function withFilterGroups(array $groups): self
* Returns a new instance of the criteria adding the given FilterGroup.
*
* @param FilterGroup|Closure $group
* @return $this
* @return Criteria
*/
public function withFilterGroup(FilterGroup|Closure $group): self
{
Expand Down Expand Up @@ -175,6 +175,15 @@ public function withPageLimit(int $limit): self
);
}

public function withPageNumber(int $number, int $size = null): self
{
return self::create(
groups: $this->groups,
order: $this->order,
page: Page::number($number, is_null($size) ? $this->page->limit() : $size)
);
}

/**
* Returns the list of group filters.
*
Expand Down
14 changes: 11 additions & 3 deletions src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,32 @@ final class Page implements ValueObject
{
use IsValueObject;

const DEFAULT_LIMIT = 25;
const DEFAULT_OFFSET = 0;

/**
* Page constructor.
*
* @param int $limit
* @param int $offset
*/
public function __construct(
private readonly int $limit = 25,
private readonly int $offset = 0,
private readonly int $limit = self::DEFAULT_LIMIT,
private readonly int $offset = self::DEFAULT_OFFSET,
) {
$this->check();
}

public static function create(int $limit = 25, int $offset = 0): self
public static function create(int $limit = self::DEFAULT_LIMIT, int $offset = self::DEFAULT_OFFSET): self
{
return new self($limit, $offset);
}

public static function number(int $number, int $size = self::DEFAULT_LIMIT): self
{
return self::create($size, ($size * $number) - $size);
}

protected function invariantLimitValueMustBePositive(): bool
{
return $this->limit >= 0;
Expand Down
10 changes: 9 additions & 1 deletion tests/CriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,12 @@ public function pageOffset(): int
->withOrderType('asc');

expect($c->__toString())->toBe('name.=.Vincent+age.>=.35#name.asc#100.0');
});
});

test('Criteria should configure limit and offset using page number', function () {
$c = Criteria::default()
->withPageNumber(3, 25);

expect($c->page()->limit())->toBe(25)
->and($c->page()->offset())->toBe(50);
});