Completed
Push — master ( e24db7...5dd27b )
by Matt
02:58
created

src/Constraints/Format.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 Format implements PropertyConstraint
10
{
11
    // @codingStandardsIgnoreStart
12
    const DATE_TIME_PATTERN = '/^([0-9]{4})-([0-9]{2})-([0-9]{2})([Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})(\\.[0-9]+)?)?(([Zz]|([+-])([0-9]{2}):([0-9]{2})))?/';
13
    // @codingStandardsIgnoreEnd
14
15
    const HOST_NAME_PATTERN = '/^[_a-z]+\.([_a-z]+\.?)+$/i';
16
17
    /**
18
     * {@inheritdoc}
19
     */
20 2
    public static function validate($value, $parameter, $pointer = null)
21
    {
22
        switch ($parameter) {
23 2
            case 'date-time':
24 2
                return self::validateRegex(
25 2
                    $parameter,
26 2
                    $value,
27 2
                    self::DATE_TIME_PATTERN,
28 2
                    ErrorCode::INVALID_DATE_TIME,
29
                    $pointer
30 2
                );
31 2
            case 'uri':
32 2
                return self::validateFilter(
33 2
                    $parameter,
34 2
                    $value,
35 2
                    FILTER_VALIDATE_URL,
36 2
                    null,
37 2
                    ErrorCode::INVALID_URI,
38
                    $pointer
39 2
                );
40 2
            case 'email':
41 2
                return self::validateFilter(
42 2
                    $parameter,
43 2
                    $value,
44 2
                    FILTER_VALIDATE_EMAIL,
45 2
                    null,
46 2
                    ErrorCode::INVALID_EMAIL,
47
                    $pointer
48 2
                );
49 2
            case 'ipv4':
50 2
                return self::validateFilter(
51 2
                    $parameter,
52 2
                    $value,
53 2
                    FILTER_VALIDATE_IP,
54 2
                    FILTER_FLAG_IPV4,
55 2
                    ErrorCode::INVALID_IPV4,
56
                    $pointer
57 2
                );
58 2
            case 'ipv6':
59 2
                return self::validateFilter(
60 2
                    $parameter,
61 2
                    $value,
62 2
                    FILTER_VALIDATE_IP,
63 2
                    FILTER_FLAG_IPV6,
64 2
                    ErrorCode::INVALID_IPV6,
65
                    $pointer
66 2
                );
67 2
            case 'hostname':
68 2
                return self::validateRegex(
69 2
                    $parameter,
70 2
                    $value,
71 2
                    self::HOST_NAME_PATTERN,
72 2
                    ErrorCode::INVALID_HOST_NAME,
73
                    $pointer
74 2
                );
75
        }
76
    }
77
78
    /**
79
     * @param string $format
80
     * @param mixed $value
81
     * @param string $pattern
82
     * @param int $errorCode
83
     * @param string $pointer
84
     *
85
     * @return \League\JsonGuard\ValidationError|null
86
     */
87 2 View Code Duplication
    private static function validateRegex($format, $value, $pattern, $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...
88
    {
89 2
        if (!is_string($value) || preg_match($pattern, $value) === 1) {
90 2
            return null;
91
        }
92
93 2
        return new ValidationError(
94
            'Value {value} does not match the format {format}',
95
            $errorCode,
96
            $value,
97
            $pointer,
98
            ['value' => $value, 'format' => $format]
99
        );
100
    }
101
102
    /**
103
     * @param string $format
104
     * @param mixed  $value
105
     * @param int    $filter
106 2
     * @param mixed  $options
107
     * @param int    $errorCode
108 2
     * @param string $pointer
109 2
     *
110
     * @return \League\JsonGuard\ValidationError|null
111
     */
112
    private static function validateFilter($format, $value, $filter, $options, $errorCode, $pointer)
113
    {
114 2
        if (!is_string($value) || filter_var($value, $filter, $options) !== false) {
115 2
            return null;
116 2
        }
117
118
        // This workaround allows otherwise valid protocol relative urls to pass.
119
        // @see https://bugs.php.net/bug.php?id=72301
120 2
        if ($filter === FILTER_VALIDATE_URL && is_string($value) && strpos($value, '//') === 0) {
121
            if (filter_var('http:' . $value, $filter, $options) !== false) {
122
                return null;
123
            }
124
        }
125
126
        return new ValidationError(
127
            'Value {value} does not match the format {format}',
128
            $errorCode,
129 2
            $value,
130
            $pointer,
131 2
            ['value' => $value, 'format' => $format]
132
        );
133
    }
134
}
135