-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNesty.php
230 lines (196 loc) · 5.13 KB
/
Nesty.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace Orchestra\Support;
class Nesty
{
use Concerns\Descendible;
/**
* List of items.
*
* @var array
*/
protected $items = [];
/**
* Configuration.
*
* @var array
*/
protected $config = [];
/**
* Construct a new instance.
*
* @param array $config
*/
public function __construct(array $config = [])
{
$this->config = $config;
}
/**
* Create a new Fluent instance while appending default config.
*
* @param int $id
*
* @return \Orchestra\Support\Fluent
*/
protected function toFluent($id): Fluent
{
$defaults = $this->config['defaults'] ?? [];
$class = $this->config['fluent'] ?? Fluent::class;
return new $class(\array_merge($defaults, [
'id' => $id,
'childs' => [],
]));
}
/**
* Add item before reference $before.
*
* @param string $id
* @param string $before
*
* @return \Orchestra\Support\Fluent
*/
protected function addBefore(string $id, string $before): Fluent
{
$items = [];
$item = $this->toFluent($id);
$keys = \array_keys($this->items);
$position = \array_search($before, $keys);
if ($position === false) {
return $this->addParent($id);
}
foreach ($keys as $key => $fluent) {
if ($key === $position) {
$items[$id] = $item;
}
$items[$fluent] = $this->items[$fluent];
}
$this->items = $items;
return $item;
}
/**
* Add item after reference $after.
*
* @param string $id
* @param string $after
*
* @return \Orchestra\Support\Fluent
*/
protected function addAfter(string $id, string $after): Fluent
{
$items = [];
$item = $this->toFluent($id);
$keys = \array_keys($this->items);
$position = \array_search($after, $keys);
if ($position === false) {
return $this->addParent($id);
}
foreach ($keys as $key => $fluent) {
$items[$fluent] = $this->items[$fluent];
if ($key === $position) {
$items[$id] = $item;
}
}
$this->items = $items;
return $item;
}
/**
* Add item as child of $parent.
*
* @param string $id
* @param string $parent
*
* @return \Orchestra\Support\Fluent|null
*/
protected function addChild(string $id, string $parent): ?Fluent
{
$node = $this->descendants($this->items, $parent);
// it might be possible parent is not defined due to ACL, in this
// case we should simply ignore this request as child should
// inherit parent ACL access.
if (! isset($node)) {
return null;
}
$item = $node->get('childs');
$item[$id] = $this->toFluent($id);
$node->childs($item);
return $item[$id];
}
/**
* Add item as parent.
*
* @param string $id
*
* @return \Orchestra\Support\Fluent
*/
protected function addParent(string $id): Fluent
{
return $this->items[$id] = $this->toFluent($id);
}
/**
* Add a new item, by prepend or append.
*
* @param string $id
* @param string $location
*
* @return \Orchestra\Support\Fluent|null
*/
public function add(string $id, string $location = '#'): ?Fluent
{
if ($location === '<' && count($keys = array_keys($this->items)) > 0) {
return $this->addBefore($id, $keys[0]);
} elseif (\preg_match('/^(<|>|\^):(.+)$/', $location, $matches) && count($matches) >= 3) {
return $this->pickTraverseFromMatchedExpression($id, $matches[1], $matches[2]);
}
return $this->addParent($id);
}
/**
* Pick traverse from matched expression.
*
* @param string $id
* @param string $key
* @param string $location
*
* @return \Orchestra\Support\Fluent|null
*/
protected function pickTraverseFromMatchedExpression(string $id, string $key, string $location): ?Fluent
{
$matching = [
'<' => 'addBefore',
'>' => 'addAfter',
'^' => 'addChild',
];
$method = $matching[$key];
return $this->{$method}($id, $location);
}
/**
* Check whether item by id exists.
*
* @param string $key
*
* @return bool
*/
public function has(string $key): bool
{
$key = \implode('.childs.', \explode('.', $key));
return ! \is_null(\data_get($this->items, $key));
}
/**
* Retrieve an item by id.
*
* @param string|null $key
*
* @return \Orchestra\Support\Fluent|array|null
*/
public function is(?string $key)
{
return $this->descendants($this->items, $key);
}
/**
* Return all items.
*
* @return \Orchestra\Support\Collection
*/
public function items(): Collection
{
return new Collection($this->items);
}
}