1
|
|
|
<?php |
2
|
|
|
namespace Kir\MySQL\Builder\Expr; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
|
6
|
|
|
class DBExprFilter implements OptionalExpression { |
7
|
|
|
/** @var */ |
8
|
|
|
private $expression; |
9
|
|
|
/** @var mixed */ |
10
|
|
|
private $value; |
11
|
|
|
/** @var string[] */ |
12
|
|
|
private $keyPath; |
13
|
|
|
/** @var callable|null */ |
14
|
|
|
private $validator; |
15
|
|
|
/** @var null */ |
16
|
|
|
private $validationResultHandler; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $expression |
20
|
|
|
* @param array $data |
21
|
|
|
* @param string|string[] $keyPath |
22
|
|
|
* @param callable|null $validator |
23
|
|
|
* @param callable|null $validationResultHandler |
24
|
|
|
* @throws Exception |
25
|
|
|
*/ |
26
|
|
|
public function __construct($expression, array $data, $keyPath, $validator = null, $validationResultHandler = null) { |
27
|
|
|
$this->expression = $expression; |
28
|
|
|
$this->value = $data; |
29
|
|
|
$this->keyPath = $this->buildKey($keyPath); |
30
|
|
|
$this->value = $this->recursiveGet($data, $this->keyPath, null); |
31
|
|
|
if($validator === null) { |
32
|
|
|
$validator = function ($data) { |
33
|
|
|
return (string) $data !== ''; |
34
|
|
|
}; |
35
|
|
|
} |
36
|
|
|
if($validationResultHandler === null) { |
37
|
|
|
$validationResultHandler = function ($data) {}; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
$this->validator = $validator; |
40
|
|
|
$this->validationResultHandler = $validationResultHandler; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function getExpression() { |
47
|
|
|
return $this->expression; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function isValid() { |
54
|
|
|
$result = call_user_func($this->validator, $this->value); |
55
|
|
|
call_user_func($this->validationResultHandler, $result, [ |
56
|
|
|
'value' => $this->value, |
57
|
|
|
'key' => join('.', $this->keyPath), |
58
|
|
|
]); |
59
|
|
|
return $result; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function getValue() { |
66
|
|
|
return [$this->value]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string|string[] $keyPath |
71
|
|
|
* @return string |
72
|
|
|
* @throws Exception |
73
|
|
|
*/ |
74
|
|
|
private function buildKey($keyPath) { |
75
|
|
|
if(is_string($keyPath)) { |
76
|
|
|
$keyPath = explode('.', $keyPath); |
77
|
|
|
} |
78
|
|
|
if(!is_array($keyPath)) { |
79
|
|
|
throw new Exception('Invalid key'); |
80
|
|
|
} |
81
|
|
|
return $keyPath; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array $array |
86
|
|
|
* @param array $path |
87
|
|
|
* @param mixed $default |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
private function recursiveGet($array, $path, $default) { |
91
|
|
|
$count = count($path); |
92
|
|
|
if (!$count) { |
93
|
|
|
return $default; |
94
|
|
|
} |
95
|
|
|
for($idx = 0; $idx < $count; $idx++) { |
96
|
|
|
$part = $path[$idx]; |
97
|
|
|
if(!array_key_exists($part, $array)) { |
98
|
|
|
return $default; |
99
|
|
|
} |
100
|
|
|
$array = $array[$part]; |
101
|
|
|
} |
102
|
|
|
return $array; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.