Skip to content

Commit a937e7f

Browse files
authored
Feature/Add page number support. (#20)
1 parent 030292a commit a937e7f

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ $criteria = Criteria::default()
3232
->withFilterGroup($g1)
3333
->withOrderBy('surname')
3434
->withOrderType('asc')
35-
->withPageLimit(10)
36-
->withPageOffset(5);
35+
->withPageLimit(25)
36+
->withPageOffset(50);
3737

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

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

5151
// In SQL, we may have something like:
5252
// WHERE status = 1 AND followers >= 700 AND country in ('es', 'fr')
@@ -66,6 +66,5 @@ $criteria = Criteria::default()
6666
->withFilterGroup(FilterGroup::create()->addFilterContains('content', $term))
6767
->withOrderBy('created_at')
6868
->withOrderType(Order::TYPE_ASC)
69-
->withPageLimit(10)
70-
->withPageOffset(5);
69+
->withPageNumber(3);
7170
```

src/Criteria.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function withFilterGroups(array $groups): self
110110
* Returns a new instance of the criteria adding the given FilterGroup.
111111
*
112112
* @param FilterGroup|Closure $group
113-
* @return $this
113+
* @return Criteria
114114
*/
115115
public function withFilterGroup(FilterGroup|Closure $group): self
116116
{
@@ -175,6 +175,15 @@ public function withPageLimit(int $limit): self
175175
);
176176
}
177177

178+
public function withPageNumber(int $number, int $size = null): self
179+
{
180+
return self::create(
181+
groups: $this->groups,
182+
order: $this->order,
183+
page: Page::number($number, is_null($size) ? $this->page->limit() : $size)
184+
);
185+
}
186+
178187
/**
179188
* Returns the list of group filters.
180189
*

src/Page.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,32 @@ final class Page implements ValueObject
1717
{
1818
use IsValueObject;
1919

20+
const DEFAULT_LIMIT = 25;
21+
const DEFAULT_OFFSET = 0;
22+
2023
/**
2124
* Page constructor.
2225
*
2326
* @param int $limit
2427
* @param int $offset
2528
*/
2629
public function __construct(
27-
private readonly int $limit = 25,
28-
private readonly int $offset = 0,
30+
private readonly int $limit = self::DEFAULT_LIMIT,
31+
private readonly int $offset = self::DEFAULT_OFFSET,
2932
) {
3033
$this->check();
3134
}
3235

33-
public static function create(int $limit = 25, int $offset = 0): self
36+
public static function create(int $limit = self::DEFAULT_LIMIT, int $offset = self::DEFAULT_OFFSET): self
3437
{
3538
return new self($limit, $offset);
3639
}
3740

41+
public static function number(int $number, int $size = self::DEFAULT_LIMIT): self
42+
{
43+
return self::create($size, ($size * $number) - $size);
44+
}
45+
3846
protected function invariantLimitValueMustBePositive(): bool
3947
{
4048
return $this->limit >= 0;

tests/CriteriaTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,12 @@ public function pageOffset(): int
144144
->withOrderType('asc');
145145

146146
expect($c->__toString())->toBe('name.=.Vincent+age.>=.35#name.asc#100.0');
147-
});
147+
});
148+
149+
test('Criteria should configure limit and offset using page number', function () {
150+
$c = Criteria::default()
151+
->withPageNumber(3, 25);
152+
153+
expect($c->page()->limit())->toBe(25)
154+
->and($c->page()->offset())->toBe(50);
155+
});

0 commit comments

Comments
 (0)