1
1
<?php
2
2
namespace Kir \MySQL \Builder \Expr ;
3
3
4
+ use Kir \MySQL \Builder \Helpers \RecursiveStructureAccess ;
4
5
use RuntimeException ;
5
6
6
7
class DBExprFilter implements OptionalExpression {
7
8
/** @var mixed */
8
9
private $ expression ;
10
+ /** @var bool */
11
+ private $ hasValue ;
9
12
/** @var mixed */
10
13
private $ value ;
11
14
/** @var string[] */
@@ -24,21 +27,18 @@ class DBExprFilter implements OptionalExpression {
24
27
*/
25
28
public function __construct ($ expression , array $ data , $ keyPath , $ validator = null , $ validationResultHandler = null ) {
26
29
$ this ->expression = $ expression ;
27
- $ this ->value = $ data ;
28
30
$ this ->keyPath = $ this ->buildKey ($ keyPath );
29
- $ this ->value = $ this ->recursiveGet ($ data , $ this ->keyPath , null );
31
+ $ this ->hasValue = RecursiveStructureAccess::recursiveHas ($ data , $ this ->keyPath );
32
+ $ this ->value = RecursiveStructureAccess::recursiveGet ($ data , $ this ->keyPath , null );
30
33
if ($ validator === null ) {
31
- $ validator = function ($ data ) {
32
- if (is_array ($ data )) {
33
- return $ this ->isValidArray ($ data );
34
- }
35
- return (string ) $ data !== '' ;
34
+ $ validator = function () {
35
+ return true ;
36
36
};
37
37
}
38
+ $ this ->validator = $ validator ;
38
39
if ($ validationResultHandler === null ) {
39
40
$ validationResultHandler = static function () {};
40
41
}
41
- $ this ->validator = $ validator ;
42
42
$ this ->validationResultHandler = $ validationResultHandler ;
43
43
}
44
44
@@ -53,6 +53,9 @@ public function getExpression() {
53
53
* @return bool
54
54
*/
55
55
public function isValid () {
56
+ if (!$ this ->hasValue ) {
57
+ return false ;
58
+ }
56
59
$ result = call_user_func ($ this ->validator , $ this ->value );
57
60
call_user_func ($ this ->validationResultHandler , $ result , [
58
61
'value ' => $ this ->value ,
@@ -95,25 +98,4 @@ private function isValidArray(array $array) {
95
98
});
96
99
return count ($ data ) > 0 ;
97
100
}
98
-
99
- /**
100
- * @param array $array
101
- * @param array $path
102
- * @param mixed $default
103
- * @return array
104
- */
105
- private function recursiveGet ($ array , $ path , $ default ) {
106
- $ count = count ($ path );
107
- if (!$ count ) {
108
- return $ default ;
109
- }
110
- foreach ($ path as $ idxValue ) {
111
- $ part = $ idxValue ;
112
- if (!array_key_exists ($ part , $ array )) {
113
- return $ default ;
114
- }
115
- $ array = $ array [$ part ];
116
- }
117
- return $ array ;
118
- }
119
101
}
0 commit comments