ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
11{
15 private $canonical_name = null;
16
23 public function getCanonicalName()
24 {
25 if ($this->canonical_name === null) {
26 $this->canonical_name = $this->getCanonicalNameByFullyQualifiedName();
27 }
28 return $this->canonical_name;
29 }
30
37 {
38 $cls = explode("\\", get_class($this));
39 $name = [];
40 $cur = array_pop($cls);
41 while ($cur !== "Component" && count($cls) > 0) {
42 $name[] = preg_replace("%([a-z])([A-Z])%", "$1 $2", $cur);
43 $cur = array_pop($cls);
44 }
45 return implode(" ", $name);
46 }
47
59 protected function checkArg($which, $check, $message)
60 {
61 assert(is_string($which));
62 assert(is_bool($check));
63 assert(is_string($message));
64 if (!$check) {
65 throw new \InvalidArgumentException("Argument '$which': $message");
66 }
67 }
68
77 protected function checkIntArg($which, $value)
78 {
79 $this->checkArg($which, is_int($value), $this->wrongTypeMessage("integer", $value));
80 }
81
90 protected function checkStringArg($which, $value)
91 {
92 $this->checkArg($which, is_string($value), $this->wrongTypeMessage("string", $value));
93 }
94
103 protected function checkFloatArg($which, $value)
104 {
105 $this->checkArg($which, is_float($value), $this->wrongTypeMessage("float", $value));
106 }
107
117 protected function checkArgInstanceOf($which, $value, $class)
118 {
119 $this->checkArg($which, $value instanceof $class, $this->wrongTypeMessage($class, $value));
120 }
121
132 protected function checkArgIsElement($which, $value, $array, $name)
133 {
134 if (!is_object($value)) {
135 $message = "expected $name, got '$value'";
136 } else {
137 $message = "expected $name, got object.";
138 }
139 $message =
140 $this->checkArg($which, in_array($value, $array), $message);
141 }
142
153 protected function checkArgList($which, array &$values, \Closure $check, \Closure $message)
154 {
155 $failed_k = null;
156 $failed_v = null;
157 foreach ($values as $key => $value) {
158 $ok = $check($key, $value);
159 if (!$ok) {
160 $failed_k = $key;
161 $failed_v = $value;
162 break;
163 }
164 }
165
166 if ($failed_k !== null) {
167 $m = $message($failed_k, $failed_v);
168 } else {
169 $m = "";
170 }
171
172 $this->checkArg($which, $failed_k === null, $m);
173 }
174
185 protected function checkArgListElements($which, array &$values, &$classes)
186 {
187 $classes = $this->toArray($classes);
188 $this->checkArgList($which, $values, function ($_, $value) use (&$classes) {
189 foreach ($classes as $cls) {
190 if ($cls === "string" && is_string($value)) {
191 return true;
192 }
193 if ($cls === "int" && is_int($value)) {
194 return true;
195 } elseif ($value instanceof $cls) {
196 return true;
197 }
198 }
199 return false;
200 }, function ($_, $failed) use (&$classes) {
201 return $this->wrongTypeMessage(implode(", ", $classes), $failed);
202 });
203 }
204
211 protected function toArray($value)
212 {
213 if (is_array($value)) {
214 return $value;
215 }
216 return array($value);
217 }
218
219 protected function wrongTypeMessage($expected, $value)
220 {
221 $type = gettype($value);
222 if (!is_object($value) && !is_array($value)) {
223 return "expected $expected, got $type '$value'";
224 } else {
225 if (is_object($value)) {
226 $type = get_class($value);
227 }
228 return "expected $expected, got $type";
229 }
230 }
231}
$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.
if($format !==null) $name
Definition: metadata.php:146
catch(Exception $e) $message
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.
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