Completed
Push — master ( be7319...157c74 )
by Matt
01:48
created

src/Constraints/Type.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace League\JsonGuard\Constraints;
4
5
use League\JsonGuard;
6
use League\JsonGuard\ErrorCode;
7
use League\JsonGuard\ValidationError;
8
9
class Type implements PropertyConstraint
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14 78
    public static function validate($value, $type, $pointer = null)
15
    {
16 78
        if (is_array($type)) {
17 8
            return self::anyType($value, $type, $pointer);
18
        }
19
20
        switch ($type) {
21 78
            case 'object':
22 24
                return self::validateType($value, $type, 'is_object', ErrorCode::INVALID_OBJECT, $pointer);
23 74
            case 'array':
24 14
                return self::validateType($value, $type, 'is_array', ErrorCode::INVALID_ARRAY, $pointer);
25 70
            case 'boolean':
26 16
                return self::validateType($value, $type, 'is_bool', ErrorCode::INVALID_BOOLEAN, $pointer);
27 66
            case 'null':
28 8
                return self::validateType($value, $type, 'is_null', ErrorCode::INVALID_NULL, $pointer);
29 66
            case 'number':
30 10
                return self::validateType(
31 64
                    $value,
32 60
                    $type,
33 60
                    'League\JsonGuard\is_json_number',
34 60
                    ErrorCode::INVALID_NUMERIC,
35 60
                    $pointer
36 60
                );
37
            case 'integer':
38 60
                return self::validateType(
39 44
                    $value,
40 44
                    $type,
41 44
                    'League\JsonGuard\is_json_integer',
42 44
                    ErrorCode::INVALID_INTEGER,
43 44
                    $pointer
44 44
                );
45
            case 'string':
46
                return self::validateType(
47
                    $value,
48 40
                    $type,
49 38
                    function ($value) {
50
                        if (is_string($value)) {
51 2
                            // Make sure the string isn't actually a number that was too large
52
                            // to be cast to an int on this platform.  This will only happen if
53 32
                            // you decode JSON with the JSON_BIGINT_AS_STRING option.
54 44
                            if (!(ctype_digit($value) && JsonGuard\compare($value, PHP_INT_MAX) === 1)) {
55 44
                                return true;
56
                            }
57 44
                        }
58
59
                        return false;
60
                    },
61
                    ErrorCode::INVALID_STRING,
62
                    $pointer
63
                );
64
        }
65
    }
66
67
    /**
68
     * @param mixed    $value
69
     * @param string   $type
70 78
     * @param callable $callable
71
     * @param int      $errorCode
72 78
     * @param string   $pointer
73 76
     *
74
     * @return \League\JsonGuard\ValidationError|null
75
     */
76 66 View Code Duplication
    private static function validateType($value, $type, callable $callable, $errorCode, $pointer)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78 66
        if (call_user_func($callable, $value) === true) {
79
            return null;
80
        }
81
82
        return new ValidationError(
83
            'Value {value} is not a(n) {type}',
84
            $errorCode,
85
            $value,
86
            $pointer,
87
            ['value' => $value, 'type' => $type]
88 8
        );
89
    }
90 8
91 8
    /**
92 8
     * @param mixed  $value
93 8
     * @param array  $choices
94
     * @param string $pointer
95 8
     *
96
     * @return \League\JsonGuard\ValidationError|null
97 8
     */
98 8
    private static function anyType($value, array $choices, $pointer)
99 8
    {
100 8
        foreach ($choices as $type) {
101 8
            $error = static::validate($value, $type, $pointer);
102
            if (is_null($error)) {
103 8
                return null;
104
            }
105
        }
106
107
        return new ValidationError(
108
            'Value {value} is not one of: {choices}',
109
            ErrorCode::INVALID_TYPE,
110
            $value,
111
            $pointer,
112
            [
113
                'value'   => $value,
114
                'choices' => $choices
115
            ]
116
        );
117
    }
118
}
119