19declare(strict_types=1);
24use InvalidArgumentException;
39 return $this->getCanonicalNameByFullyQualifiedName();
45 protected function getCanonicalNameByFullyQualifiedName(): string
47 $cls = explode(
"\\", get_class($this));
49 $cur = array_pop($cls);
50 while ($cur !==
"Component" && count($cls) > 0) {
51 $name[] = preg_replace(
"%([a-z])([A-Z])%",
"$1 $2", $cur);
52 $cur = array_pop($cls);
54 return implode(
" ", $name);
62 protected function checkArg(
string $which,
bool $check,
string $message): void
65 throw new InvalidArgumentException(
"Argument '$which': $message");
75 protected function checkStringArg(
string $which, $value): void
77 $this->checkArg($which, is_string($value), $this->wrongTypeMessage(
"string", $value));
86 protected function checkStringOrSignalArg(
string $which, $value): void
90 is_string($value) || $value instanceof Signal,
91 $this->wrongTypeMessage(
"string or Signal", gettype($value))
101 protected function checkBoolArg(
string $which, $value): void
103 $this->checkArg($which, is_bool($value), $this->wrongTypeMessage(
"bool", $value));
112 protected function checkArgInstanceOf(
string $which, $value,
string $class): void
114 $this->checkArg($which, $value instanceof $class, $this->wrongTypeMessage($class, $value));
123 protected function checkArgIsElement(
string $which, $value, array $array,
string $name): void
125 if (!is_object($value)) {
126 $message =
"expected $name, got '$value'";
128 $message =
"expected $name, got object.";
130 $this->checkArg($which, in_array($value, $array),
$message);
140 protected function checkArgList(
string $which, array &$values, Closure
$check, Closure
$message): void
144 foreach ($values as $key => $value) {
145 $ok =
$check($key, $value);
153 if ($failed_k !==
null) {
154 $m =
$message($failed_k, $failed_v);
159 $this->checkArg($which, $failed_k ===
null, $m);
169 protected function checkArgListElements(
string $which, array &$values, $classes): void
171 $classes = $this->toArray($classes);
172 $this->checkArgList($which, $values,
function ($_, $value) use (&$classes) {
173 foreach ($classes as $cls) {
174 if ($cls ===
"string" && is_string($value)) {
177 if ($cls ===
"int" && is_int($value)) {
179 } elseif ($value instanceof $cls) {
184 },
function ($_, $failed) use (&$classes) {
185 return $this->wrongTypeMessage(implode(
", ", $classes), $failed);
197 protected function checkInputListElements(
string $which, array $values, $classes,
int $depth = 0): void
200 throw new \LogicException(
'Input nesting limit of 255 exceeded.');
203 $classes = $this->toArray($classes);
204 foreach ($values as $value) {
205 foreach ($classes as $class) {
206 $this->checkArgInstanceOf($which, $value, $class);
210 $this->checkInputListElements($which, $value->getInputs(), $classes, $depth + 1);
220 protected function toArray($value): array
222 if (is_array($value)) {
225 return array($value);
231 protected function wrongTypeMessage(
string $expected, $value): string
233 $type = gettype($value);
234 if (!is_object($value) && !is_array($value)) {
235 return "expected $expected, got $type '$value'";
237 if (is_object($value)) {
238 $type = get_class($value);
240 return "expected $expected, got $type";
244 public function reduceWith(\Closure $fn): mixed
246 $clone = clone $this;
248 foreach ($clone->getSubComponents() ?? [] as $component) {
249 $results[] = $component->reduceWith($fn);
262 protected function getSubComponents(): ?array
A component is the most general form of an entity in the UI.
reduceWith(\Closure $fn)
The scheme starts at the leaves of the structure and applies the function to each leave and moves up ...
getCanonicalName()
Get the canonical name of the component.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.