1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the JVal 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 JVal\Constraint; |
11
|
|
|
|
12
|
|
|
use JVal\Constraint; |
13
|
|
|
use JVal\Context; |
14
|
|
|
use JVal\Exception\Constraint\InvalidTypeException; |
15
|
|
|
use JVal\Exception\Constraint\NotPrimitiveTypeException; |
16
|
|
|
use JVal\Exception\Constraint\NotUniqueException; |
17
|
|
|
use JVal\Types; |
18
|
|
|
use JVal\Walker; |
19
|
|
|
use stdClass; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constraint for the "type" keyword. |
23
|
|
|
*/ |
24
|
|
|
class TypeConstraint implements Constraint |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
357 |
|
public function keywords() |
30
|
|
|
{ |
31
|
357 |
|
return ['type']; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
348 |
|
public function supports($type) |
38
|
|
|
{ |
39
|
348 |
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
205 |
|
public function normalize(stdClass $schema, Context $context, Walker $walker) |
46
|
|
|
{ |
47
|
205 |
|
$context->enterNode('type'); |
48
|
|
|
|
49
|
205 |
|
if (is_string($schema->type)) { |
50
|
186 |
|
if (!Types::isPrimitive($schema->type)) { |
51
|
1 |
|
throw new NotPrimitiveTypeException($context); |
52
|
|
|
} |
53
|
204 |
|
} elseif (is_array($schema->type)) { |
54
|
18 |
|
foreach ($schema->type as $index => $type) { |
55
|
18 |
|
$context->enterNode($index); |
56
|
|
|
|
57
|
18 |
|
if (!is_string($type)) { |
58
|
1 |
|
throw new InvalidTypeException($context, Types::TYPE_STRING); |
59
|
|
|
} |
60
|
|
|
|
61
|
18 |
|
if (!Types::isPrimitive($type)) { |
62
|
1 |
|
throw new NotPrimitiveTypeException($context); |
63
|
|
|
} |
64
|
|
|
|
65
|
18 |
|
$context->leaveNode(); |
66
|
18 |
|
} |
67
|
|
|
|
68
|
16 |
|
if (count(array_unique($schema->type)) !== count($schema->type)) { |
69
|
1 |
|
throw new NotUniqueException($context); |
70
|
|
|
} |
71
|
15 |
|
} else { |
72
|
1 |
|
throw new InvalidTypeException($context, [Types::TYPE_STRING, Types::TYPE_ARRAY]); |
73
|
|
|
} |
74
|
|
|
|
75
|
200 |
|
$context->leaveNode(); |
76
|
200 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
184 |
|
public function apply($instance, stdClass $schema, Context $context, Walker $walker) |
82
|
|
|
{ |
83
|
184 |
|
if (is_string($schema->type)) { |
84
|
169 |
|
if (!Types::isA($instance, $schema->type)) { |
85
|
90 |
|
$context->addViolation('instance must be of type %s', [$schema->type]); |
86
|
90 |
|
} |
87
|
169 |
|
} else { |
88
|
15 |
|
if (!Types::isOneOf($instance, $schema->type)) { |
89
|
7 |
|
$context->addViolation('instance does not match any of the expected types'); |
90
|
7 |
|
} |
91
|
|
|
} |
92
|
184 |
|
} |
93
|
|
|
} |
94
|
|
|
|