File tree Expand file tree Collapse file tree 4 files changed +35
-11
lines changed Expand file tree Collapse file tree 4 files changed +35
-11
lines changed Original file line number Diff line number Diff line change @@ -32,8 +32,8 @@ $criteria = Criteria::default()
32
32
->withFilterGroup($g1)
33
33
->withOrderBy('surname')
34
34
->withOrderType('asc')
35
- ->withPageLimit(10 )
36
- ->withPageOffset(5 );
35
+ ->withPageLimit(25 )
36
+ ->withPageOffset(50 );
37
37
38
38
$users = $repository->match($criteria);
39
39
@@ -45,8 +45,8 @@ $criteria = Criteria::default()
45
45
->addFilterIn('country', ['es', 'fr']))
46
46
->withOrderBy('surname')
47
47
->withOrderType('asc')
48
- ->withPageLimit(10 )
49
- ->withPageOffset(5 );
48
+ ->withPageLimit(25 )
49
+ ->withPageOffset(50 );
50
50
51
51
// In SQL, we may have something like:
52
52
// WHERE status = 1 AND followers >= 700 AND country in ('es', 'fr')
@@ -66,6 +66,5 @@ $criteria = Criteria::default()
66
66
->withFilterGroup(FilterGroup::create()->addFilterContains('content', $term))
67
67
->withOrderBy('created_at')
68
68
->withOrderType(Order::TYPE_ASC)
69
- ->withPageLimit(10)
70
- ->withPageOffset(5);
69
+ ->withPageNumber(3);
71
70
```
Original file line number Diff line number Diff line change @@ -110,7 +110,7 @@ public function withFilterGroups(array $groups): self
110
110
* Returns a new instance of the criteria adding the given FilterGroup.
111
111
*
112
112
* @param FilterGroup|Closure $group
113
- * @return $this
113
+ * @return Criteria
114
114
*/
115
115
public function withFilterGroup (FilterGroup |Closure $ group ): self
116
116
{
@@ -175,6 +175,15 @@ public function withPageLimit(int $limit): self
175
175
);
176
176
}
177
177
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
+
178
187
/**
179
188
* Returns the list of group filters.
180
189
*
Original file line number Diff line number Diff line change @@ -17,24 +17,32 @@ final class Page implements ValueObject
17
17
{
18
18
use IsValueObject;
19
19
20
+ const DEFAULT_LIMIT = 25 ;
21
+ const DEFAULT_OFFSET = 0 ;
22
+
20
23
/**
21
24
* Page constructor.
22
25
*
23
26
* @param int $limit
24
27
* @param int $offset
25
28
*/
26
29
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 ,
29
32
) {
30
33
$ this ->check ();
31
34
}
32
35
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
34
37
{
35
38
return new self ($ limit , $ offset );
36
39
}
37
40
41
+ public static function number (int $ number , int $ size = self ::DEFAULT_LIMIT ): self
42
+ {
43
+ return self ::create ($ size , ($ size * $ number ) - $ size );
44
+ }
45
+
38
46
protected function invariantLimitValueMustBePositive (): bool
39
47
{
40
48
return $ this ->limit >= 0 ;
Original file line number Diff line number Diff line change @@ -144,4 +144,12 @@ public function pageOffset(): int
144
144
->withOrderType ('asc ' );
145
145
146
146
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
+ });
You can’t perform that action at this time.
0 commit comments