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
|
|
|
// regex from http://www.pelagodesign.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/ |
12
|
|
|
// @codingStandardsIgnoreStart |
13
|
|
|
const DATE_TIME_PATTERN = '/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/'; |
14
|
|
|
// @codingStandardsIgnoreEnd |
15
|
|
|
|
16
|
|
|
const HOST_NAME_PATTERN = '/^[_a-z]+\.([_a-z]+\.?)+$/i'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
2 |
|
public static function validate($value, $parameter, $pointer = null) |
22
|
|
|
{ |
23
|
|
|
switch ($parameter) { |
24
|
2 |
|
case 'date-time': |
25
|
2 |
|
return self::validateRegex( |
26
|
2 |
|
$parameter, |
27
|
2 |
|
$value, |
28
|
2 |
|
self::DATE_TIME_PATTERN, |
29
|
2 |
|
ErrorCode::INVALID_DATE_TIME, |
30
|
|
|
$pointer |
31
|
2 |
|
); |
32
|
2 |
|
case 'uri': |
33
|
2 |
|
return self::validateFilter( |
34
|
2 |
|
$parameter, |
35
|
2 |
|
$value, |
36
|
2 |
|
FILTER_VALIDATE_URL, |
37
|
2 |
|
null, |
38
|
2 |
|
ErrorCode::INVALID_URI, |
39
|
|
|
$pointer |
40
|
2 |
|
); |
41
|
2 |
|
case 'email': |
42
|
2 |
|
return self::validateFilter( |
43
|
2 |
|
$parameter, |
44
|
2 |
|
$value, |
45
|
2 |
|
FILTER_VALIDATE_EMAIL, |
46
|
2 |
|
null, |
47
|
2 |
|
ErrorCode::INVALID_EMAIL, |
48
|
|
|
$pointer |
49
|
2 |
|
); |
50
|
2 |
|
case 'ipv4': |
51
|
2 |
|
return self::validateFilter( |
52
|
2 |
|
$parameter, |
53
|
2 |
|
$value, |
54
|
2 |
|
FILTER_VALIDATE_IP, |
55
|
2 |
|
FILTER_FLAG_IPV4, |
56
|
2 |
|
ErrorCode::INVALID_IPV4, |
57
|
|
|
$pointer |
58
|
2 |
|
); |
59
|
2 |
|
case 'ipv6': |
60
|
2 |
|
return self::validateFilter( |
61
|
2 |
|
$parameter, |
62
|
2 |
|
$value, |
63
|
2 |
|
FILTER_VALIDATE_IP, |
64
|
2 |
|
FILTER_FLAG_IPV6, |
65
|
2 |
|
ErrorCode::INVALID_IPV6, |
66
|
|
|
$pointer |
67
|
2 |
|
); |
68
|
2 |
|
case 'hostname': |
69
|
2 |
|
return self::validateRegex( |
70
|
2 |
|
$parameter, |
71
|
2 |
|
$value, |
72
|
2 |
|
self::HOST_NAME_PATTERN, |
73
|
2 |
|
ErrorCode::INVALID_HOST_NAME, |
74
|
|
|
$pointer |
75
|
2 |
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $format |
81
|
|
|
* @param mixed $value |
82
|
|
|
* @param string $pattern |
83
|
|
|
* @param int $errorCode |
84
|
|
|
* @param string $pointer |
85
|
|
|
* |
86
|
|
|
* @return \League\JsonGuard\ValidationError|null |
87
|
|
|
*/ |
88
|
2 |
|
private static function validateRegex($format, $value, $pattern, $errorCode, $pointer) |
89
|
|
|
{ |
90
|
2 |
|
if (preg_match($pattern, $value) === 1) { |
91
|
2 |
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
return new ValidationError(self::invalidFormatMessage($format, $value), $errorCode, $value, $pointer); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $format |
99
|
|
|
* @param mixed $value |
100
|
|
|
* @param int $filter |
101
|
|
|
* @param mixed $options |
102
|
|
|
* @param int $errorCode |
103
|
|
|
* @param string $pointer |
104
|
|
|
* |
105
|
|
|
* @return \League\JsonGuard\ValidationError|null |
106
|
|
|
*/ |
107
|
2 |
|
private static function validateFilter($format, $value, $filter, $options, $errorCode, $pointer) |
108
|
|
|
{ |
109
|
2 |
|
if (filter_var($value, $filter, $options) !== false) { |
110
|
2 |
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
return new ValidationError(self::invalidFormatMessage($format, $value), $errorCode, $value, $pointer); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $format |
118
|
|
|
* @param mixed $value |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
2 |
|
private static function invalidFormatMessage($format, $value) |
123
|
|
|
{ |
124
|
2 |
|
return sprintf('"%s" is not a valid %s.', JsonGuard\asString($value), $format); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|