|
1
|
|
|
<?php |
|
2
|
|
|
namespace Kir\MySQL\Builder\Expr; |
|
3
|
|
|
|
|
4
|
|
|
use Kir\MySQL\Builder\Helpers\RecursiveStructureAccess; |
|
5
|
|
|
use RuntimeException; |
|
6
|
|
|
|
|
7
|
|
|
class DBExprFilter implements OptionalExpression { |
|
8
|
|
|
/** @var bool */ |
|
9
|
|
|
private $hasValue; |
|
10
|
|
|
/** @var mixed */ |
|
11
|
|
|
private $value; |
|
12
|
|
|
/** @var null|callable(mixed): bool */ |
|
13
|
|
|
private $validator; |
|
14
|
|
|
/** @var callable(bool, array{key: mixed, value: string}) */ |
|
15
|
|
|
private $validationResultHandler; |
|
16
|
|
|
/** @var string[] */ |
|
17
|
|
|
private array $keyPath; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $expression |
|
21
|
|
|
* @param array<string, mixed> $data |
|
22
|
|
|
* @param string|string[] $keyPath |
|
23
|
|
|
* @param callable|null $validator |
|
24
|
|
|
* @param callable|null $validationResultHandler |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct( |
|
27
|
|
|
private string $expression, |
|
28
|
|
|
array $data, |
|
29
|
|
|
string|array $keyPath, |
|
30
|
|
|
$validator = null, |
|
31
|
|
|
$validationResultHandler = null |
|
32
|
|
|
) { |
|
33
|
|
|
$this->keyPath = $this->buildKey($keyPath); |
|
34
|
|
|
$this->value = RecursiveStructureAccess::recursiveGet($data, $this->keyPath, null); |
|
35
|
|
|
$this->hasValue = is_scalar($this->value) ? trim((string) $this->value) !== '' : !empty($this->value); |
|
36
|
|
|
if($validator === null) { |
|
37
|
|
|
$validator = static fn() => true; |
|
38
|
|
|
} |
|
39
|
|
|
$this->validator = $validator; |
|
40
|
|
|
if($validationResultHandler === null) { |
|
41
|
|
|
$validationResultHandler = static function () {}; |
|
42
|
|
|
} |
|
43
|
|
|
$this->validationResultHandler = $validationResultHandler; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getExpression(): string { |
|
50
|
|
|
return $this->expression; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public function isValid(): bool { |
|
57
|
|
|
if(!$this->hasValue) { |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
if($this->validator !== null) { |
|
61
|
|
|
$result = call_user_func($this->validator, $this->value); |
|
62
|
|
|
call_user_func($this->validationResultHandler, $result, [ |
|
63
|
|
|
'value' => $this->value, |
|
64
|
|
|
'key' => implode('.', $this->keyPath), |
|
65
|
|
|
]); |
|
66
|
|
|
return $result; |
|
67
|
|
|
} |
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return array{mixed} |
|
|
|
|
|
|
73
|
|
|
*/ |
|
74
|
|
|
public function getValue(): array { |
|
75
|
|
|
return [$this->value]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string|string[] $keyPath |
|
80
|
|
|
* @return string[] |
|
81
|
|
|
*/ |
|
82
|
|
|
private function buildKey($keyPath): array { |
|
83
|
|
|
if(is_string($keyPath)) { |
|
84
|
|
|
$keyPath = explode('.', $keyPath); |
|
85
|
|
|
} |
|
86
|
|
|
if(!is_array($keyPath)) { |
|
|
|
|
|
|
87
|
|
|
throw new RuntimeException('Invalid key'); |
|
88
|
|
|
} |
|
89
|
|
|
return $keyPath; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|