-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCriteria.php
More file actions
239 lines (206 loc) · 6.04 KB
/
Criteria.php
File metadata and controls
239 lines (206 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
declare(strict_types=1);
namespace ComplexHeart\Domain\Criteria;
use Closure;
use ComplexHeart\Domain\Contracts\Model\ValueObject;
use ComplexHeart\Domain\Criteria\Contracts\CriteriaSource;
use ComplexHeart\Domain\Criteria\Errors\CriteriaError;
use ComplexHeart\Domain\Model\IsValueObject;
use function Lambdish\Phunctional\map;
/**
* Class Criteria
*
* @author Unay Santisteban <usantisteban@othercode.io>
* @package ComplexHeart\Domain\Criteria
*/
final class Criteria implements ValueObject
{
use IsValueObject;
/**
* Criteria constructor.
*
* @param array<FilterGroup<Filter>> $groups
* @param Order $order
* @param Page $page
*/
public function __construct(
private readonly array $groups,
private readonly Order $order,
private readonly Page $page,
) {
$this->check();
}
protected function invariantGroupsMustBeArrayOfFilterGroup(): bool
{
foreach ($this->groups as $group) {
if (!($group instanceof FilterGroup)) {
return false;
}
}
return true;
}
/**
* @param array<string> $violations
* @return void
*/
protected function invariantHandler(array $violations): void
{
throw CriteriaError::create('Unable to create criteria object.', $violations);
}
/**
* @param array<FilterGroup<Filter>> $groups
* @param Order $order
* @param Page $page
* @return Criteria
*/
public static function create(array $groups, Order $order, Page $page): self
{
return new self(groups: $groups, order: $order, page: $page);
}
public static function default(): self
{
return self::create(
groups: [],
order: Order::none(),
page: Page::default()
);
}
/**
* Creates a new instance of Criteria from the given data source.
*
* @param CriteriaSource $source
* @return Criteria
*/
public static function fromSource(CriteriaSource $source): self
{
return self::create(
groups: map(
fn (array $g): FilterGroup => FilterGroup::fromArray($g),
$source->filterGroups()
),
order: Order::create($source->orderBy(), OrderType::make($source->orderType())),
page: $source->pageNumber() > 0
? Page::number($source->pageNumber(), $source->pageLimit())
: Page::create($source->pageLimit(), $source->pageOffset())
);
}
/**
* Returns a new instance of the criteria with the given FilterGroups.
*
* @param array<FilterGroup<Filter>> $groups
* @return Criteria
*/
public function withFilterGroups(array $groups): self
{
return self::create(
groups: $groups,
order: $this->order,
page: $this->page
);
}
/**
* Returns a new instance of the criteria adding the given FilterGroup.
*
* @param FilterGroup|Closure $group
* @return Criteria
*/
public function withFilterGroup(FilterGroup|Closure $group): self
{
$group = $group instanceof FilterGroup ? $group : $group(FilterGroup::create());
// push single FilterGroup into an array.
$group = is_array($group) ? $group : [$group];
return $this->withFilterGroups(groups: array_merge($this->groups, $group));
}
public function withOrder(Order $order): self
{
return self::create(groups: $this->groups, order: $order, page: $this->page);
}
public function withOrderRandom(): self
{
return self::create(groups: $this->groups, order: Order::random(), page: $this->page);
}
public function withOrderBy(string $field): self
{
return self::create(
groups: $this->groups,
order: Order::create($field, $this->order->type()),
page: $this->page
);
}
public function withOrderType(string $type): self
{
return self::create(
groups: $this->groups,
order: Order::create($this->orderBy(), OrderType::make($type)),
page: $this->page
);
}
public function withPage(Page $page): self
{
return self::create(groups: $this->groups, order: $this->order, page: $page);
}
public function withPageLimit(int $limit): self
{
return self::create(
groups: $this->groups,
order: $this->order,
page: Page::create($limit, $this->pageOffset())
);
}
public function withPageOffset(int $offset): self
{
return self::create(
groups: $this->groups,
order: $this->order,
page: Page::create($this->pageLimit(), $offset)
);
}
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->pageLimit() : $size)
);
}
/**
* Returns the list of group filters.
*
* @return array<FilterGroup<Filter>>
*/
public function groups(): array
{
return $this->groups;
}
public function order(): Order
{
return $this->order;
}
public function orderBy(): string
{
return $this->order->by();
}
public function orderType(): string
{
return $this->order->type()->value;
}
public function page(): Page
{
return $this->page;
}
public function pageLimit(): int
{
return $this->page->limit();
}
public function pageOffset(): int
{
return $this->page->offset();
}
public function __toString(): string
{
$groups = join('||', map(fn (FilterGroup $group): string => $group->__toString(), $this->groups));
$order = $this->order->__toString();
$page = $this->page->__toString();
return sprintf('%s#%s#%s', $groups, $order, $page);
}
}