RequiredDBFilterMap   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 8
c 1
b 0
f 0
dl 0
loc 37
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A from() 0 2 1
A __invoke() 0 5 2
A getMap() 0 2 1
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