Passed
Pull Request — master (#360)
by Valentin
04:12
created

AbstractValidator::getErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Validation;
13
14
use Spiral\Validation\Exception\ValidationException;
15
16
abstract class AbstractValidator implements ValidatorInterface
17
{
18
    /** @var array */
19
    private $errors;
20
21
    /** @var array */
22
    private $rules;
23
24
    /** @var mixed */
25
    private $context;
26
27
    /** @var RulesInterface */
28
    private $provider;
29
30
    /**
31
     * @param array          $rules
32
     * @param mixed          $context
33
     * @param RulesInterface $ruleProvider
34
     */
35
    public function __construct(array $rules, $context, RulesInterface $ruleProvider)
36
    {
37
        $this->errors = [];
38
        $this->rules = $rules;
39
        $this->context = $context;
40
        $this->provider = $ruleProvider;
41
    }
42
43
    /**
44
     * Destruct the service.
45
     */
46
    public function __destruct()
47
    {
48
        $this->rules = [];
49
        $this->provider = null;
50
        $this->errors = [];
51
    }
52
53
    public function __clone()
54
    {
55
        $this->errors = [];
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function withContext($context): ValidatorInterface
62
    {
63
        $validator = clone $this;
64
        $validator->context = $context;
65
        $validator->errors = [];
66
67
        return $validator;
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function getContext()
74
    {
75
        return $this->context;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function isValid(): bool
82
    {
83
        return $this->getErrors() === [];
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function getErrors(): array
90
    {
91
        $this->validate();
92
93
        return $this->errors;
94
    }
95
96
    /**
97
     * Check if value has any error associated.
98
     *
99
     * @param string $field
100
     *
101
     * @return bool
102
     */
103
    public function hasError(string $field): bool
104
    {
105
        return isset($this->getErrors()[$field]);
106
    }
107
108
    /**
109
     * Validate data over given rules and context.
110
     *
111
     * @throws ValidationException
112
     */
113
    final protected function validate(): void
114
    {
115
        if ($this->errors !== []) {
116
            // already validated
117
            return;
118
        }
119
120
        $this->errors = [];
121
122
        foreach ($this->rules as $field => $rules) {
123
            $hasValue = $this->hasValue($field);
124
            $value = $this->getValue($field);
125
126
            foreach ($this->provider->getRules($rules) as $rule) {
127
                if (!$hasValue && $rule->ignoreEmpty($value) && !$rule->hasConditions()) {
128
                    continue;
129
                }
130
131
                foreach ($rule->getConditions() as $condition) {
132
                    if (!$condition->isMet($this, $field, $value)) {
133
                        // condition is not met, skipping validation
134
                        continue 2;
135
                    }
136
                }
137
138
                if (!$rule->validate($this, $field, $value)) {
139
                    // got error, jump to next field
140
                    $this->errors[$field] = $rule->getMessage($field, $value);
141
                    break;
142
                }
143
            }
144
        }
145
    }
146
}
147