ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ComponentHelper.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
6
8
13{
17 private $canonical_name = null;
18
25 public function getCanonicalName()
26 {
27 if ($this->canonical_name === null) {
28 $this->canonical_name = $this->getCanonicalNameByFullyQualifiedName();
29 }
30 return $this->canonical_name;
31 }
32
39 {
40 $cls = explode("\\", get_class($this));
41 $name = [];
42 $cur = array_pop($cls);
43 while ($cur !== "Component" && count($cls) > 0) {
44 $name[] = preg_replace("%([a-z])([A-Z])%", "$1 $2", $cur);
45 $cur = array_pop($cls);
46 }
47 return implode(" ", $name);
48 }
49
61 protected function checkArg($which, $check, $message)
62 {
63 assert(is_string($which));
64 assert(is_bool($check));
65 assert(is_string($message));
66 if (!$check) {
67 throw new \InvalidArgumentException("Argument '$which': $message");
68 }
69 }
70
79 protected function checkIntArg($which, $value)
80 {
81 $this->checkArg($which, is_int($value), $this->wrongTypeMessage("integer", $value));
82 }
83
92 protected function checkStringArg($which, $value)
93 {
94 $this->checkArg($which, is_string($value), $this->wrongTypeMessage("string", $value));
95 }
96
105 protected function checkStringOrSignalArg($which, $value)
106 {
107 $this->checkArg(
108 $which,
109 is_string($value) || $value instanceof Signal,
110 $this->wrongTypeMessage("string or Signal", gettype($value))
111 );
112 }
113
122 protected function checkFloatArg($which, $value)
123 {
124 $this->checkArg($which, is_float($value), $this->wrongTypeMessage("float", $value));
125 }
126
135 protected function checkBoolArg($which, $value)
136 {
137 $this->checkArg($which, is_bool($value), $this->wrongTypeMessage("bool", $value));
138 }
139
149 protected function checkArgInstanceOf($which, $value, $class)
150 {
151 $this->checkArg($which, $value instanceof $class, $this->wrongTypeMessage($class, $value));
152 }
153
164 protected function checkArgIsElement($which, $value, $array, $name)
165 {
166 if (!is_object($value)) {
167 $message = "expected $name, got '$value'";
168 } else {
169 $message = "expected $name, got object.";
170 }
171 $message =
172 $this->checkArg($which, in_array($value, $array), $message);
173 }
174
185 protected function checkArgList($which, array &$values, \Closure $check, \Closure $message)
186 {
187 $failed_k = null;
188 $failed_v = null;
189 foreach ($values as $key => $value) {
190 $ok = $check($key, $value);
191 if (!$ok) {
192 $failed_k = $key;
193 $failed_v = $value;
194 break;
195 }
196 }
197
198 if ($failed_k !== null) {
199 $m = $message($failed_k, $failed_v);
200 } else {
201 $m = "";
202 }
203
204 $this->checkArg($which, $failed_k === null, $m);
205 }
206
217 protected function checkArgListElements($which, array &$values, &$classes)
218 {
219 $classes = $this->toArray($classes);
220 $this->checkArgList($which, $values, function ($_, $value) use (&$classes) {
221 foreach ($classes as $cls) {
222 if ($cls === "string" && is_string($value)) {
223 return true;
224 }
225 if ($cls === "int" && is_int($value)) {
226 return true;
227 } elseif ($value instanceof $cls) {
228 return true;
229 }
230 }
231 return false;
232 }, function ($_, $failed) use (&$classes) {
233 return $this->wrongTypeMessage(implode(", ", $classes), $failed);
234 });
235 }
236
243 protected function toArray($value)
244 {
245 if (is_array($value)) {
246 return $value;
247 }
248 return array($value);
249 }
250
251 protected function wrongTypeMessage($expected, $value)
252 {
253 $type = gettype($value);
254 if (!is_object($value) && !is_array($value)) {
255 return "expected $expected, got $type '$value'";
256 } else {
257 if (is_object($value)) {
258 $type = get_class($value);
259 }
260 return "expected $expected, got $type";
261 }
262 }
263}
$failed
Definition: Utf8Test.php:85
An exception for terminatinating execution or to throw for unit testing.
$key
Definition: croninfo.php:18
getCanonicalName()
Get the canonical name of the component.
catch(Exception $e) $message
checkStringOrSignalArg($which, $value)
Throw an InvalidArgumentException if $value is no string or Signal.
checkArgList($which, array &$values, \Closure $check, \Closure $message)
Check every key and value of the list with a supplied closure.
checkStringArg($which, $value)
Throw an InvalidArgumentException if $value is no string.
checkArg($which, $check, $message)
/** Throw an InvalidArgumentException containing the message if $check is false.
checkIntArg($which, $value)
Throw an InvalidArgumentException if $value is no int.
toArray($value)
Wrap the given value in an array if it is no array.
checkArgListElements($which, array &$values, &$classes)
Check every element of the list if it is an instance of one of the given classes.
checkBoolArg($which, $value)
Throw an InvalidArgumentException if $value is not a bool.
trait ComponentHelper
Provides common functionality for component implementations.
checkArgIsElement($which, $value, $array, $name)
Throw an InvalidArgumentException if $value is not an element of array.
checkFloatArg($which, $value)
Throw an InvalidArgumentException if $value is not a float.
checkArgInstanceOf($which, $value, $class)
Throw an InvalidArgumentException if $value is not an instance of $class.
getCanonicalNameByFullyQualifiedName()
Does the calculation required for getCanonicalName.
$type
$values