|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TKuni\PhpNormalizer; |
|
4
|
|
|
|
|
5
|
|
|
use TKuni\PhpNormalizer\Filters\BooleanFilter; |
|
6
|
|
|
use TKuni\PhpNormalizer\Filters\ClosureFilter; |
|
7
|
|
|
use TKuni\PhpNormalizer\Filters\ConditionalFilter; |
|
8
|
|
|
use TKuni\PhpNormalizer\Filters\FloatFilter; |
|
9
|
|
|
use TKuni\PhpNormalizer\Filters\IntegerFilter; |
|
10
|
|
|
use TKuni\PhpNormalizer\Filters\Contracts\FilterContract; |
|
11
|
|
|
use TKuni\PhpNormalizer\Filters\StringFilter; |
|
12
|
|
|
use TKuni\PhpNormalizer\Filters\ToNullFilter; |
|
13
|
|
|
use TKuni\PhpNormalizer\Filters\ToValueFilter; |
|
14
|
|
|
|
|
15
|
|
|
class Condition |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var null|\Closure |
|
19
|
|
|
*/ |
|
20
|
|
|
private $condition; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct($condition) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->condition = $condition; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public static function is($value) { |
|
28
|
|
|
if (is_callable($value)) { |
|
29
|
|
|
return new Condition($value); |
|
30
|
|
|
} else { |
|
31
|
|
|
return new Condition(function($in) use ($value) { |
|
32
|
|
|
return $in == $value; |
|
33
|
|
|
}); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function isEmpty() { |
|
38
|
|
|
return new Condition(function($in) { |
|
39
|
|
|
return empty($in); |
|
40
|
|
|
}); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function isNotEmpty() { |
|
44
|
|
|
return new Condition(function($in) { |
|
45
|
|
|
return !empty($in); |
|
46
|
|
|
}); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function isNotNull() { |
|
50
|
|
|
return new Condition(function($in) { |
|
51
|
|
|
return !is_null($in); |
|
52
|
|
|
}); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public static function isAny() { |
|
56
|
|
|
return new Condition(null); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function isContains($text) { |
|
60
|
|
|
return new Condition(function($in) use ($text) { |
|
61
|
|
|
return strpos($in, $text) !== false; |
|
62
|
|
|
}); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public static function isRegexp($regexp) { |
|
66
|
|
|
return new Condition(function($in) use ($regexp) { |
|
67
|
|
|
return preg_match($regexp, $in); |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function to($value) { |
|
72
|
|
|
if (is_callable($value)) { |
|
73
|
|
|
return new ConditionalFilter($this->condition, new ClosureFilter($value)); |
|
74
|
|
|
} else if (is_object($value) && $value instanceof FilterContract) { |
|
75
|
|
|
return new ConditionalFilter($this->condition, $value); |
|
76
|
|
|
} else { |
|
77
|
|
|
return new ConditionalFilter($this->condition, new ToValueFilter($value)); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function toNull() { |
|
82
|
|
|
return new ConditionalFilter($this->condition, new ToNullFilter()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function toInt() { |
|
86
|
|
|
return new ConditionalFilter($this->condition, new IntegerFilter()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function toFloat() { |
|
90
|
|
|
return new ConditionalFilter($this->condition, new FloatFilter()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function toBoolean() { |
|
94
|
|
|
return new ConditionalFilter($this->condition, new BooleanFilter()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function toString() { |
|
98
|
|
|
return new ConditionalFilter($this->condition, new StringFilter()); |
|
99
|
|
|
} |
|
100
|
|
|
} |