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 (#911)
by Henrique
04:59 queued 02:34
created

Validator::__callStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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