1 | <?php |
||
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 | 458 | public function enterNode($pathSegment) |
|
40 | { |
||
41 | 458 | $this->pathSegments[$this->pathLength++] = $pathSegment; |
|
42 | 458 | } |
|
43 | |||
44 | |||
45 | /** |
||
46 | * Leaves the current node and enters another node located at the same |
||
47 | * depth in the hierarchy. |
||
48 | * |
||
49 | * @param string $pathSegment |
||
50 | */ |
||
51 | 170 | public function enterSibling($pathSegment) |
|
52 | { |
||
53 | 170 | $this->pathSegments[$this->pathLength - 1] = $pathSegment; |
|
54 | 170 | } |
|
55 | |||
56 | /** |
||
57 | * Removes the current node from the context stack, thus returning to the |
||
58 | * previous (parent) node. |
||
59 | */ |
||
60 | 411 | public function leaveNode() |
|
61 | { |
||
62 | 411 | if ($this->pathLength === 0) { |
|
63 | 1 | throw new \LogicException('Cannot leave node'); |
|
64 | } |
||
65 | |||
66 | 410 | $this->pathLength--; |
|
67 | 410 | } |
|
68 | |||
69 | /** |
||
70 | * Returns the path of the current node. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 416 | public function getCurrentPath() |
|
75 | { |
||
76 | 416 | $this->pathSegments = array_slice($this->pathSegments, 0, $this->pathLength); |
|
77 | 416 | 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 | 238 | public function addViolation($message, array $parameters = []) |
|
93 | |||
94 | /** |
||
95 | * Returns the list of accumulated violations. |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | 487 | public function getViolations() |
|
103 | |||
104 | /** |
||
105 | * Returns the number of accumulated violations. |
||
106 | * |
||
107 | * @return int |
||
108 | */ |
||
109 | 72 | public function countViolations() |
|
113 | |||
114 | /** |
||
115 | * Returns a copy of the context, optionally purged of its |
||
116 | * accumulated violations. |
||
117 | * |
||
118 | * @param bool $withViolations |
||
119 | * @return Context |
||
120 | */ |
||
121 | 51 | public function duplicate($withViolations = true) |
|
132 | |||
133 | /** |
||
134 | * Merges the current violations with the violations stored in |
||
135 | * another context. |
||
136 | * |
||
137 | * @param Context $context |
||
138 | */ |
||
139 | 18 | public function mergeViolations(Context $context) |
|
143 | |||
144 | /** |
||
145 | * Deletes the list of accumulated violations. |
||
146 | */ |
||
147 | 21 | public function purgeViolations() |
|
151 | } |
||
152 |