DBExprFilter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 12
eloc 31
c 2
b 1
f 0
dl 0
loc 83
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 4
A getValue() 0 2 1
A getExpression() 0 2 1
A buildKey() 0 8 3
A isValid() 0 13 3
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}
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...
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)) {
0 ignored issues
show
introduced by
The condition is_array($keyPath) is always true.
Loading history...
87
			throw new RuntimeException('Invalid key');
88
		}
89
		return $keyPath;
90
	}
91
}
92