AbstractValidator::getMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Lavibi\Popoya;
4
5
use Lavibi\Popoya\Exception\MissingOptionException;
6
7
abstract class AbstractValidator implements ValidatorInterface
8
{
9
    /**
10
     * Input value
11
     *
12
     * @var mixed
13
     */
14
    protected $value;
15
16
    /**
17
     * Filter value for valid input value.
18
     *
19
     * @var mixed
20
     */
21
    protected $standardValue;
22
23
    /**
24
     * Error messages
25
     *
26
     * @var array
27
     */
28
    protected $messages = array();
29
30
    /**
31
     * Error message
32
     *
33
     * @var string
34
     */
35
    protected $message;
36
37
    /**
38
     * Error code
39
     *
40
     * @var string
41
     */
42
    protected $messageCode;
43
44
    /**
45
     * Options.
46
     *
47
     * @var array
48
     */
49
    protected $options = [];
50
51
    /**
52
     * Default options.
53
     *
54
     * @var array
55
     */
56
    protected $defaultOptions = [];
57
58
    /**
59
     * Required options.
60
     *
61
     * @var string[]
62
     */
63
    protected $requiredOptions = [];
64
65 49
    public function __construct()
66
    {
67 49
        $this->reset();
68 49
    }
69
70
    /**
71
     * Run validator
72
     *
73
     * @param mixed $value
74
     *
75
     * @return bool
76
     */
77
    public function __invoke($value)
78
    {
79
        return $this->isValid($value);
80
    }
81
82
    /**
83
     * Set options.
84
     *
85
     * @param array $options
86
     *
87
     * @return AbstractValidator
88
     */
89 4
    public function setOptions($options)
90
    {
91 4
        $this->options = array_merge($this->options, $options);
92
93 4
        return $this;
94
    }
95
96
    /**
97
     * Get options.
98
     *
99
     * @return array
100
     */
101 4
    public function getOptions()
102
    {
103 4
        return $this->options;
104
    }
105
106
    /**
107
     * Check missing options.
108
     *
109
     * @throws MissingOptionException
110
     */
111 18
    public function checkMissingOptions()
112
    {
113 18
        foreach ($this->requiredOptions as $option) {
114 18
            if (!array_key_exists($option, $this->options)) {
115 5
                throw new MissingOptionException('Missing option ' . $option . ' for validator');
116
            }
117
        }
118 14
    }
119
120
    /**
121
     * Reset validator.
122
     *
123
     * @return AbstractValidator
124
     */
125 49
    public function reset()
126
    {
127 49
        $this->init();
128
129 49
        $this->options = $this->defaultOptions;
130 49
        $this->standardValue = null;
131
132 49
        return $this;
133
    }
134
135
    /**
136
     * Set error.
137
     *
138
     * @param string $code
139
     *
140
     * @return AbstractValidator
141
     */
142 24
    protected function setError($code)
143
    {
144 24
        $this->message = $this->messages[$code];
145 24
        $this->messageCode = $code;
146
147 24
        return $this;
148
    }
149
150
    /**
151
     * @inheritdoc
152
     */
153 19
    public function getMessage()
154
    {
155 19
        return $this->message;
156
    }
157
158
    /**
159
     * @inheritdoc
160
     */
161 19
    public function getMessageCode()
162
    {
163 19
        return $this->messageCode;
164
    }
165
166
    /**
167
     * Get standard value
168
     *
169
     * @return mixed
170
     */
171 6
    public function getStandardValue()
172
    {
173 6
        return $this->standardValue;
174
    }
175
176
    /**
177
     * Init method.
178
     */
179 49
    protected function init()
180
    {
181 49
    }
182
}
183