Passed
Push — master ( 112334...e680d4 )
by Ron
03:37
created

DBExprFilter::isValidArray()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 2
nc 1
nop 1
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 mixed */
9
	private $expression;
10
	/** @var bool */
11
	private $hasValue;
12
	/** @var mixed */
13
	private $value;
14
	/** @var string[] */
15
	private $keyPath;
16
	/** @var null|callable(mixed): bool */
17
	private $validator;
18
	/** @var callable(bool, array{key: mixed, value: string}) */
19
	private $validationResultHandler;
20
21
	/**
22
	 * @param string $expression
23
	 * @param array<string, mixed> $data
24
	 * @param string|string[] $keyPath
25
	 * @param callable|null $validator
26
	 * @param callable|null $validationResultHandler
27
	 */
28
	public function __construct(string $expression, array $data, $keyPath, $validator = null, $validationResultHandler = null) {
29
		$this->expression = $expression;
30
		$this->keyPath = $this->buildKey($keyPath);
31
		$this->value = RecursiveStructureAccess::recursiveGet($data, $this->keyPath, null);
32
		$this->hasValue = is_scalar($this->value) ? trim((string) $this->value) !== '' : !empty($this->value);
33
		if($validator === null) {
34
			$validator = function() {
35
				return true;
36
			};
37
		}
38
		$this->validator = $validator;
39
		if($validationResultHandler === null) {
40
			$validationResultHandler = static function () {};
41
		}
42
		$this->validationResultHandler = $validationResultHandler;
43
	}
44
45
	/**
46
	 * @return string
47
	 */
48
	public function getExpression(): string {
49
		return $this->expression;
50
	}
51
52
	/**
53
	 * @return bool
54
	 */
55
	public function isValid(): bool {
56
		if(!$this->hasValue) {
57
			return false;
58
		}
59
		if($this->validator !== null) {
60
			$result = call_user_func($this->validator, $this->value);
61
			call_user_func($this->validationResultHandler, $result, [
62
				'value' => $this->value,
63
				'key' => implode('.', $this->keyPath),
64
			]);
65
			return $result;
66
		}
67
		return true;
68
	}
69
70
	/**
71
	 * @return array{mixed}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{mixed} at position 2 could not be parsed: Expected ':' at position 2, but found 'mixed'.
Loading history...
72
	 */
73
	public function getValue(): array {
74
		return [$this->value];
75
	}
76
77
	/**
78
	 * @param string|string[] $keyPath
79
	 * @return string[]
80
	 */
81
	private function buildKey($keyPath): array {
82
		if(is_string($keyPath)) {
83
			$keyPath = explode('.', $keyPath);
84
		}
85
		if(!is_array($keyPath)) {
0 ignored issues
show
introduced by
The condition is_array($keyPath) is always true.
Loading history...
86
			throw new RuntimeException('Invalid key');
87
		}
88
		return $keyPath;
89
	}
90
}
91