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
|
|
|
|