|
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\EmptyArrayException; |
|
15
|
|
|
use JVal\Exception\Constraint\InvalidTypeException; |
|
16
|
|
|
use JVal\Exception\Constraint\NotUniqueException; |
|
17
|
|
|
use JVal\Types; |
|
18
|
|
|
use JVal\Utils; |
|
19
|
|
|
use JVal\Walker; |
|
20
|
|
|
use stdClass; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Constraint for the "enum" keyword. |
|
24
|
|
|
*/ |
|
25
|
|
|
class EnumConstraint implements Constraint |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
373 |
|
public function keywords() |
|
31
|
|
|
{ |
|
32
|
373 |
|
return ['enum']; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
357 |
|
public function supports($type) |
|
39
|
|
|
{ |
|
40
|
357 |
|
return true; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
26 |
|
public function normalize(stdClass $schema, Context $context, Walker $walker) |
|
47
|
|
|
{ |
|
48
|
26 |
|
$context->enterNode('enum'); |
|
49
|
|
|
|
|
50
|
26 |
|
if (!is_array($schema->enum)) { |
|
51
|
1 |
|
throw new InvalidTypeException($context, Types::TYPE_ARRAY); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
25 |
|
if (count($schema->enum) === 0) { |
|
55
|
1 |
|
throw new EmptyArrayException($context); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
24 |
|
foreach ($schema->enum as $i => $aItem) { |
|
59
|
24 |
|
foreach ($schema->enum as $j => $bItem) { |
|
60
|
24 |
|
if ($i !== $j && Utils::areEqual($aItem, $bItem)) { |
|
61
|
1 |
|
throw new NotUniqueException($context); |
|
62
|
|
|
} |
|
63
|
24 |
|
} |
|
64
|
23 |
|
} |
|
65
|
|
|
|
|
66
|
23 |
|
$context->leaveNode(); |
|
67
|
23 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
19 |
|
public function apply($instance, stdClass $schema, Context $context, Walker $walker) |
|
73
|
|
|
{ |
|
74
|
19 |
|
$hasMatch = false; |
|
75
|
|
|
|
|
76
|
19 |
|
foreach ($schema->enum as $value) { |
|
77
|
19 |
|
if (Utils::areEqual($instance, $value)) { |
|
78
|
11 |
|
$hasMatch = true; |
|
79
|
11 |
|
break; |
|
80
|
|
|
} |
|
81
|
19 |
|
} |
|
82
|
|
|
|
|
83
|
19 |
|
if (!$hasMatch) { |
|
84
|
9 |
|
$context->addViolation('should match one element in enum'); |
|
85
|
9 |
|
} |
|
86
|
19 |
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|