BaseValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Churn\Configuration\Validator;
6
7
use Churn\Configuration\EditableConfig;
8
use Churn\Configuration\Validator;
9
10
/**
11
 * @internal
12
 */
13
abstract class BaseValidator implements Validator
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $key;
19
20
    /**
21
     * @param EditableConfig $config The configuration object.
22
     * @param mixed $value The value to validate.
23
     */
24
    abstract protected function validateValue(EditableConfig $config, $value): void;
25
26
    /**
27
     * @param string $key The key of the configuration to validate.
28
     */
29
    public function __construct(string $key)
30
    {
31
        $this->key = $key;
32
    }
33
34
    /**
35
     * Returns the configuration key.
36
     */
37
    #[\Override]
38
    public function getKey(): string
39
    {
40
        return $this->key;
41
    }
42
43
    /**
44
     * @param EditableConfig $config The configuration object.
45
     * @param array<mixed> $configuration The array containing the configuration values.
46
     */
47
    #[\Override]
48
    public function validate(EditableConfig $config, array $configuration): void
49
    {
50
        if (!\array_key_exists($this->key, $configuration)) {
51
            return;
52
        }
53
54
        /** @var mixed $value */
55
        $value = $configuration[$this->key];
56
57
        $this->validateValue($config, $value);
58
    }
59
}
60