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

Passed
Pull Request — master (#1189)
by
unknown
05:46
created

Zend::assert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 17
c 0
b 0
f 0
ccs 8
cts 9
cp 0.8889
rs 9.9666
cc 2
nc 2
nop 1
crap 2.0054
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\Rules;
15
16
use ReflectionClass;
17
use Respect\Validation\Exceptions\ComponentException;
18
use Respect\Validation\Exceptions\ZendException;
19
use function get_object_vars;
20
use function is_object;
21
use function is_string;
22
use function mb_stripos;
23
24
/**
25
 * Use Zend validators inside Respect\Validation flow. Messages are preserved.
26
 *
27
 * @author Alexandre Gomes Gaigalas <[email protected]>
28
 * @author Danilo Correa <[email protected]>
29
 * @author Henrique Moody <[email protected]>
30
 * @author Hugo Hamon <[email protected]>
31
 */
32
final class Zend extends AbstractRule
33
{
34
    /**
35
     * @var mixed
36
     */
37
    private $zendValidator;
38
39 1
    public function __construct($validator, $params = [])
40
    {
41 1
        if (is_object($validator)) {
42
            $this->zendValidator = $validator;
43
            return;
44
        }
45
46 1
        if (!is_string($validator)) {
47
            throw new ComponentException('Invalid Validator Construct');
48
        }
49
50 1
        if (false === mb_stripos($validator, 'Zend')) {
51 1
            $validator = "Zend\\Validator\\{$validator}";
52
        } else {
53
            $validator = "\\{$validator}";
54
        }
55
56 1
        $zendMirror = new ReflectionClass($validator);
57
58 1
        if ($zendMirror->hasMethod('__construct')) {
59 1
            $this->zendValidator = $zendMirror->newInstanceArgs($params);
60
        } else {
61
            $this->zendValidator = $zendMirror->newInstance();
62
        }
63 1
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function assert($input): void
69
    {
70 1
        $validator = clone $this->zendValidator;
71
72 1
        if ($validator->isValid($input)) {
73
            return;
74
        }
75
        
76
        $exceptions = array_map(function ($message) {
77 1
            return $this->reportError($message, get_object_vars($this));
78 1
        }, $validator->getMessages());
79
80
        /** @var ZendException $zendException */
81 1
        $zendException = $this->reportError($input);
82 1
        $zendException->addChildren($exceptions);
83
84 1
        throw $zendException;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 6
    public function validate($input): bool
91
    {
92 6
        $validator = clone $this->zendValidator;
93 6
        return $validator->isValid($input);
94
    }
95
}
96