Completed
Push — master ( 38deb0...7544ac )
by Anton
11s
created

ValidatorChain   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 82.86%

Importance

Changes 0
Metric Value
dl 0
loc 149
ccs 29
cts 35
cp 0.8286
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 5 1
A addRule() 0 5 1
A validate() 0 11 3
A assert() 0 6 2
A isRequired() 0 4 1
A getError() 0 7 2
A setError() 0 5 1
A getDescription() 0 8 2
A setDescription() 0 5 1
A __toString() 0 4 1
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Validator;
12
13
use Bluz\Validator\Exception\ValidatorException;
14
use Bluz\Validator\Rule\RuleInterface;
15
16
/**
17
 * Chain of Validators
18
 *
19
 * Chain can consists one or more validation rules
20
 *
21
 * @package  Bluz\Validator
22
 * @author   Anton Shevchuk
23
 *
24
 * @method ValidatorChain alpha($additionalCharacters = '')
25
 * @method ValidatorChain alphaNumeric($additionalCharacters = '')
26
 * @method ValidatorChain array($callback)
27
 * @method ValidatorChain between($min, $max)
28
 * @method ValidatorChain betweenInclusive($min, $max)
29
 * @method ValidatorChain callback($callback)
30
 * @method ValidatorChain condition($condition)
31
 * @method ValidatorChain contains($containsValue)
32
 * @method ValidatorChain containsStrict($containsValue)
33
 * @method ValidatorChain countryCode()
34
 * @method ValidatorChain creditCard()
35
 * @method ValidatorChain date($format)
36
 * @method ValidatorChain domain($checkDns = false)
37
 * @method ValidatorChain email($checkDns = false)
38
 * @method ValidatorChain equals($compareTo)
39
 * @method ValidatorChain equalsStrict($compareTo)
40
 * @method ValidatorChain float()
41
 * @method ValidatorChain in($haystack)
42
 * @method ValidatorChain inStrict($haystack)
43
 * @method ValidatorChain integer()
44
 * @method ValidatorChain ip($options = null)
45
 * @method ValidatorChain json()
46
 * @method ValidatorChain latin($additionalCharacters = '')
47
 * @method ValidatorChain latinNumeric($additionalCharacters = '')
48
 * @method ValidatorChain length($min = null, $max = null)
49
 * @method ValidatorChain less($maxValue)
50
 * @method ValidatorChain lessOrEqual($maxValue)
51
 * @method ValidatorChain more($minValue)
52
 * @method ValidatorChain moreOrEqual($minValue)
53
 * @method ValidatorChain notEmpty()
54
 * @method ValidatorChain noWhitespace()
55
 * @method ValidatorChain numeric()
56
 * @method ValidatorChain required()
57
 * @method ValidatorChain regexp($expression)
58
 * @method ValidatorChain slug()
59
 * @method ValidatorChain string()
60
 */
61
class ValidatorChain
62
{
63
    /**
64
     * @var string description of rules chain
65
     */
66
    protected $description;
67
68
    /**
69
     * @var RuleInterface[] list of validation rules
70
     */
71
    protected $rules = [];
72
73
    /**
74
     * @var string Error message
75
     */
76
    protected $error;
77
78
    /**
79
     * Magic call for create new rule
80
     *
81
     * @param  string $ruleName
82
     * @param  array  $arguments
83
     *
84
     * @return ValidatorChain
85
     * @throws Exception\ComponentException
86
     */
87 11
    public function __call($ruleName, $arguments) : ValidatorChain
88
    {
89 11
        $this->addRule(Validator::rule($ruleName, $arguments));
90 11
        return $this;
91
    }
92
93
    /**
94
     * addRule
95
     *
96
     * @param RuleInterface $rule
97
     *
98
     * @return ValidatorChain
99
     */
100 13
    public function addRule($rule) : ValidatorChain
101
    {
102 13
        $this->rules[] = $rule;
103 13
        return $this;
104
    }
105
106
    /**
107
     * Validate chain of rules
108
     *
109
     * @param mixed $input
110
     *
111
     * @return bool
112
     */
113 11
    public function validate($input) : bool
114
    {
115 11
        $this->error = null; // clean
116 11
        foreach ($this->rules as $rule) {
117 11
            if (!$rule->validate($input)) {
118 8
                $this->error = $rule->getDescription();
119 11
                return false;
120
            }
121
        }
122 4
        return true;
123
    }
124
125
    /**
126
     * Assert
127
     *
128
     * @param  mixed $input
129
     *
130
     * @throws ValidatorException
131
     */
132 3
    public function assert($input)
133
    {
134 3
        if (!$this->validate($input)) {
135 3
            throw new ValidatorException($this->getError());
136
        }
137
    }
138
139
    /**
140
     * Get required flag
141
     *
142
     * @return bool
143
     */
144
    public function isRequired() : bool
145
    {
146
        return array_key_exists('required', $this->rules);
147
    }
148
149
    /**
150
     * Get error message
151
     *
152
     * @return null|string
153
     */
154 7
    public function getError()
155
    {
156 7
        if ($this->description) {
157 3
            return $this->description;
158
        }
159 4
        return $this->error;
160
    }
161
162
    /**
163
     * Set error message for replace text from rule
164
     *
165
     * @param  string $message
166
     *
167
     * @return ValidatorChain
168
     */
169
    protected function setError($message) : ValidatorChain
170
    {
171
        $this->error = $message;
172
        return $this;
173
    }
174
175
    /**
176
     * Get validation description
177
     *
178
     * @return array
179
     */
180 3
    public function getDescription() : array
181
    {
182 3
        if ($this->description) {
183 2
            return [$this->description];
184
        }
185
186 2
        return array_map('strval', $this->rules);
187
    }
188
189
    /**
190
     * Set validation description
191
     *
192
     * @param  string $message
193
     *
194
     * @return ValidatorChain
195
     */
196 4
    public function setDescription($message) : ValidatorChain
197
    {
198 4
        $this->description = $message;
199 4
        return $this;
200
    }
201
202
    /**
203
     * @inheritdoc
204
     */
205 1
    public function __toString()
206
    {
207 1
        return implode("\n", $this->getDescription());
208
    }
209
}
210