StaticValidator::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * This file is part of slick/validator package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Validator;
11
12
use Slick\Validator\Exception\InvalidArgumentException;
13
14
/**
15
 * Static Validator
16
 *
17
 * @package Slick\Validator
18
 * @author  Filipe Silva <[email protected]>
19
 */
20
final class StaticValidator
21
{
22
23
    /**
24
     * @var array List of available validators
25
     */
26
    public static $validators = [
27
        'notEmpty'     => 'Slick\Validator\NotEmpty',
28
        'email'        => 'Slick\Validator\Email',
29
        'alphaNumeric' => 'Slick\Validator\AlphaNumeric',
30
        'number'       => 'Slick\Validator\Number',
31
        'url'          => 'Slick\Validator\Url'
32
    ];
33
34
    /**
35
     * @var array The error messages from last validation
36
     */
37
    protected static $messages = [];
38
39
    /**
40
     * @var string
41
     */
42
    protected static $message = '';
43
44
    /**
45
     * Returns true if and only if $value meets the validation requirements
46
     *
47
     * @param string $validator The validator name
48
     * @param mixed $value
49
     *
50
     * @throws Exception\UnknownValidatorClassException
51
     * @return bool
52
     *
53
     * @deprecated Should use the validates instead
54
     *
55
     * @see Slick\Validator\StaticValidator::$validators
56
     */
57
    public static function isValid($validator, $value)
58
    {
59
        /** @var ValidatorInterface $validator */
60
        $validator = static::create($validator);
0 ignored issues
show
Documentation introduced by
$validator is of type object<Slick\Validator\ValidatorInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
        $result = $validator->validates($value);
62
        static::$messages[] = $validator->getMessage();
63
        return $result;
64
    }
65
66
    /**
67
     * Returns true if and only if $value meets the validation requirements
68
     *
69
     * The context specified can be used in the validation process so that
70
     * the same value can be valid or invalid depending on that data.
71
     *
72
     * @param string $validator
73
     * @param mixed $value
74
     * @param array|mixed $context
75
     *
76
     * @return bool
77
     */
78 2
    public static function validates($validator, $value, $context = [])
79
    {
80
        /** @var ValidatorInterface $validator */
81 2
        $validator = static::create($validator);
0 ignored issues
show
Documentation introduced by
$validator is of type object<Slick\Validator\ValidatorInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82 2
        $result = $validator->validates($value, $context);
83 2
        static::$message = $validator->getMessage();
0 ignored issues
show
Documentation Bug introduced by
It seems like $validator->getMessage() of type array is incompatible with the declared type string of property $message.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
84 2
        return $result;
85
    }
86
87
    /**
88
     * Creates a validator object
89
     *
90
     * @param string $validator The validator class name or alias
91
     *
92
     * @param null $message
93
     * @throws Exception\UnknownValidatorClassException
94
     *
95
     * @return ValidatorInterface
96
     *
97
     */
98 22
    public static function create($validator, $message = null)
99
    {
100 22
        $class = self::checkValidator($validator);
101
102
        /** @var ValidatorInterface $object */
103 18
        $object = new $class;
104 18
        if (!is_null($message)) {
105 2
            $object->setMessage($message);
106 2
        }
107 18
        return $object;
108
    }
109
110
    /**
111
     * Returns an array of messages that explain why the most recent
112
     * isValid() call returned false. The array keys are validation failure
113
     * message identifiers, and the array values are the corresponding
114
     * human-readable message strings.
115
     *
116
     * @deprecated  You should use instead the StaticValidator::getMessage()
117
     *
118
     * @return array
119
     */
120
    public static function geMessages()
121
    {
122
        return static::$messages;
123
    }
124
125
    /**
126
     * Returns the message that explain why the most recent
127
     * validates() call returned false.
128
     *
129
     * @return array
130
     */
131 2
    public static function geMessage()
132
    {
133 2
        return static::$message;
134
    }
135
136
    /**
137
     * Check if the provided validator is a known alias or a valid validator
138
     * interface class
139
     *
140
     * @param string $validator Alias or FQ class name of the validator
141
     *
142
     * @return string
143
     */
144 22
    private static function checkValidator($validator)
145
    {
146 22
        if (!array_key_exists($validator, self::$validators)) {
147 6
            return self::checkClass($validator);
148
        }
149 16
        return self::$validators[$validator];
150
    }
151
152
    /**
153
     * Check if is a valid validator class
154
     *
155
     * @param string $validator FQ class name of validator
156
     *
157
     * @return string
158
     */
159 6
    private static function checkClass($validator)
160
    {
161 6
        if (! class_exists($validator)) {
162 2
            throw new InvalidArgumentException(
163 2
                "Class {$validator} does not exists."
164 2
            );
165
        }
166
167 4
        if (! is_subclass_of($validator, ValidatorInterface::class)) {
168 2
            throw new Exception\UnknownValidatorClassException(
169 2
                "The validator '{$validator}' is not defined or does not " .
170
                "implements the Slick\\Validator\\ValidatorInterface interface"
171 2
            );
172
        }
173
174 2
        return $validator;
175
    }
176
177
}
178