Context   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A push() 0 3 1
A offsetExists() 0 3 1
A offsetSet() 0 3 1
A isRoot() 0 3 1
A offsetGet() 0 3 1
A pop() 0 3 1
A isBranch() 0 3 1
A offsetUnset() 0 3 1
A closest() 0 9 3
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie\Transformation;
13
14
/**
15
 * A transformation context.
16
 */
17
class Context implements \ArrayAccess
18
{
19
    /**
20
     * @var array
21
     */
22
    private $data = [];
23
24
    /**
25
     * @var array
26
     */
27
    private $chain = [];
28
29
    /**
30
     * @param array $data
31
     */
32
    public function __construct(array $data = [])
33
    {
34
        $this->data = $data;
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function offsetExists($offset)
41
    {
42
        return isset($this->data[$offset]);
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function offsetGet($offset)
49
    {
50
        return $this->data[$offset] ?? null;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function offsetSet($offset, $value)
57
    {
58
        $this->data[$offset] = $value;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function offsetUnset($offset)
65
    {
66
        unset($this->data[$offset]);
67
    }
68
69
    /**
70
     * Push data to the transformation chain.
71
     *
72
     * @param mixed $data
73
     */
74
    public function push($data)
75
    {
76
        array_push($this->chain, $data);
77
    }
78
79
    /**
80
     * Pop data from the transformation chain.
81
     */
82
    public function pop()
83
    {
84
        array_pop($this->chain);
85
    }
86
87
    /**
88
     * Whether the current data being transformed is the root data.
89
     *
90
     * @return bool
91
     */
92
    public function isRoot(): bool
93
    {
94
        return count($this->chain) < 2;
95
    }
96
97
    /**
98
     * Whether the current data being transformed is a branch.
99
     *
100
     * @return bool
101
     */
102
    public function isBranch(): bool
103
    {
104
        return count($this->chain) > 1;
105
    }
106
107
    /**
108
     * Finds the closest item in the transformation chain matching the specified class.
109
     *
110
     * @param string $class
111
     *
112
     * @return mixed|null
113
     */
114
    public function closest(string $class)
115
    {
116
        foreach (array_reverse($this->chain) as $data) {
117
            if ($data instanceof $class) {
118
                return $data;
119
            }
120
        }
121
122
        return null;
123
    }
124
}
125