RequiredDBFilterMap   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

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

4 Methods

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