GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( bd2bc3...a7d08c )
by Rick
8s
created

Chain::validate()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 8.8571
cc 6
eloc 11
nc 10
nop 3
crap 6
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator;
10
11
use Particle\Validator\Output\Structure;
12
use Particle\Validator\Output\Subject;
13
use Particle\Validator\Value\Container;
14
15
/**
16
 * Represents a collection of Rules which may break the chain of validation (but usually don't).
17
 *
18
 * @package Particle\Validator
19
 */
20
class Chain
21
{
22
    /**
23
     * The key we want to validate.
24
     *
25
     * @var string
26
     */
27
    protected $key;
28
29
    /**
30
     * The name that we can use in error messages.
31
     *
32
     * @var string
33
     */
34
    protected $name;
35
36
    /**
37
     * The array of all rules for this chain.
38
     *
39
     * @var Rule[]
40
     */
41
    protected $rules = [];
42
43
    /**
44
     * The message stack to append messages to.
45
     *
46
     * @var MessageStack
47
     */
48
    protected $messageStack;
49
50
    /**
51
     * Construct the chain.
52
     *
53
     * @param string $key
54
     * @param string $name
55
     * @param bool $required
56
     * @param bool $allowEmpty
57
     */
58 188
    public function __construct($key, $name, $required, $allowEmpty)
59
    {
60 188
        $this->key = $key;
61 188
        $this->name = $name;
62
63 188
        $this->addRule(new Rule\Required($required));
64 188
        $this->addRule(new Rule\NotEmpty($allowEmpty));
65 188
    }
66
67
    /**
68
     * Overwrite the default __clone behaviour to make sure the rules are cloned too.
69
     */
70 3
    public function __clone()
71
    {
72 3
        $rules = [];
73 3
        foreach ($this->rules as $rule) {
74 3
            $rules[] = clone $rule;
75 3
        }
76 3
        $this->rules = $rules;
77 3
    }
78
79
    /**
80
     * Validate the value to consist only out of alphanumeric characters.
81
     *
82
     * @param bool $allowWhitespace
83
     * @return $this
84
     */
85 7
    public function alnum($allowWhitespace = false)
86
    {
87 7
        return $this->addRule(new Rule\Alnum($allowWhitespace));
88
    }
89
90
    /**
91
     * Validate that the value only consists our of alphabetic characters.
92
     *
93
     * @param bool $allowWhitespace
94
     * @return $this
95
     */
96 7
    public function alpha($allowWhitespace = false)
97
    {
98 7
        return $this->addRule(new Rule\Alpha($allowWhitespace));
99
    }
100
101
    /**
102
     * Validate that the value is between $min and $max (inclusive).
103
     *
104
     * @param int $min
105
     * @param int $max
106
     * @return $this
107
     */
108 6
    public function between($min, $max)
109
    {
110 6
        return $this->addRule(new Rule\Between($min, $max));
111
    }
112
113
    /**
114
     * Validate that the value is a boolean.
115
     *
116
     * @return $this
117
     */
118 9
    public function bool()
119
    {
120 9
        return $this->addRule(new Rule\Boolean());
121
    }
122
123
    /**
124
     * Validate by executing a callback function, and returning its result.
125
     *
126
     * @param callable $callable
127
     * @return $this
128
     */
129 5
    public function callback(callable $callable)
130
    {
131 5
        return $this->addRule(new Rule\Callback($callable));
132
    }
133
134
    /**
135
     * Validates that the value is a valid credit card number.
136
     * @return $this
137
     */
138 15
    public function creditCard()
139
    {
140 15
        return $this->addRule(new Rule\CreditCard());
141
    }
142
143
    /**
144
     * Validates that the value is a date. If format is passed, it *must* be in that format.
145
     *
146
     * @param string|null $format
147
     * @return $this
148
     */
149 8
    public function datetime($format = null)
150
    {
151 8
        return $this->addRule(new Rule\Datetime($format));
152
    }
153
154
    /**
155
     * Validates that all characters of the value are decimal digits.
156
     *
157
     * @return $this
158
     */
159 4
    public function digits()
160
    {
161 4
        return $this->addRule(new Rule\Digits());
162
    }
163
164
    /**
165
     * Validates a value to be a nested array, which can then be validated using a new Validator instance.
166
     *
167
     * @param callable $callback
168
     * @return $this
169
     */
170 5
    public function each(callable $callback)
171
    {
172 5
        return $this->addRule(new Rule\Each($callback));
173
    }
174
175
    /**
176
     * Validates that the value is a valid email address (format only).
177
     * @return $this
178
     */
179 6
    public function email()
180
    {
181 6
        return $this->addRule(new Rule\Email());
182
    }
183
184
    /**
185
     * Validates that the value is equal to $value.
186
     *
187
     * @param string $value
188
     * @return $this
189
     */
190 2
    public function equals($value)
191
    {
192 2
        return $this->addRule(new Rule\Equal($value));
193
    }
194
195
    /**
196
     * Validates that the value is greater than $value.
197
     *
198
     * @param int $value
199
     * @return $this
200
     */
201 3
    public function greaterThan($value)
202
    {
203 3
        return $this->addRule(new Rule\GreaterThan($value));
204
    }
205
206
    /**
207
     * Validates that the value is in the array with optional "loose" checking.
208
     *
209
     * @param array $array
210
     * @param bool $strict
211
     * @return $this
212
     */
213 4
    public function inArray(array $array, $strict = true)
214
    {
215 4
        return $this->addRule(new Rule\InArray($array, $strict));
216
    }
217
218
    /**
219
     * Validates the value represents a valid integer
220
     *
221
     * @return $this
222
     * @see \Particle\Validator\Rule\Integer
223
     */
224 10
    public function integer()
225
    {
226 10
        return $this->addRule(new Rule\Integer());
227
    }
228
229
    /**
230
     * Validates the value is an array
231
     *
232
     * @return $this
233
     * @see \Particle\Validator\Rule\IsArray
234
     */
235 7
    public function isArray()
236
    {
237 7
        return $this->addRule(new Rule\IsArray());
238
    }
239
240
    /**
241
     * Validates that the value represents a valid JSON string
242
     *
243
     * @return $this
244
     * @see \Particle\Validator\Rule\Json
245
     */
246 11
    public function json()
247
    {
248 11
        return $this->addRule(new Rule\Json());
249
    }
250
251
    /**
252
     * Validate the value to be of precisely length $length.
253
     *
254
     * @param int $length
255
     * @return $this
256
     */
257 13
    public function length($length)
258
    {
259 13
        return $this->addRule(new Rule\Length($length));
260
    }
261
262
    /**
263
     * Validates that the length of the value is between $min and $max.
264
     *
265
     * If $max is null, it has no upper limit. The default is inclusive.
266
     *
267
     * @param int $min
268
     * @param int|null $max
269
     * @return $this
270
     */
271 7
    public function lengthBetween($min, $max)
272
    {
273 7
        return $this->addRule(new Rule\LengthBetween($min, $max));
274
    }
275
276
    /**
277
     * Validates that the value is less than $value.
278
     *
279
     * @param int $value
280
     * @return $this
281
     */
282 3
    public function lessThan($value)
283
    {
284 3
        return $this->addRule(new Rule\LessThan($value));
285
    }
286
287
    /**
288
     * Mount a rule object onto this chain.
289
     *
290
     * @param Rule $rule
291
     * @return $this
292
     */
293 1
    public function mount(Rule $rule)
294
    {
295 1
        return $this->addRule($rule);
296
    }
297
298
    /**
299
     * Validates that the value is either a integer or a float.
300
     *
301
     * @return $this
302
     */
303 11
    public function numeric()
304
    {
305 11
        return $this->addRule(new Rule\Numeric());
306
    }
307
308
    /**
309
     * Validates that the value is a valid phone number for $countryCode.
310
     *
311
     * @param string $countryCode
312
     * @see \Particle\Validator\Rule\Phone
313
     * @return $this
314
     */
315 13
    public function phone($countryCode)
316
    {
317 13
        return $this->addRule(new Rule\Phone($countryCode));
318
    }
319
320
    /**
321
     * Validates that the value matches the regular expression $regex.
322
     *
323
     * @param string $regex
324
     * @return $this
325
     */
326 2
    public function regex($regex)
327
    {
328 2
        return $this->addRule(new Rule\Regex($regex));
329
    }
330
331
    /**
332
     * Validates that the value is a valid URL. The schemes array is to selectively whitelist URL schemes.
333
     *
334
     * @param array $schemes
335
     * @return $this
336
     */
337 7
    public function url(array $schemes = [])
338
    {
339 7
        return $this->addRule(new Rule\Url($schemes));
340
    }
341
342
    /**
343
     * Validates that the value is a valid UUID
344
     *
345
     * @param int $version
346
     * @return $this
347
     */
348 12
    public function uuid($version = Rule\Uuid::UUID_V4)
349
    {
350 12
        return $this->addRule(new Rule\Uuid($version));
351
    }
352
353
    /**
354
     * Set a callable or boolean value which may be used to alter the required requirement on validation time.
355
     *
356
     * This may be incredibly helpful when doing conditional validation.
357
     *
358
     * @param callable|bool $required
359
     * @return $this
360
     */
361 5
    public function required($required)
362
    {
363 5
        $this->getRequiredRule()->setRequired($required);
364 5
        return $this;
365
    }
366
367
    /**
368
     * Set a callable or boolean value which may be used to alter the allow empty requirement on validation time.
369
     *
370
     * This may be incredibly helpful when doing conditional validation.
371
     *
372
     * @param callable|bool $allowEmpty
373
     * @return $this
374
     */
375 11
    public function allowEmpty($allowEmpty)
376
    {
377 11
        $this->getNotEmptyRule()->setAllowEmpty($allowEmpty);
378 11
        return $this;
379
    }
380
381
    /**
382
     * Attach a representation of this Chain to the Output\Structure $structure.
383
     *
384
     * @internal
385
     * @param Structure $structure
386
     * @param MessageStack $messageStack
387
     * @return Structure
388
     */
389 2
    public function output(Structure $structure, MessageStack $messageStack)
390
    {
391 2
        $subject = new Subject($this->key, $this->name);
392
393 2
        foreach ($this->rules as $rule) {
394 2
            $rule->output($subject, $messageStack);
395 2
        }
396
397 2
        $structure->addSubject($subject);
398
399 2
        return $structure;
400
    }
401
402
    /**
403
     * Validates the values in the $values array and appends messages to $messageStack. Returns the result as a bool.
404
     *
405
     * @param MessageStack $messageStack
406
     * @param Container $input
407
     * @param Container $output
408
     * @return bool
409
     */
410 185
    public function validate(MessageStack $messageStack, Container $input, Container $output)
411
    {
412 185
        $valid = true;
413 185
        foreach ($this->rules as $rule) {
414 185
            $rule->setMessageStack($messageStack);
415 185
            $rule->setParameters($this->key, $this->name);
416
417 185
            $valid = $rule->isValid($this->key, $input) && $valid;
418
419 185
            if ($rule->shouldBreakChain()) {
420 17
                break;
421
            }
422 185
        }
423
424 185
        if ($valid && $input->has($this->key)) {
425 98
            $output->set($this->key, $input->get($this->key));
426 98
        }
427 185
        return $valid;
428
    }
429
430
    /**
431
     * Shortcut method for storing a rule on this chain, and returning the chain.
432
     *
433
     * @param Rule $rule
434
     * @return $this
435
     */
436 188
    protected function addRule(Rule $rule)
437
    {
438 188
        $this->rules[] = $rule;
439
440 188
        return $this;
441
    }
442
443
    /**
444
     * Returns the first rule, which is always the required rule.
445
     *
446
     * @return Rule\Required
447
     */
448 5
    protected function getRequiredRule()
449
    {
450 5
        return $this->rules[0];
451
    }
452
453
    /**
454
     * Returns the second rule, which is always the allow empty rule.
455
     *
456
     * @return Rule\NotEmpty
457
     */
458 11
    protected function getNotEmptyRule()
459
    {
460 11
        return $this->rules[1];
461
    }
462
}
463