Completed
Push — master ( 476779...f1dfa3 )
by Lee
01:28
created

AbstractValidator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 176
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 4 1
A checkMissingOptions() 0 8 3
A reset() 0 9 1
A setError() 0 7 1
A setOptions() 0 6 1
A getOptions() 0 4 1
A getMessage() 0 4 1
A getMessageCode() 0 4 1
A init() 0 3 1
A getStandardValue() 0 4 1
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 14
    public function __construct()
66
    {
67 14
        $this->reset();
68 14
    }
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 2
    public function setOptions($options)
90
    {
91 2
        $this->options = array_merge($this->options, $options);
92
93 2
        return $this;
94
    }
95
96
    /**
97
     * Get options.
98
     *
99
     * @return array
100
     */
101 2
    public function getOptions()
102
    {
103 2
        return $this->options;
104
    }
105
106
    /**
107
     * Check missing options.
108
     *
109
     * @throws MissingOptionException
110
     */
111 12
    public function checkMissingOptions()
112
    {
113 12
        foreach ($this->requiredOptions as $option) {
114 12
            if (!array_key_exists($option, $this->options)) {
115 4
                throw new MissingOptionException('Missing option ' . $option . ' for validator');
116
            }
117
        }
118 9
    }
119
120
    /**
121
     * Reset validator.
122
     *
123
     * @return AbstractValidator
124
     */
125 14
    public function reset()
126
    {
127 14
        $this->init();
128
129 14
        $this->options = $this->defaultOptions;
130 14
        $this->standardValue = null;
131
132 14
        return $this;
133
    }
134
135
    /**
136
     * Set error.
137
     *
138
     * @param string $code
139
     *
140
     * @return AbstractValidator
141
     */
142 5
    protected function setError($code)
143
    {
144 5
        $this->message = $this->messages[$code];
145 5
        $this->messageCode = $code;
146
147 5
        return $this;
148
    }
149
150
    /**
151
     * @inheritdoc
152
     */
153 3
    public function getMessage()
154
    {
155 3
        return $this->message;
156
    }
157
158
    /**
159
     * @inheritdoc
160
     */
161 3
    public function getMessageCode()
162
    {
163 3
        return $this->messageCode;
164
    }
165
166
    /**
167
     * Get standard value
168
     *
169
     * @return mixed
170
     */
171 2
    public function getStandardValue()
172
    {
173 2
        return $this->standardValue;
174
    }
175
176
    /**
177
     * Init method.
178
     */
179 14
    protected function init()
180
    {
181 14
    }
182
}
183