ILIAS  release_8 Revision v8.24
ComponentHelper.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
22
24use InvalidArgumentException;
25use Closure;
26
30trait ComponentHelper
31{
36 public function getCanonicalName(): string
37 {
38 return $this->getCanonicalNameByFullyQualifiedName();
39 }
40
44 protected function getCanonicalNameByFullyQualifiedName(): string
45 {
46 $cls = explode("\\", get_class($this));
47 $name = [];
48 $cur = array_pop($cls);
49 while ($cur !== "Component" && count($cls) > 0) {
50 $name[] = preg_replace("%([a-z])([A-Z])%", "$1 $2", $cur);
51 $cur = array_pop($cls);
52 }
53 return implode(" ", $name);
54 }
55
61 protected function checkArg(string $which, bool $check, string $message): void
62 {
63 if (!$check) {
64 throw new InvalidArgumentException("Argument '$which': $message");
65 }
66 }
67
74 protected function checkStringArg(string $which, $value): void
75 {
76 $this->checkArg($which, is_string($value), $this->wrongTypeMessage("string", $value));
77 }
78
85 protected function checkStringOrSignalArg(string $which, $value): void
86 {
87 $this->checkArg(
88 $which,
89 is_string($value) || $value instanceof Signal,
90 $this->wrongTypeMessage("string or Signal", gettype($value))
91 );
92 }
93
100 protected function checkBoolArg(string $which, $value): void
101 {
102 $this->checkArg($which, is_bool($value), $this->wrongTypeMessage("bool", $value));
103 }
104
111 protected function checkArgInstanceOf(string $which, $value, string $class): void
112 {
113 $this->checkArg($which, $value instanceof $class, $this->wrongTypeMessage($class, $value));
114 }
115
122 protected function checkArgIsElement(string $which, $value, array $array, string $name): void
123 {
124 if (!is_object($value)) {
125 $message = "expected $name, got '$value'";
126 } else {
127 $message = "expected $name, got object.";
128 }
129 $this->checkArg($which, in_array($value, $array), $message);
130 }
131
139 protected function checkArgList(string $which, array &$values, Closure $check, Closure $message): void
140 {
141 $failed_k = null;
142 $failed_v = null;
143 foreach ($values as $key => $value) {
144 $ok = $check($key, $value);
145 if (!$ok) {
146 $failed_k = $key;
147 $failed_v = $value;
148 break;
149 }
150 }
151
152 if ($failed_k !== null) {
153 $m = $message($failed_k, $failed_v);
154 } else {
155 $m = "";
156 }
157
158 $this->checkArg($which, $failed_k === null, $m);
159 }
160
168 protected function checkArgListElements(string $which, array &$values, $classes): void
169 {
170 $classes = $this->toArray($classes);
171 $this->checkArgList($which, $values, function ($_, $value) use (&$classes) {
172 foreach ($classes as $cls) {
173 if ($cls === "string" && is_string($value)) {
174 return true;
175 }
176 if ($cls === "int" && is_int($value)) {
177 return true;
178 } elseif ($value instanceof $cls) {
179 return true;
180 }
181 }
182 return false;
183 }, function ($_, $failed) use (&$classes) {
184 return $this->wrongTypeMessage(implode(", ", $classes), $failed);
185 });
186 }
187
196 protected function checkInputListElements(string $which, array $values, $classes, int $depth = 0): void
197 {
198 if (255 < $depth) {
199 throw new \LogicException('Input nesting limit of 255 exceeded.');
200 }
201
202 $classes = $this->toArray($classes);
203 foreach ($values as $value) {
204 foreach ($classes as $class) {
205 $this->checkArgInstanceOf($which, $value, $class);
206 }
207
208 if ($value instanceof \ILIAS\UI\Component\Input\Group) {
209 $this->checkInputListElements($which, $value->getInputs(), $classes, $depth + 1);
210 }
211 }
212 }
213
219 protected function toArray($value): array
220 {
221 if (is_array($value)) {
222 return $value;
223 }
224 return array($value);
225 }
226
230 protected function wrongTypeMessage(string $expected, $value): string
231 {
232 $type = gettype($value);
233 if (!is_object($value) && !is_array($value)) {
234 return "expected $expected, got $type '$value'";
235 } else {
236 if (is_object($value)) {
237 $type = get_class($value);
238 }
239 return "expected $expected, got $type";
240 }
241 }
242}
$check
Definition: buildRTE.php:81
getCanonicalName()
Get the canonical name of the component.
if($format !==null) $name
Definition: metadata.php:247
string $key
Consumer key/client ID value.
Definition: System.php:193
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider \MainMenu\Provider.
Class Factory.
$type
$message
Definition: xapiexit.php:32