Context::getViolations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 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 = [])
87
    {
88 238
        $this->violations[] = [
89 238
            'path' => $this->getCurrentPath(),
90 238
            'message' => vsprintf($message, $parameters),
91
        ];
92 238
    }
93
94
    /**
95
     * Returns the list of accumulated violations.
96
     *
97
     * @return array
98
     */
99 487
    public function getViolations()
100
    {
101 487
        return $this->violations;
102
    }
103
104
    /**
105
     * Returns the number of accumulated violations.
106
     *
107
     * @return int
108
     */
109 72
    public function countViolations()
110
    {
111 72
        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
     * @return Context
120
     */
121 51
    public function duplicate($withViolations = true)
122
    {
123
        // cloning as long as the context doesn't hold object references
124 51
        $clone = clone $this;
125
126 51
        if (!$withViolations) {
127 21
            $clone->purgeViolations();
128 21
        }
129
130 51
        return $clone;
131
    }
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)
140
    {
141 18
        $this->violations = array_merge($this->violations, $context->getViolations());
142 18
    }
143
144
    /**
145
     * Deletes the list of accumulated violations.
146
     */
147 21
    public function purgeViolations()
148
    {
149 21
        $this->violations = [];
150 21
    }
151
}
152