Validator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 65.63%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 110
ccs 21
cts 32
cp 0.6563
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 4 1
A __construct() 0 4 1
A getFields() 0 4 1
A merge() 0 8 2
A addRawField() 0 11 2
A addField() 0 4 1
B validate() 0 24 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Albert221\Validation;
6
7
use InvalidArgumentException;
8
use RuntimeException;
9
use Symfony\Component\PropertyAccess\PropertyAccess;
10
11
class Validator
12
{
13
    /**
14
     * @var RuleValidatorFactory
15
     */
16
    private $ruleValidatorFactory;
17
18
    /**
19
     * @var Field[]
20
     */
21
    private $fields = [];
22
23
    /**
24
     * @return Validator
25
     */
26 2
    public static function build()
27
    {
28 2
        return new self();
29
    }
30
31
    /**
32
     * Validator constructor.
33
     */
34 2
    public function __construct()
35
    {
36 2
        $this->ruleValidatorFactory = new RuleValidatorFactory();
37 2
    }
38
39
    /**
40
     * @return Field[]
41
     */
42
    public function getFields(): array
43
    {
44
        return $this->fields;
45
    }
46
47
    /**
48
     * Merges given Validator fields with already existing ones.
49
     * NOTE: It does not merge single rules in fields but the fields as whole.
50
     *
51
     * @param Validator $validatorBuilder
52
     *
53
     * @return Validator
54
     */
55
    public function merge(Validator $validatorBuilder): self
56
    {
57
        foreach ($validatorBuilder->getFields() as $field) {
58
            $this->addRawField($field);
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param Field $field
66 2
     *
67
     * @return Field
68 2
     */
69
    public function addRawField(Field $field)
70
    {
71
        if (array_key_exists($field->getName(), $this->fields)) {
72
            throw new InvalidArgumentException(sprintf(
73
                'Field with name "%s" already exists.',
74
                $field->getName()
75 2
            ));
76
        }
77
78
        return $this->fields[$field->getName()] = $field;
79
    }
80
81
    /**
82
     * @param string $name
83 2
     *
84
     * @return Field
85 2
     */
86
    public function addField(string $name): Field
87
    {
88
        return $this->addRawField(new Field($name, $this));
89
    }
90
91
    /**
92
     * @param $data
93 2
     *
94
     * @return VerdictList
95 2
     */
96
    public function validate($data): VerdictList
97 2
    {
98 2
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
99
100 2
        $verdicts = [];
101 2
        foreach ($this->fields as $field) {
102 2
            try {
103
                $value = $propertyAccessor->getValue(
104
                    $data,
105
                    is_array($data) ? "[" . $field->getName() . "]" : $field->getName()
106
                );
107
            } catch (RuntimeException $e) {
108 2
                $value = null;
109 2
            }
110
111 2
            foreach ($field->getRules() as $rule) {
112
                $ruleValidator = $this->ruleValidatorFactory->getInstance($rule);
113
114
                $verdicts[] = $ruleValidator->verdict($value, $rule);
115 2
            }
116
        }
117
118
        return new VerdictList($verdicts);
119
    }
120
}
121