Completed
Push — master ( 9b078b...3e0fd7 )
by Ron
03:10
created

DBExprFilter::isValidArray()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 1
nop 1
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 callable */
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
				if(is_array($data)) {
34
					return $this->isValidArray($data);
35
				}
36
				return (string) $data !== '';
37
			};
38
		}
39
		if($validationResultHandler === null) {
40
			$validationResultHandler = function () {};
41
		}
42
		$this->validator = $validator;
43
		$this->validationResultHandler = $validationResultHandler;
44
	}
45
46
	/**
47
	 * @return mixed
48
	 */
49
	public function getExpression() {
50
		return $this->expression;
51
	}
52
53
	/**
54
	 * @return bool
55
	 */
56
	public function isValid() {
57
		$result = call_user_func($this->validator, $this->value);
58
		call_user_func($this->validationResultHandler, $result, [
59
			'value' => $this->value,
60
			'key' => join('.', $this->keyPath),
61
		]);
62
		return $result;
63
	}
64
65
	/**
66
	 * @return mixed
67
	 */
68
	public function getValue() {
69
		return [$this->value];
70
	}
71
72
	/**
73
	 * @param string|string[] $keyPath
74
	 * @return string
75
	 * @throws Exception
76
	 */
77
	private function buildKey($keyPath) {
78
		if(is_string($keyPath)) {
79
			$keyPath = explode('.', $keyPath);
80
		}
81
		if(!is_array($keyPath)) {
82
			throw new Exception('Invalid key');
83
		}
84
		return $keyPath;
85
	}
86
87
	/**
88
	 * @param array $array
89
	 * @return bool
90
	 */
91
	private function isValidArray(array $array) {
92
		$data = array_filter($array, function ($value) {
93
			if(is_array($value)) {
94
				return $this->isValidArray($value);
95
			}
96
			return (string) $value !== '';
97
		});
98
		return count($data) > 0;
99
	}
100
101
	/**
102
	 * @param array $array
103
	 * @param array $path
104
	 * @param mixed $default
105
	 * @return array
106
	 */
107
	private function recursiveGet($array, $path, $default) {
108
		$count = count($path);
109
		if (!$count) {
110
			return $default;
111
		}
112
		for($idx = 0; $idx < $count; $idx++) {
113
			$part = $path[$idx];
114
			if(!array_key_exists($part, $array)) {
115
				return $default;
116
			}
117
			$array = $array[$part];
118
		}
119
		return $array;
120
	}
121
}
122