Completed
Push — master ( 73d790...a6f521 )
by Ron
02:28
created

DBExprFilter::getExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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) {};
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
		}
39
		$this->validator = $validator;
40
		$this->validationResultHandler = $validationResultHandler;
0 ignored issues
show
Documentation Bug introduced by
It seems like $validationResultHandler of type callable is incompatible with the declared type null of property $validationResultHandler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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