RequiredDBFilterMap   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

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

4 Methods

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