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+ }
0 commit comments