BaseElementBuilder.php
3.16 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
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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Builders;
use YahnisElsts\AdminMenuEditor\Customizable\Controls\UiElement;
/**
* @template ElementClass of UiElement
*/
abstract class BaseElementBuilder implements ElementBuilder {
/**
* @var array
*/
protected $params = array();
/**
* @var class-string<ElementClass>
*/
protected $elementClass;
/**
* @param class-string<\YahnisElsts\AdminMenuEditor\Customizable\Controls\UiElement> $elementClass
* @param array $params
*/
protected function __construct($elementClass, $params = array()) {
$this->elementClass = $elementClass;
$this->params = $params;
}
protected static function buildItems($items, $preserveKeys = false) {
$results = array();
foreach ($items as $key => $item) {
if ( is_array($item) ) {
//Flatten nested arrays of buildable things.
$results = array_merge($results, self::buildItems($item, $preserveKeys));
continue;
}
if ( $item instanceof ElementBuilder ) {
$item = $item->build();
} elseif ( !($item instanceof UiElement) ) {
$typeString = is_object($item) ? get_class($item) : gettype($item);
throw new \InvalidArgumentException(
'Invalid item type for an element builder: ' . $typeString
);
}
if ( $preserveKeys ) {
$results[$key] = $item;
} else {
$results[] = $item;
}
}
return $results;
}
public function id($string) {
$this->params['id'] = $string;
return $this;
}
public function getCustomId() {
return isset($this->params['id']) ? $this->params['id'] : null;
}
/**
* @param string|callable $textOrCallback
* @return $this
*/
public function description($textOrCallback) {
$this->params['description'] = $textOrCallback;
return $this;
}
public function classes(...$cssClassNames) {
return $this->addItemsToArrayParam('classes', $cssClassNames);
}
/**
* Add CSS class names if their values are truthy.
*
* @param array<string,mixed> $classEnabled ['class-a' => true, 'class-b' => false, ...]
* @return $this
*/
public function conditionalClasses($classEnabled) {
return $this->classes(...array_keys(array_filter($classEnabled)));
}
public function styles($propertyPairs) {
return $this->addItemsToArrayParam('style', $propertyPairs);
}
/**
* @param string $paramName
* @param $items
* @return $this
*/
protected function addItemsToArrayParam($paramName, $items) {
if ( !isset($this->params[$paramName]) ) {
$this->params[$paramName] = array();
}
$this->params[$paramName] = array_merge($this->params[$paramName], (array)$items);
return $this;
}
/**
* Set one or more parameters for the element.
*
* Will overwrite any existing parameters with the same name.
*
* @param array<string,mixed> $additionalParams
* @return $this
*/
public function params($additionalParams) {
$this->params = array_merge($this->params, $additionalParams);
return $this;
}
/**
* Render the element only if the condition evaluates to true.
*
* @param bool|callable $condition
*/
public function onlyIf($condition) {
$this->params['renderCondition'] = $condition;
return $this;
}
/**
* @return ElementClass
*/
abstract public function build();
}