Skip to content

Commit 84d39ed

Browse files
authored
Merge pull request #22 from GitHubHubus/conditionRefactor
Refactoring condition
2 parents 1d77cb5 + bc1e6bf commit 84d39ed

File tree

2 files changed

+298
-285
lines changed

2 files changed

+298
-285
lines changed

src/Condition.php

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
<?php
2+
3+
namespace Nahid\JsonQ;
4+
5+
class Condition
6+
{
7+
/**
8+
* Simple equals
9+
*
10+
* @param mixed $value
11+
* @param mixed $comparable
12+
*
13+
* @return bool
14+
*/
15+
public static function equal($value, $comparable)
16+
{
17+
return $value == $comparable;
18+
}
19+
20+
/**
21+
* Strict equals
22+
*
23+
* @param mixed $value
24+
* @param mixed $comparable
25+
*
26+
* @return bool
27+
*/
28+
public static function strictEqual($value, $comparable)
29+
{
30+
return $value === $comparable;
31+
}
32+
33+
/**
34+
* Simple not equal
35+
*
36+
* @param mixed $value
37+
* @param mixed $comparable
38+
*
39+
* @return bool
40+
*/
41+
public static function notEqual($value, $comparable)
42+
{
43+
return $value != $comparable;
44+
}
45+
46+
/**
47+
* Strict not equal
48+
*
49+
* @param mixed $value
50+
* @param mixed $comparable
51+
*
52+
* @return bool
53+
*/
54+
public static function notExactEqual($value, $comparable)
55+
{
56+
return $value !== $comparable;
57+
}
58+
59+
/**
60+
* Strict greater than
61+
*
62+
* @param mixed $value
63+
* @param mixed $comparable
64+
*
65+
* @return bool
66+
*/
67+
public static function greaterThan($value, $comparable)
68+
{
69+
return $value > $comparable;
70+
}
71+
72+
/**
73+
* Strict less than
74+
*
75+
* @param mixed $value
76+
* @param mixed $comparable
77+
*
78+
* @return bool
79+
*/
80+
public static function lessThan($value, $comparable)
81+
{
82+
return $value < $comparable;
83+
}
84+
85+
/**
86+
* Greater or equal
87+
*
88+
* @param mixed $value
89+
* @param mixed $comparable
90+
*
91+
* @return bool
92+
*/
93+
public static function greaterThanOrEqual($value, $comparable)
94+
{
95+
return $value >= $comparable;
96+
}
97+
98+
/**
99+
* Less or equal
100+
*
101+
* @param mixed $value
102+
* @param mixed $comparable
103+
*
104+
* @return bool
105+
*/
106+
public static function lessThanOrEqual($value, $comparable)
107+
{
108+
return $value <= $comparable;
109+
}
110+
111+
/**
112+
* In array
113+
*
114+
* @param mixed $value
115+
* @param array $comparable
116+
*
117+
* @return bool
118+
*/
119+
public static function in($value, $comparable)
120+
{
121+
return (is_array($comparable) && in_array($value, $comparable));
122+
}
123+
124+
/**
125+
* Not in array
126+
*
127+
* @param mixed $value
128+
* @param array $comparable
129+
*
130+
* @return bool
131+
*/
132+
public static function notIn($value, $comparable)
133+
{
134+
return (is_array($comparable) && !in_array($value, $comparable));
135+
}
136+
137+
/**
138+
* Is null equal
139+
*
140+
* @param mixed $value
141+
*
142+
* @return bool
143+
*/
144+
public static function isNull($value)
145+
{
146+
return is_null($value);
147+
}
148+
149+
/**
150+
* Is not null equal
151+
*
152+
* @param mixed $value
153+
*
154+
* @return bool
155+
*/
156+
public static function isNotNull($value)
157+
{
158+
return !is_null($value);
159+
}
160+
161+
/**
162+
* Start With
163+
*
164+
* @param mixed $value
165+
* @param string $comparable
166+
*
167+
* @return bool
168+
*/
169+
public static function startWith($value, $comparable)
170+
{
171+
if (preg_match("/^$comparable/", $value)) {
172+
return true;
173+
}
174+
175+
return false;
176+
}
177+
178+
/**
179+
* End with
180+
*
181+
* @param mixed $value
182+
* @param string $comparable
183+
*
184+
* @return bool
185+
*/
186+
public static function endWith($value, $comparable)
187+
{
188+
if (preg_match("/$comparable$/", $value)) {
189+
return true;
190+
}
191+
192+
return false;
193+
}
194+
195+
/**
196+
* Match with pattern
197+
*
198+
* @param mixed $value
199+
* @param string $comparable
200+
*
201+
* @return bool
202+
*/
203+
public static function match($value, $comparable)
204+
{
205+
$comparable = trim($comparable);
206+
207+
if (preg_match("/^$comparable$/", $value)) {
208+
return true;
209+
}
210+
211+
return false;
212+
}
213+
214+
/**
215+
* Contains substring in string
216+
*
217+
* @param string $value
218+
* @param string $comparable
219+
*
220+
* @return bool
221+
*/
222+
public static function contains($value, $comparable)
223+
{
224+
return (strpos($value, $comparable) !== false);
225+
}
226+
227+
/**
228+
* Dates equal
229+
*
230+
* @param string $value
231+
* @param string $comparable
232+
*
233+
* @return bool
234+
*/
235+
public static function dateEqual($value, $comparable, $format = 'Y-m-d')
236+
{
237+
$date = date($format, strtotime($value));
238+
return $date == $comparable;
239+
}
240+
241+
/**
242+
* Months equal
243+
*
244+
* @param string $value
245+
* @param string $comparable
246+
*
247+
* @return bool
248+
*/
249+
public static function monthEqual($value, $comparable)
250+
{
251+
$month = date('m', strtotime($value));
252+
return $month == $comparable;
253+
}
254+
255+
/**
256+
* Years equal
257+
*
258+
* @param string $value
259+
* @param string $comparable
260+
*
261+
* @return bool
262+
*/
263+
public static function yearEqual($value, $comparable)
264+
{
265+
$year = date('Y', strtotime($value));
266+
return $year == $comparable;
267+
}
268+
}

0 commit comments

Comments
 (0)