InterfaceBuilder.php
2.12 KB
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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Builders;
use YahnisElsts\AdminMenuEditor\Customizable\Controls;
class InterfaceBuilder {
protected $children = array();
/**
* @param \YahnisElsts\AdminMenuEditor\Customizable\Controls\Container|ContainerBuilder $container
* @return $this
*/
public function add($container) {
$this->children[] = $container;
return $this;
}
/**
* Add a container before the first direct descendant that has the specified ID.
*
* If there is no child with that ID, the container will be added to the beginning
* of the list.
*
* @param Controls\Container|ContainerBuilder $container
* @param string $beforeId
* @return $this
*/
public function addBefore($container, $beforeId) {
$index = $this->findChildIndex($beforeId);
if ( $index === false ) {
array_unshift($this->children, $container);
} else {
array_splice($this->children, $index, 0, [$container]);
}
return $this;
}
/**
* @param Controls\Container|ContainerBuilder $container
* @param string $afterId
* @return $this
*/
public function addAfter($container, $afterId) {
$index = $this->findChildIndex($afterId);
if ( $index === false ) {
$this->children[] = $container;
} else {
array_splice($this->children, $index + 1, 0, [$container]);
}
return $this;
}
protected function findChildIndex($id) {
foreach ($this->children as $index => $child) {
if ( $child instanceof Controls\UiElement ) {
$childId = $child->getId();
} else if ( $child instanceof BaseElementBuilder ) {
$childId = $child->getCustomId();
} else {
$childId = null;
}
if ( $childId === $id ) {
return $index;
}
}
return null;
}
/**
* @return \YahnisElsts\AdminMenuEditor\Customizable\Controls\InterfaceStructure
*/
public function build() {
return new Controls\InterfaceStructure('', $this->buildChildren());
}
protected function buildChildren() {
$children = array();
foreach ($this->children as $child) {
if ( $child instanceof ElementBuilder ) {
$children[] = $child->build();
} else {
$children[] = $child;
}
}
return $children;
}
}