|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\JsonGuard\Constraint\DraftFour; |
|
4
|
|
|
|
|
5
|
|
|
use League\JsonGuard\Assert; |
|
6
|
|
|
use League\JsonGuard\ConstraintInterface; |
|
7
|
|
|
use League\JsonGuard\ValidationError; |
|
8
|
|
|
use League\JsonGuard\Validator; |
|
9
|
|
|
use function League\JsonGuard\error; |
|
10
|
|
|
|
|
11
|
|
|
final class Type implements ConstraintInterface |
|
12
|
|
|
{ |
|
13
|
|
|
const KEYWORD = 'type'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* {@inheritdoc} |
|
17
|
|
|
*/ |
|
18
|
50 |
|
public function validate($value, $type, Validator $validator) |
|
19
|
|
|
{ |
|
20
|
50 |
|
Assert::type($type, ['array', 'string'], self::KEYWORD, $validator->getSchemaPath()); |
|
21
|
|
|
|
|
22
|
48 |
|
if (is_array($type)) { |
|
23
|
4 |
|
return $this->anyType($value, $type, $validator); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
switch ($type) { |
|
27
|
48 |
|
case 'object': |
|
28
|
16 |
|
return $this->validateType($value, 'is_object', $validator); |
|
29
|
46 |
|
case 'array': |
|
30
|
8 |
|
return $this->validateType($value, 'is_array', $validator); |
|
31
|
44 |
|
case 'boolean': |
|
32
|
8 |
|
return $this->validateType($value, 'is_bool', $validator); |
|
33
|
42 |
|
case 'null': |
|
34
|
4 |
|
return $this->validateType($value, 'is_null', $validator); |
|
35
|
42 |
|
case 'number': |
|
36
|
10 |
|
return $this->validateType( |
|
37
|
5 |
|
$value, |
|
38
|
10 |
|
'League\JsonGuard\is_json_number', |
|
39
|
|
|
$validator |
|
40
|
5 |
|
); |
|
41
|
38 |
|
case 'integer': |
|
42
|
32 |
|
return $this->validateType( |
|
43
|
16 |
|
$value, |
|
44
|
32 |
|
'League\JsonGuard\is_json_integer', |
|
45
|
|
|
$validator |
|
46
|
16 |
|
); |
|
47
|
26 |
|
case 'string': |
|
48
|
26 |
|
return $this->validateType($value, 'is_string', $validator); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param mixed $value |
|
54
|
|
|
* @param callable $callable |
|
55
|
|
|
* @param \League\JsonGuard\Validator $validator |
|
56
|
|
|
* |
|
57
|
|
|
* @return \League\JsonGuard\ValidationError|null |
|
58
|
|
|
* |
|
59
|
|
|
*/ |
|
60
|
48 |
|
private function validateType($value, callable $callable, Validator $validator) |
|
61
|
|
|
{ |
|
62
|
48 |
|
if (call_user_func($callable, $value) === true) { |
|
63
|
44 |
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
36 |
|
return error('The data must be a(n) {parameter}.', $validator); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param mixed $value |
|
71
|
|
|
* @param array $choices |
|
72
|
|
|
* |
|
73
|
|
|
* @param Validator $validator |
|
74
|
|
|
* |
|
75
|
|
|
* @return ValidationError|null |
|
76
|
|
|
*/ |
|
77
|
4 |
|
private function anyType($value, array $choices, Validator $validator) |
|
78
|
|
|
{ |
|
79
|
4 |
|
foreach ($choices as $type) { |
|
80
|
4 |
|
$error = $this->validate($value, $type, $validator); |
|
81
|
4 |
|
if (is_null($error)) { |
|
82
|
4 |
|
return null; |
|
83
|
|
|
} |
|
84
|
2 |
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
return error('The data must be one of {parameter}.', $validator); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|