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
Push — master ( 6f5c62...0512ff )
by Henrique
04:56
created

Validator::check()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 7
nc 3
nop 1
ccs 0
cts 6
cp 0
crap 20
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()
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 arrayVal()
30
 * @method static Validator arrayType()
31
 * @method static Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true)
32
 * @method static Validator bank(string $countryCode)
33
 * @method static Validator bankAccount(string $countryCode)
34
 * @method static Validator base()
35
 * @method static Validator between(mixed $min = null, mixed $max = null, bool $inclusive = true)
36
 * @method static Validator bic(string $countryCode)
37
 * @method static Validator boolType()
38
 * @method static Validator boolVal()
39
 * @method static Validator bsn()
40
 * @method static Validator call()
41
 * @method static Validator callableType()
42
 * @method static Validator callback(mixed $callback)
43
 * @method static Validator charset(mixed $charset)
44
 * @method static Validator cnh()
45
 * @method static Validator cnpj()
46
 * @method static Validator consonant(string $additionalChars = null)
47
 * @method static Validator contains(mixed $containsValue, bool $identical = false)
48
 * @method static Validator countable()
49
 * @method static Validator countryCode()
50
 * @method static Validator currencyCode()
51
 * @method static Validator cpf()
52
 * @method static Validator creditCard(string $brand = null)
53
 * @method static Validator date(string $format = null)
54
 * @method static Validator digit(string $additionalChars = null)
55
 * @method static Validator directory()
56
 * @method static Validator domain(bool $tldCheck = true)
57
 * @method static Validator each(Validatable $itemValidator = null, Validatable $keyValidator = null)
58
 * @method static Validator email()
59
 * @method static Validator endsWith(mixed $endValue, bool $identical = false)
60
 * @method static Validator equals(mixed $compareTo)
61
 * @method static Validator even()
62
 * @method static Validator executable()
63
 * @method static Validator exists()
64
 * @method static Validator extension(string $extension)
65
 * @method static Validator factor(int $dividend)
66
 * @method static Validator falseVal()
67
 * @method static Validator fibonacci()
68
 * @method static Validator file()
69
 * @method static Validator filterVar(int $filter, mixed $options = null)
70
 * @method static Validator finite()
71
 * @method static Validator floatVal()
72
 * @method static Validator floatType()
73
 * @method static Validator graph(string $additionalChars = null)
74
 * @method static Validator hexRgbColor()
75
 * @method static Validator identical(mixed $value)
76
 * @method static Validator identityCard(string $countryCode)
77
 * @method static Validator image(finfo $fileInfo = null)
78
 * @method static Validator imei()
79
 * @method static Validator in(mixed $haystack, bool $compareIdentical = false)
80
 * @method static Validator infinite()
81
 * @method static Validator instance(string $instanceName)
82
 * @method static Validator intVal()
83
 * @method static Validator intType()
84
 * @method static Validator ip(mixed $ipOptions = null)
85
 * @method static Validator iterableType()
86
 * @method static Validator json()
87
 * @method static Validator key(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
88
 * @method static Validator keyNested(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
89
 * @method static Validator keySet(Key $rule...)
90
 * @method static Validator keyValue(string $comparedKey, string $ruleName, string $baseKey)
91
 * @method static Validator languageCode(string $set)
92
 * @method static Validator leapDate(string $format)
93
 * @method static Validator leapYear()
94
 * @method static Validator length(int $min = null, int $max = null, bool $inclusive = true)
95
 * @method static Validator lowercase()
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)
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()
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 numeric()
113
 * @method static Validator objectType()
114
 * @method static Validator odd()
115
 * @method static Validator oneOf()
116
 * @method static Validator optional(Validatable $rule)
117
 * @method static Validator perfectSquare()
118
 * @method static Validator pesel()
119
 * @method static Validator phone()
120
 * @method static Validator phpLabel()
121
 * @method static Validator positive()
122
 * @method static Validator postalCode(string $countryCode)
123
 * @method static Validator primeNumber()
124
 * @method static Validator prnt(string $additionalChars = null)
125
 * @method static Validator punct(string $additionalChars = null)
126
 * @method static Validator readable()
127
 * @method static Validator regex(string $regex)
128
 * @method static Validator resourceType()
129
 * @method static Validator roman()
130
 * @method static Validator scalarVal()
131
 * @method static Validator sf(string $name, array $params = null)
132
 * @method static Validator size(string $minSize = null, string $maxSize = null)
133
 * @method static Validator slug()
134
 * @method static Validator space(string $additionalChars = null)
135
 * @method static Validator startsWith(mixed $startValue, bool $identical = false)
136
 * @method static Validator stringType()
137
 * @method static Validator subdivisionCode(string $countryCode)
138
 * @method static Validator symbolicLink()
139
 * @method static Validator tld()
140
 * @method static Validator trueVal()
141
 * @method static Validator type(string $type)
142
 * @method static Validator uploaded()
143
 * @method static Validator uppercase()
144
 * @method static Validator url()
145
 * @method static Validator vatin(string $countryCode)
146
 * @method static Validator version()
147
 * @method static Validator videoUrl(string $service = null)
148
 * @method static Validator vowel()
149
 * @method static Validator when(Validatable $if, Validatable $then, Validatable $when = null)
150
 * @method static Validator writable()
151
 * @method static Validator xdigit(string $additionalChars = null)
152
 * @method static Validator yes($useLocale = false)
153
 * @method static Validator zend(mixed $validator, array $params = null)
154
 */
155
class Validator extends AllOf
156
{
157
    protected static $factory;
158
159
    /**
160
     * @return Factory
161
     */
162 2
    protected static function getFactory()
163
    {
164 2
        if (!static::$factory instanceof Factory) {
165
            static::$factory = new Factory();
166
        }
167
168 2
        return static::$factory;
169
    }
170
171
    /**
172
     * @param Factory $factory
173
     */
174
    public static function setFactory($factory)
175
    {
176
        static::$factory = $factory;
177
    }
178
179
    /**
180
     * @param string $rulePrefix
181
     * @param bool   $prepend
182
     */
183
    public static function with($rulePrefix, $prepend = false)
184
    {
185
        if (false === $prepend) {
186
            self::getFactory()->appendRulePrefix($rulePrefix);
187
        } else {
188
            self::getFactory()->prependRulePrefix($rulePrefix);
189
        }
190
    }
191
192
    public function check($input)
193
    {
194
        try {
195
            return parent::check($input);
196
        } catch (ValidationException $exception) {
197
            if (count($this->getRules()) == 1 && $this->template) {
198
                $exception->setTemplate($this->template);
199
            }
200
201
            throw $exception;
202
        }
203
    }
204
205
    /**
206
     * @param string $ruleName
207
     * @param array  $arguments
208
     *
209
     * @return Validator
210
     */
211 1
    public static function __callStatic($ruleName, $arguments)
212
    {
213 1
        if ('allOf' === $ruleName) {
214
            return static::buildRule($ruleName, $arguments);
215
        }
216
217 1
        $validator = new static();
218
219 1
        return $validator->__call($ruleName, $arguments);
220
    }
221
222
    /**
223
     * @param mixed $ruleSpec
224
     * @param array $arguments
225
     *
226
     * @return Validatable
227
     */
228 2
    public static function buildRule($ruleSpec, $arguments = [])
229
    {
230
        try {
231 2
            return static::getFactory()->rule($ruleSpec, $arguments);
232 1
        } catch (\Exception $exception) {
233 1
            throw new ComponentException($exception->getMessage(), $exception->getCode(), $exception);
234
        }
235
    }
236
237
    /**
238
     * @param string $method
239
     * @param array  $arguments
240
     *
241
     * @return self
242
     */
243 2
    public function __call($method, $arguments)
244
    {
245 2
        return $this->addRule(static::buildRule($method, $arguments));
246
    }
247
248
    protected function createException()
249
    {
250
        return new AllOfException();
251
    }
252
253
    /**
254
     * Create instance validator.
255
     *
256
     * @return Validator
257
     */
258 1
    public static function create()
259
    {
260 1
        $ref = new ReflectionClass(__CLASS__);
261
262 1
        return $ref->newInstanceArgs(func_get_args());
263
    }
264
}
265