Passed
Push — master ( e1084c...1b59a4 )
by Fabien
02:20
created

BaseValidator::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 2
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
    public function getKey(): string
38
    {
39
        return $this->key;
40
    }
41
42
    /**
43
     * @param EditableConfig $config The configuration object.
44
     * @param array<mixed> $configuration The array containing the configuration values.
45
     */
46
    public function validate(EditableConfig $config, array $configuration): void
47
    {
48
        if (!\array_key_exists($this->key, $configuration)) {
49
            return;
50
        }
51
52
        $value = $configuration[$this->key];
53
54
        $this->validateValue($config, $value);
55
    }
56
}
57