Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#893)
by Islam
06:18
created

Validator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 5
dl 0
loc 110
ccs 26
cts 34
cp 0.7647
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setFactory() 0 4 1
A with() 0 8 2
A buildRule() 0 8 2
A __call() 0 4 1
A create() 0 6 1
A getFactory() 0 8 2
A check() 0 12 4
A __callStatic() 0 10 2
A createException() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Respect\Validation;
13
14
use finfo;
15
use ReflectionClass;
16
use Respect\Validation\Exceptions\AllOfException;
17
use Respect\Validation\Exceptions\ComponentException;
18
use Respect\Validation\Exceptions\ValidationException;
19
use Respect\Validation\Rules\AllOf;
20
use Respect\Validation\Rules\Key;
21
22
/**
23
 * @method static Validator age(int $minAge = null, int $maxAge = null)
24
 * @method static Validator allOf(Validatable ...$rule)
25
 * @method static Validator alnum(string $additionalChars = null)
26
 * @method static Validator alpha(string $additionalChars = null)
27
 * @method static Validator alwaysInvalid()
28
 * @method static Validator alwaysValid()
29
 * @method static Validator anyOf()
30
 * @method static Validator arrayType()
31
 * @method static Validator arrayVal()
32
 * @method static Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true)
33
 * @method static Validator base()
34
 * @method static Validator base64()
35
 * @method static Validator between(mixed $min = null, mixed $max = null, bool $inclusive = true)
36
 * @method static Validator boolType()
37
 * @method static Validator boolVal()
38
 * @method static Validator bsn()
39
 * @method static Validator call()
40
 * @method static Validator callableType()
41
 * @method static Validator callback(mixed $callback)
42
 * @method static Validator charset(mixed $charset)
43
 * @method static Validator cnh()
44
 * @method static Validator cnpj()
45
 * @method static Validator consonant(string $additionalChars = null)
46
 * @method static Validator contains(mixed $containsValue, bool $identical = false)
47
 * @method static Validator countable()
48
 * @method static Validator countryCode()
49
 * @method static Validator currencyCode()
50
 * @method static Validator cpf()
51
 * @method static Validator creditCard(string $brand = null)
52
 * @method static Validator dateTime(string $format = null)
53
 * @method static Validator digit(string $additionalChars = null)
54
 * @method static Validator directory()
55
 * @method static Validator domain(bool $tldCheck = true)
56
 * @method static Validator each(Validatable $itemValidator = null, Validatable $keyValidator = null)
57
 * @method static Validator email()
58
 * @method static Validator endsWith(mixed $endValue, bool $identical = false)
59
 * @method static Validator equals(mixed $compareTo)
60
 * @method static Validator even()
61
 * @method static Validator executable()
62
 * @method static Validator exists()
63
 * @method static Validator extension(string $extension)
64
 * @method static Validator factor(int $dividend)
65
 * @method static Validator falseVal()
66
 * @method static Validator fibonacci()
67
 * @method static Validator file()
68
 * @method static Validator filterVar(int $filter, mixed $options = null)
69
 * @method static Validator finite()
70
 * @method static Validator floatVal()
71
 * @method static Validator floatType()
72
 * @method static Validator graph(string $additionalChars = null)
73
 * @method static Validator hexRgbColor()
74
 * @method static Validator identical(mixed $value)
75
 * @method static Validator identityCard(string $countryCode)
76
 * @method static Validator image(finfo $fileInfo = null)
77
 * @method static Validator imei()
78
 * @method static Validator in(mixed $haystack, bool $compareIdentical = false)
79
 * @method static Validator infinite()
80
 * @method static Validator instance(string $instanceName)
81
 * @method static Validator intVal()
82
 * @method static Validator intType()
83
 * @method static Validator ip(mixed $ipOptions = null)
84
 * @method static Validator iterableType()
85
 * @method static Validator json()
86
 * @method static Validator key(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
87
 * @method static Validator keyNested(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
88
 * @method static Validator keySet(Key ...$rule)
89
 * @method static Validator keyValue(string $comparedKey, string $ruleName, string $baseKey)
90
 * @method static Validator languageCode(string $set)
91
 * @method static Validator leapDate(string $format)
92
 * @method static Validator leapYear()
93
 * @method static Validator length(int $min = null, int $max = null, bool $inclusive = true)
94
 * @method static Validator lowercase()
95
 * @method static Validator luhn()
96
 * @method static Validator macAddress()
97
 * @method static Validator max(mixed $maxValue, bool $inclusive = true)
98
 * @method static Validator mimetype(string $mimetype)
99
 * @method static Validator min(mixed $minValue, bool $inclusive = true)
100
 * @method static Validator minimumAge(int $age, bool $format = null)
101
 * @method static Validator multiple(int $multipleOf)
102
 * @method static Validator negative()
103
 * @method static Validator nif()
104
 * @method static Validator no($useLocale = false)
105
 * @method static Validator noneOf(Validatable ...$rule)
106
 * @method static Validator not(Validatable $rule)
107
 * @method static Validator notBlank()
108
 * @method static Validator notEmpty()
109
 * @method static Validator notOptional()
110
 * @method static Validator noWhitespace()
111
 * @method static Validator nullType()
112
 * @method static Validator number()
113
 * @method static Validator numericVal()
114
 * @method static Validator objectType()
115
 * @method static Validator odd()
116
 * @method static Validator oneOf(Validatable ...$rule)
117
 * @method static Validator optional(Validatable $rule)
118
 * @method static Validator perfectSquare()
119
 * @method static Validator pesel()
120
 * @method static Validator phone()
121
 * @method static Validator phpLabel()
122
 * @method static Validator pis()
123
 * @method static Validator positive()
124
 * @method static Validator postalCode(string $countryCode)
125
 * @method static Validator primeNumber()
126
 * @method static Validator prnt(string $additionalChars = null)
127
 * @method static Validator punct(string $additionalChars = null)
128
 * @method static Validator readable()
129
 * @method static Validator regex(string $regex)
130
 * @method static Validator resourceType()
131
 * @method static Validator roman()
132
 * @method static Validator scalarVal()
133
 * @method static Validator sf(string $name, array $params = null)
134
 * @method static Validator size(string $minSize = null, string $maxSize = null)
135
 * @method static Validator slug()
136
 * @method static Validator space(string $additionalChars = null)
137
 * @method static Validator startsWith(mixed $startValue, bool $identical = false)
138
 * @method static Validator stringType()
139
 * @method static Validator stringVal()
140
 * @method static Validator subdivisionCode(string $countryCode)
141
 * @method static Validator symbolicLink()
142
 * @method static Validator tld()
143
 * @method static Validator trueVal()
144
 * @method static Validator type(string $type)
145
 * @method static Validator unique()
146
 * @method static Validator uploaded()
147
 * @method static Validator uppercase()
148
 * @method static Validator url()
149
 * @method static Validator uuid()
150
 * @method static Validator vatin(string $countryCode)
151
 * @method static Validator version()
152
 * @method static Validator videoUrl(string $service = null)
153
 * @method static Validator vowel()
154
 * @method static Validator when(Validatable $if, Validatable $then, Validatable $when = null)
155
 * @method static Validator writable()
156
 * @method static Validator xdigit(string $additionalChars = null)
157
 * @method static Validator yes($useLocale = false)
158
 * @method static Validator zend(mixed $validator, array $params = null)
159
 */
160
class Validator extends AllOf
161
{
162
    protected static $factory;
163
164
    /**
165
     * @return Factory
166
     */
167 290
    protected static function getFactory()
168
    {
169 290
        if (!static::$factory instanceof Factory) {
170 288
            static::$factory = new Factory();
171
        }
172
173 290
        return static::$factory;
174
    }
175
176
    /**
177
     * @param Factory $factory
178
     */
179
    public static function setFactory($factory)
180
    {
181
        static::$factory = $factory;
182
    }
183
184
    /**
185
     * @param string $rulePrefix
186
     * @param bool   $prepend
187
     */
188
    public static function with($rulePrefix, $prepend = false)
189
    {
190
        if (false === $prepend) {
191
            self::getFactory()->appendRulePrefix($rulePrefix);
192
        } else {
193
            self::getFactory()->prependRulePrefix($rulePrefix);
194
        }
195
    }
196
197 177
    public function check($input)
198
    {
199
        try {
200 177
            return parent::check($input);
201 120
        } catch (ValidationException $exception) {
202 120
            if (count($this->getRules()) == 1 && $this->template) {
203 1
                $exception->setTemplate($this->template);
204
            }
205
206 120
            throw $exception;
207
        }
208
    }
209
210
    /**
211
     * @param string $ruleName
212
     * @param array  $arguments
213
     *
214
     * @return Validator
215
     */
216 289
    public static function __callStatic($ruleName, $arguments)
217
    {
218 289
        if ('allOf' === $ruleName) {
219 8
            return static::buildRule($ruleName, $arguments);
220
        }
221
222 289
        $validator = new static();
223
224 289
        return $validator->__call($ruleName, $arguments);
225
    }
226
227
    /**
228
     * @param mixed $ruleSpec
229
     * @param array $arguments
230
     *
231
     * @return Validatable
232
     */
233 290
    public static function buildRule($ruleSpec, $arguments = [])
234
    {
235
        try {
236 290
            return static::getFactory()->rule($ruleSpec, $arguments);
237 1
        } catch (\Exception $exception) {
238 1
            throw new ComponentException($exception->getMessage(), $exception->getCode(), $exception);
239
        }
240
    }
241
242
    /**
243
     * @param string $method
244
     * @param array  $arguments
245
     *
246
     * @return self
247
     */
248 290
    public function __call($method, $arguments)
249
    {
250 290
        return $this->addRule(static::buildRule($method, $arguments));
251
    }
252
253 124
    protected function createException()
254
    {
255 124
        return new AllOfException();
256
    }
257
258
    /**
259
     * Create instance validator.
260
     *
261
     * @return Validator
262
     */
263 11
    public static function create()
264
    {
265 11
        $ref = new ReflectionClass(__CLASS__);
266
267 11
        return $ref->newInstanceArgs(func_get_args());
268
    }
269
}
270