Completed
Push — master ( 3bfafa...8acc04 )
by Laurent
03:05
created

AbstractValidator::getWarningMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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 string[]|array
22
     */
23
    private $warningMessages;
24
25
    /**
26
     * @var boolean
27
     */
28
    protected $valid;
29
30
    /**
31
     * @var DoliDB
32
     */
33
    protected $db;
34
35
    /**
36
     * AbstractValidator constructor.
37
     *
38
     * @param Translate $langs
39
     * @param DoliDB    $db
40
     */
41
    public function __construct(Translate $langs, DoliDB $db)
42
    {
43
        $this->langs = $langs;
44
        $this->db = $db;
45
        $this->errors = [];
46
        $this->warningMessages = [];
47
    }
48
49
    /**
50
     * @param string $field
51
     * @param string $message
52
     */
53
    protected function addError($field, $message)
54
    {
55
        if (!isset($this->errors[$field])) {
56
            $this->errors[$field] = [];
57
        }
58
59
        $this->errors[$field][] = $message;
60
        $this->valid = false;
61
    }
62
63
    /**
64
     * @param string $message
65
     *
66
     * @return AbstractValidator
67
     */
68
    protected function addWarning($message)
69
    {
70
        $this->warningMessages[] = $message;
71
        return $this;
72
    }
73
74
    /**
75
     * @return array|string[]
76
     */
77
    public function getWarningMessages()
78
    {
79
        return $this->warningMessages;
80
    }
81
82
    /**
83
     * @return array|string[]
84
     */
85
    public function getErrors()
86
    {
87
        $errors = [];
88
        foreach ($this->errors as $currentField) {
89
            foreach ($currentField as $fieldError) {
90
                $errors[] = $fieldError;
91
            }
92
        }
93
94
        return $errors;
95
    }
96
97
    /**
98
     * @param string $field
99
     *
100
     * @return bool
101
     */
102
    public function hasError($field)
103
    {
104
        return isset($this->errors[$field]) && !empty($field);
105
    }
106
107
}