Completed
Push — master ( f1bd87...81d11b )
by Laurent
01:43
created

AbstractValidator::addError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
require_once __DIR__ . '/ValidatorInterface.php';
3
4
/**
5
 * AbstractValidator class
6
 *
7
 * @author Laurent De Coninck <[email protected]>
8
 */
9
abstract class AbstractValidator implements ValidatorInterface
10
{
11
12
    /** @var Translate */
13
    private $langs;
14
15
    /**
16
     * @var string[]|array
17
     */
18
    protected $errors;
19
20
    /**
21
     * @var boolean
22
     */
23
    protected $valid;
24
25
    /**
26
     * AbstractValidator constructor.
27
     *
28
     * @param Translate $langs
29
     */
30
    public function __construct(Translate $langs)
31
    {
32
        $this->langs = $langs;
33
    }
34
35
    /**
36
     * @param string $field
37
     * @param string $message
38
     */
39
    protected function addError($field, $message)
40
    {
41
        if (!isset($this->errors[$field])) {
42
            $this->errors[$field] = [];
43
        }
44
45
        $this->errors[$field][] = $message;
46
        $this->valid = false;
47
    }
48
49
    /**
50
     * @return array|string[]
51
     */
52
    public function getErrors()
53
    {
54
        $errors = [];
55
        foreach ($this->errors as $currentField) {
56
            foreach ($currentField as $fieldError) {
57
                $errors[] = $fieldError;
58
            }
59
        }
60
61
        return $errors;
62
    }
63
64
    /**
65
     * @param string $field
66
     *
67
     * @return bool
68
     */
69
    public function hasError($field)
70
    {
71
        return isset($this->errors[$field]) && !empty($field);
72
    }
73
74
}