DBOptionalValue::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Kir\MySQL\Builder\Value;
4
5
use Kir\MySQL\Builder\Helpers\RecursiveStructureAccess;
6
7
class DBOptionalValue implements OptionalValue {
8
	/**
9
	 * @param object|array<string, mixed> $data
10
	 * @param string|string[] $path
11
	 * @param null|callable(): bool $validator
12
	 */
13
	public function __construct(
14
		private object|array $data,
15
		private string|array $path,
16
		private $validator = null
17
	) {}
18
19
	/**
20
	 * @return bool
21
	 */
22
	public function isValid(): bool {
23
		if(!RecursiveStructureAccess::recursiveHas($this->data, $this->path)) {
24
			return false;
25
		}
26
		if($this->validator !== null) {
27
			$value = $this->getValue();
28
			return call_user_func($this->validator, $value);
29
		}
30
		return true;
31
	}
32
33
	/**
34
	 * @return mixed|null
35
	 */
36
	public function getValue() {
37
		return RecursiveStructureAccess::recursiveGet($this->data, $this->path, null);
38
	}
39
}
40