RequiredDBFilterMap::getMap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace Kir\MySQL\Builder\Expr;
3
4
use Kir\MySQL\Builder\Helpers\RecursiveStructureAccess;
5
6
class RequiredDBFilterMap {
7
	/** @var array<string, mixed> */
8
	private $map;
9
10
	/**
11
	 * @param array<string, mixed> $map
12
	 */
13
	final public function __construct(array $map) {
14
		$this->map = $map;
15
	}
16
17
	/**
18
	 * @param array<string, mixed> $map
19
	 * @return RequiredDBFilterMap
20
	 */
21
	public static function from(array $map) {
22
		return new static($map);
23
	}
24
25
	/**
26
	 * @return array<string, mixed>
27
	 */
28
	protected function getMap(): array {
29
		return $this->map;
30
	}
31
32
	/**
33
	 * @param string $expression
34
	 * @param string|string[] $keyPath
35
	 * @param null|callable(mixed): bool $validator
36
	 * @return DBExprFilter
37
	 */
38
	public function __invoke(string $expression, $keyPath, $validator = null) {
39
		if(!RecursiveStructureAccess::recursiveHas($this->map, $keyPath)) {
40
			throw new RequiredValueNotFoundException(sprintf("Required value %s not found", json_encode($keyPath, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)));
41
		}
42
		return new DBExprFilter($expression, $this->map, $keyPath, $validator);
43
	}
44
}
45