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

BaseValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 1
b 0
f 0
dl 0
loc 42
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 9 2
A __construct() 0 3 1
A getKey() 0 3 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
    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