4
4
5
5
namespace ComplexHeart \Domain \Criteria ;
6
6
7
+ use Closure ;
7
8
use ComplexHeart \Domain \Contracts \Model \ValueObject ;
8
9
use ComplexHeart \Domain \Model \IsValueObject ;
9
10
11
+ use function Lambdish \Phunctional \map ;
12
+
10
13
/**
11
14
* Class Criteria
12
15
*
13
16
* @author Unay Santisteban <usantisteban@othercode.io>
14
- * @package ComplexHeart\SDK\ Domain\Criteria
17
+ * @package ComplexHeart\Domain\Criteria
15
18
*/
16
19
final class Criteria implements ValueObject
17
20
{
@@ -20,130 +23,97 @@ final class Criteria implements ValueObject
20
23
/**
21
24
* Criteria constructor.
22
25
*
23
- * @param FilterGroup<Filter> $filters
26
+ * @param array< FilterGroup<Filter>> $groups
24
27
* @param Order $order
25
28
* @param Page $page
26
29
*/
27
30
public function __construct (
28
- private readonly FilterGroup $ filters ,
31
+ private readonly array $ groups ,
29
32
private readonly Order $ order ,
30
33
private readonly Page $ page ,
31
34
) {
32
- $ this ->check ();
33
35
}
34
36
35
- public static function create (FilterGroup $ filters , Order $ order , Page $ page ): self
37
+ /**
38
+ * @param array<FilterGroup<Filter>> $groups
39
+ * @param Order $order
40
+ * @param Page $page
41
+ * @return Criteria
42
+ */
43
+ public static function create (array $ groups , Order $ order , Page $ page ): self
36
44
{
37
- return new self ($ filters , $ order , $ page );
45
+ return new self ($ groups , $ order , $ page );
38
46
}
39
47
40
- public static function createDefault (): self
48
+ public static function default (): self
41
49
{
42
- return self ::create (FilterGroup:: create () , Order::none (), Page::create ());
50
+ return self ::create ([] , Order::none (), Page::create ());
43
51
}
44
52
45
- public function withFilters (FilterGroup $ filters ): self
53
+ /**
54
+ * Returns a new instance of the criteria with the given FilterGroups.
55
+ *
56
+ * @param array<FilterGroup<Filter>> $groups
57
+ * @return Criteria
58
+ */
59
+ public function withFilterGroups (array $ groups ): self
46
60
{
47
- return new self ($ filters , $ this ->order , $ this ->page );
61
+ return self ::create ($ groups , $ this ->order , $ this ->page );
62
+ }
63
+
64
+ /**
65
+ * Returns a new instance of the criteria adding the given FilterGroup.
66
+ *
67
+ * @param FilterGroup|Closure $group
68
+ * @return $this
69
+ */
70
+ public function withFilterGroup (FilterGroup |Closure $ group ): self
71
+ {
72
+ if (is_callable ($ group )) {
73
+ $ group = $ group (new FilterGroup ());
74
+ }
75
+
76
+ return $ this ->withFilterGroups (array_merge ($ this ->groups , [$ group ]));
48
77
}
49
78
50
79
public function withOrder (Order $ order ): self
51
80
{
52
- return new self ($ this ->filters , $ order , $ this ->page );
81
+ return self :: create ($ this ->groups , $ order , $ this ->page );
53
82
}
54
83
55
84
public function withOrderBy (string $ field ): self
56
85
{
57
- return new self ($ this ->filters , Order::create ($ field , $ this ->orderType ()), $ this ->page );
86
+ return self :: create ($ this ->groups , Order::create ($ field , $ this ->orderType ()), $ this ->page );
58
87
}
59
88
60
89
public function withOrderType (string $ type ): self
61
90
{
62
- return new self ($ this ->filters , Order::create ($ this ->orderBy (), $ type ), $ this ->page );
91
+ return self :: create ($ this ->groups , Order::create ($ this ->orderBy (), $ type ), $ this ->page );
63
92
}
64
93
65
94
public function withPage (Page $ page ): self
66
95
{
67
- return new self ($ this ->filters , $ this ->order , $ page );
96
+ return self :: create ($ this ->groups , $ this ->order , $ page );
68
97
}
69
98
70
99
public function withPageOffset (int $ offset ): self
71
100
{
72
- return new self ($ this ->filters , $ this ->order , Page::create ($ this ->pageLimit (), $ offset ));
101
+ return self :: create ($ this ->groups , $ this ->order , Page::create ($ this ->pageLimit (), $ offset ));
73
102
}
74
103
75
104
public function withPageLimit (int $ limit ): self
76
105
{
77
- return new self ($ this ->filters , $ this ->order , Page::create ($ limit , $ this ->pageOffset ()));
78
- }
79
-
80
- public function filters (): FilterGroup
81
- {
82
- return $ this ->filters ;
83
- }
84
-
85
- public function addFilterEqual (string $ field , mixed $ value ): self
86
- {
87
- $ this ->filters ->addFilter (Filter::createEqual ($ field , $ value ));
88
- return $ this ;
89
- }
90
-
91
- public function addFilterNotEqual (string $ field , mixed $ value ): self
92
- {
93
- $ this ->filters ->addFilter (Filter::createNotEqual ($ field , $ value ));
94
- return $ this ;
95
- }
96
-
97
- public function addFilterGreaterThan (string $ field , string $ value ): self
98
- {
99
- $ this ->filters ->addFilter (Filter::createGreaterThan ($ field , $ value ));
100
- return $ this ;
101
- }
102
-
103
- public function addFilterGreaterOrEqualThan (string $ field , string $ value ): self
104
- {
105
- $ this ->filters ->addFilter (Filter::createGreaterOrEqualThan ($ field , $ value ));
106
- return $ this ;
107
- }
108
-
109
- public function addFilterLessThan (string $ field , string $ value ): self
110
- {
111
- $ this ->filters ->addFilter (Filter::createLessThan ($ field , $ value ));
112
- return $ this ;
113
- }
114
-
115
- public function addFilterLessOrEqualThan (string $ field , string $ value ): self
116
- {
117
- $ this ->filters ->addFilter (Filter::createLessOrEqualThan ($ field , $ value ));
118
- return $ this ;
106
+ return self ::create ($ this ->groups , $ this ->order , Page::create ($ limit , $ this ->pageOffset ()));
119
107
}
120
108
121
109
/**
122
- * @param string $field
123
- * @param array<scalar> $value
124
- * @return $this
125
- */
126
- public function addFilterIn (string $ field , array $ value ): self
127
- {
128
- $ this ->filters ->addFilter (Filter::createIn ($ field , $ value ));
129
- return $ this ;
130
- }
131
-
132
- /**
133
- * @param string $field
134
- * @param array<scalar> $value
135
- * @return $this
110
+ * Returns the list of group filters.
111
+ *
112
+ * @return array<FilterGroup<Filter>>
136
113
*/
137
- public function addFilterNotIn (string $ field , array $ value ): self
138
- {
139
- $ this ->filters ->addFilter (Filter::createNotIn ($ field , $ value ));
140
- return $ this ;
141
- }
142
-
143
- public function addFilterLike (string $ field , string $ value ): self
114
+ public function groups (): array
144
115
{
145
- $ this ->filters ->addFilter (Filter::createLike ($ field , $ value ));
146
- return $ this ;
116
+ return $ this ->groups ;
147
117
}
148
118
149
119
public function order (): Order
@@ -178,6 +148,10 @@ public function pageLimit(): int
178
148
179
149
public function __toString (): string
180
150
{
181
- return sprintf ('%s#%s#%s ' , $ this ->filters , $ this ->order , $ this ->page );
151
+ $ groups = join ('|| ' , map (fn (FilterGroup $ group ): string => $ group ->__toString (), $ this ->groups ));
152
+ $ order = $ this ->order ->__toString ();
153
+ $ page = $ this ->page ->__toString ();
154
+
155
+ return sprintf ('%s#%s#%s ' , $ groups , $ order , $ page );
182
156
}
183
157
}
0 commit comments