|
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; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Stores data related to a particular validation task (default schema version, |
|
14
|
|
|
* accumulated violations, current path, etc.). |
|
15
|
|
|
*/ |
|
16
|
|
|
class Context |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private $violations = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private $pathSegments = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
private $pathLength = 0; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Pushes a path segment onto the context stack, making it the current |
|
35
|
|
|
* visited node. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $pathSegment |
|
38
|
|
|
*/ |
|
39
|
455 |
|
public function enterNode($pathSegment) |
|
40
|
|
|
{ |
|
41
|
455 |
|
$this->pathSegments[$this->pathLength++] = $pathSegment; |
|
42
|
455 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Leaves the current node and enters another node located at the same |
|
46
|
|
|
* depth in the hierarchy. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $pathSegment |
|
49
|
|
|
*/ |
|
50
|
167 |
|
public function enterSibling($pathSegment) |
|
51
|
|
|
{ |
|
52
|
167 |
|
$this->pathSegments[$this->pathLength - 1] = $pathSegment; |
|
53
|
167 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Removes the current node from the context stack, thus returning to the |
|
57
|
|
|
* previous (parent) node. |
|
58
|
|
|
*/ |
|
59
|
408 |
|
public function leaveNode() |
|
60
|
|
|
{ |
|
61
|
408 |
|
if ($this->pathLength === 0) { |
|
62
|
1 |
|
throw new \LogicException('Cannot leave node'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
407 |
|
--$this->pathLength; |
|
66
|
407 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns the path of the current node. |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
413 |
|
public function getCurrentPath() |
|
74
|
|
|
{ |
|
75
|
413 |
|
$this->pathSegments = array_slice($this->pathSegments, 0, $this->pathLength); |
|
76
|
|
|
|
|
77
|
413 |
|
return $this->pathLength ? '/'.implode('/', $this->pathSegments) : ''; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Adds a violation message for the current node. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $message |
|
84
|
|
|
* @param array $parameters |
|
85
|
|
|
*/ |
|
86
|
235 |
|
public function addViolation($message, array $parameters = []) |
|
87
|
|
|
{ |
|
88
|
235 |
|
$this->violations[] = [ |
|
89
|
235 |
|
'path' => $this->getCurrentPath(), |
|
90
|
235 |
|
'message' => vsprintf($message, $parameters), |
|
91
|
|
|
]; |
|
92
|
235 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns the list of accumulated violations. |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
484 |
|
public function getViolations() |
|
100
|
|
|
{ |
|
101
|
484 |
|
return $this->violations; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns the number of accumulated violations. |
|
106
|
|
|
* |
|
107
|
|
|
* @return int |
|
108
|
|
|
*/ |
|
109
|
69 |
|
public function countViolations() |
|
110
|
|
|
{ |
|
111
|
69 |
|
return count($this->violations); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns a copy of the context, optionally purged of its |
|
116
|
|
|
* accumulated violations. |
|
117
|
|
|
* |
|
118
|
|
|
* @param bool $withViolations |
|
119
|
|
|
* |
|
120
|
|
|
* @return Context |
|
121
|
|
|
*/ |
|
122
|
48 |
|
public function duplicate($withViolations = true) |
|
123
|
|
|
{ |
|
124
|
|
|
// cloning as long as the context doesn't hold object references |
|
125
|
48 |
|
$clone = clone $this; |
|
126
|
|
|
|
|
127
|
48 |
|
if (!$withViolations) { |
|
128
|
18 |
|
$clone->purgeViolations(); |
|
129
|
18 |
|
} |
|
130
|
|
|
|
|
131
|
48 |
|
return $clone; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Merges the current violations with the violations stored in |
|
136
|
|
|
* another context. |
|
137
|
|
|
* |
|
138
|
|
|
* @param Context $context |
|
139
|
|
|
*/ |
|
140
|
15 |
|
public function mergeViolations(Context $context) |
|
141
|
|
|
{ |
|
142
|
15 |
|
$this->violations = array_merge($this->violations, $context->getViolations()); |
|
143
|
15 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Deletes the list of accumulated violations. |
|
147
|
|
|
*/ |
|
148
|
18 |
|
public function purgeViolations() |
|
149
|
|
|
{ |
|
150
|
18 |
|
$this->violations = []; |
|
151
|
18 |
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|