Skip to content

Commit 347bbb6

Browse files
authored
Release 0.3.0
2 parents edb931a + 430e44a commit 347bbb6

File tree

22 files changed

+875
-34
lines changed

22 files changed

+875
-34
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ jobs:
1111
strategy:
1212
matrix:
1313
php-version:
14-
- "7.4"
14+
- 7.4
15+
- 8.0
1516

1617
steps:
1718
- name: Checkout source code
@@ -22,7 +23,7 @@ jobs:
2223
with:
2324
php-version: ${{ matrix.php-version }}
2425
extensions: mbstring
25-
coverage: xdebug
26+
coverage: pcov
2627
tools: composer:v2
2728

2829
- name: Cache dependencies

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"spatie/data-transfer-object": "^2.6"
2020
},
2121
"require-dev": {
22-
"ext-xdebug": "*",
2322
"mockery/mockery": "^1.4",
2423
"phpunit/phpunit": "^9.3",
2524
"fakerphp/faker": "^1.9.1",
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OtherCode\ComplexHeart\Application;
6+
7+
use OtherCode\ComplexHeart\Domain\Criteria\Criteria;
8+
use OtherCode\ComplexHeart\Domain\Criteria\Filter;
9+
use OtherCode\ComplexHeart\Domain\Criteria\FilterGroup;
10+
use OtherCode\ComplexHeart\Domain\Criteria\Order;
11+
use OtherCode\ComplexHeart\Domain\Criteria\Page;
12+
13+
/**
14+
* Class CriteriaBuilder
15+
*
16+
* @author Unay Santisteban <[email protected]>
17+
* @package OtherCode\ComplexHeart\Application
18+
*/
19+
final class CriteriaBuilder
20+
{
21+
private array $filters = [];
22+
23+
private string $orderBy = '';
24+
25+
private string $orderType = 'asc';
26+
27+
private int $pageOffset = 0;
28+
29+
private int $pageLimit = 1000;
30+
31+
/**
32+
* Adds an arbitrary filter.
33+
*
34+
* @param string $field
35+
* @param string $operator
36+
* @param mixed $value
37+
* @return $this
38+
*/
39+
public function filter(string $field, string $operator, $value): self
40+
{
41+
$this->filters[] = [$field, $operator, $value];
42+
return $this;
43+
}
44+
45+
/**
46+
* Adds new filter for equals operator.
47+
*
48+
* @param string $field
49+
* @param mixed $value
50+
* @return $this
51+
*/
52+
public function filterEqual(string $field, $value): self
53+
{
54+
$this->filter($field, Filter::EQUAL, $value);
55+
return $this;
56+
}
57+
58+
/**
59+
* Adds new filter for not equals operator.
60+
*
61+
* @param string $field
62+
* @param mixed $value
63+
* @return $this
64+
*/
65+
public function filterNotEqual(string $field, $value): self
66+
{
67+
$this->filter($field, Filter::NOT_EQUAL, $value);
68+
return $this;
69+
}
70+
71+
/**
72+
* Adds new filter for greater than operator.
73+
*
74+
* @param string $field
75+
* @param mixed $value
76+
* @return $this
77+
*/
78+
public function filterGreaterThan(string $field, $value): self
79+
{
80+
$this->filter($field, Filter::GT, $value);
81+
return $this;
82+
}
83+
84+
/**
85+
* Adds new filter for greater or equal than operator.
86+
*
87+
* @param string $field
88+
* @param mixed $value
89+
* @return $this
90+
*/
91+
public function filterGreaterOrEqualThan(string $field, $value): self
92+
{
93+
$this->filter($field, Filter::GTE, $value);
94+
return $this;
95+
}
96+
97+
/**
98+
* Adds new filter for less than operator.
99+
*
100+
* @param string $field
101+
* @param mixed $value
102+
* @return $this
103+
*/
104+
public function filterLessThan(string $field, $value): self
105+
{
106+
$this->filter($field, Filter::LT, $value);
107+
return $this;
108+
}
109+
110+
/**
111+
* Adds new filter for less or equal than operator.
112+
*
113+
* @param string $field
114+
* @param mixed $value
115+
* @return $this
116+
*/
117+
public function filterLessOrEqualThan(string $field, $value): self
118+
{
119+
$this->filter($field, Filter::LTE, $value);
120+
return $this;
121+
}
122+
123+
/**
124+
* Adds new filter for in operator.
125+
*
126+
* @param string $field
127+
* @param mixed $value
128+
* @return $this
129+
*/
130+
public function filterIn(string $field, $value): self
131+
{
132+
$this->filter($field, Filter::IN, $value);
133+
return $this;
134+
}
135+
136+
/**
137+
* Adds new filter for not in operator.
138+
*
139+
* @param string $field
140+
* @param mixed $value
141+
* @return $this
142+
*/
143+
public function filterNotIn(string $field, $value): self
144+
{
145+
$this->filter($field, Filter::NOT_IN, $value);
146+
return $this;
147+
}
148+
149+
/**
150+
* Adds new filter for like operator.
151+
*
152+
* @param string $field
153+
* @param mixed $value
154+
* @return $this
155+
*/
156+
public function filterLike(string $field, $value): self
157+
{
158+
$this->filter($field, Filter::LIKE, $value);
159+
return $this;
160+
}
161+
162+
/**
163+
* Sets the order by field parameter.
164+
*
165+
* @param string $field
166+
* @return $this
167+
*/
168+
public function orderedBy(string $field): self
169+
{
170+
$this->orderBy = $field;
171+
return $this;
172+
}
173+
174+
/**
175+
* Sets the order type parameter.
176+
*
177+
* @param string $type
178+
* @return $this
179+
*/
180+
public function orderedType(string $type): self
181+
{
182+
$this->orderType = $type;
183+
return $this;
184+
}
185+
186+
/**
187+
* Set the page limit parameter.
188+
*
189+
* @param int $limit
190+
* @return $this
191+
*/
192+
public function withLimit(int $limit): self
193+
{
194+
$this->pageLimit = $limit;
195+
return $this;
196+
}
197+
198+
/**
199+
* Set the page offset parameter.
200+
*
201+
* @param int $offset
202+
* @return $this
203+
*/
204+
public function withOffset(int $offset): self
205+
{
206+
$this->pageOffset = $offset;
207+
return $this;
208+
}
209+
210+
/**
211+
* Builds the Criteria object.
212+
*
213+
* @return Criteria
214+
*/
215+
public function build(): Criteria
216+
{
217+
return new Criteria(
218+
FilterGroup::create($this->filters),
219+
Order::create($this->orderBy, $this->orderType),
220+
Page::create($this->pageLimit, $this->pageOffset)
221+
);
222+
}
223+
}

src/Domain/Criteria/FilterGroup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static function create(array $filters): FilterGroup
4242

4343
public function add(Filter $filter): FilterGroup
4444
{
45-
return new self(array_merge($this->filters, [$filter]));
45+
return new self(...array_merge($this->filters, [$filter]));
4646
}
4747

4848
public function filters(): array
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OtherCode\ComplexHeart\Domain\Exceptions;
6+
7+
use RuntimeException;
8+
9+
/**
10+
* Class ImmutableException
11+
*
12+
* @author Unay Santisteban <[email protected]>
13+
* @package OtherCode\ComplexHeart\Domain\Exceptions
14+
*/
15+
class ImmutableException extends RuntimeException
16+
{
17+
18+
}

0 commit comments

Comments
 (0)