1 | <?php |
||
18 | class ObjectHelper |
||
19 | { |
||
20 | /** |
||
21 | * Checks a given variable if its an instance of an element in the $instances list. |
||
22 | * |
||
23 | * ```php |
||
24 | * $object = new \Exception(); |
||
25 | * |
||
26 | * ObjectHelper::isInstanceOf($object, '\Exception'); |
||
27 | * ``` |
||
28 | * |
||
29 | * In order to check if an object is at least an instance of one given resourcese use: |
||
30 | * |
||
31 | * ```php |
||
32 | * ObjectHelper::isInstanceOf($object, ['not\this\Object', 'maybe\this\Object', '\Exception']); |
||
33 | * ``` |
||
34 | * |
||
35 | * If at least one of the given exception names in the array is an instance of the given object, true |
||
36 | * is returned. |
||
37 | * |
||
38 | * @param object $object The object to type check against haystack. |
||
39 | * @param string|array|object $haystack A list of classes, a string for a given class, or an object. |
||
40 | * @param boolean $throwException Whether an exception should be thrown or not. |
||
41 | * @throws \luya\Exception |
||
42 | * @return boolean |
||
43 | * @since 1.0.3 |
||
44 | */ |
||
45 | public static function isInstanceOf($object, $haystack, $throwException = true) |
||
66 | |||
67 | /** |
||
68 | * Check whether a given object contains a trait. |
||
69 | * |
||
70 | * ```php |
||
71 | * trait XYZ { |
||
72 | * |
||
73 | * } |
||
74 | * |
||
75 | * class ABC { |
||
76 | * use XYZ; |
||
77 | * } |
||
78 | * |
||
79 | * $object = new ABC(); |
||
80 | * |
||
81 | * ObjectHelper::isTraitInstanceOf($object, XYZ::class); |
||
82 | * ``` |
||
83 | * |
||
84 | * @param object $object |
||
85 | * @param string|array|object $haystack |
||
86 | * @return boolean |
||
87 | * @since 1.0.17 |
||
88 | */ |
||
89 | public static function isTraitInstanceOf($object, $haystack) |
||
106 | |||
107 | /** |
||
108 | * Get an array with all traits for a given object |
||
109 | * |
||
110 | * @param object $object |
||
111 | * @param boolean $autoload |
||
112 | * @return array |
||
113 | * @since 1.0.17 |
||
114 | * @see https://www.php.net/manual/en/function.class-uses.php#122427 |
||
115 | */ |
||
116 | public static function traitsList($object, $autoload = true) |
||
139 | |||
140 | /** |
||
141 | * Convert Object to Array |
||
142 | * |
||
143 | * @param object $object |
||
144 | * @return array |
||
145 | */ |
||
146 | public static function toArray($object) |
||
150 | |||
151 | /** |
||
152 | * Call a method and ensure arguments. |
||
153 | * |
||
154 | * Call a class method with arguments and verify the arguments if they are in the list of method arguments or not. |
||
155 | * |
||
156 | * ```php |
||
157 | * ObjectHelper::callMethodSanitizeArguments(new MyClass(), 'methodToCall', ['paramName' => 'paramValue']); |
||
158 | * ``` |
||
159 | * |
||
160 | * The response is the return value from the called method of the object. |
||
161 | * |
||
162 | * @param object $object The class object where the method must be found. |
||
163 | * @param string $method The class method to call inside the object. |
||
164 | * @param array $argumentsList A massiv assigned list of array items, where the key is bind to the method argument and the value to be passed in the method on call. |
||
165 | * @throws \luya\Exception Throws an exception if a argument coult not be found. |
||
166 | * @return mixed |
||
167 | */ |
||
168 | public static function callMethodSanitizeArguments($object, $method, array $argumentsList = []) |
||
169 | { |
||
170 | // get class reflection object |
||
171 | $reflection = new ReflectionMethod($object, $method); |
||
172 | // array where the sanitized arguemnts will be stored |
||
173 | $methodArgs = []; |
||
174 | |||
175 | foreach ($reflection->getParameters() as $param) { |
||
176 | // add the argument into the method list when existing |
||
177 | if (array_key_exists($param->name, $argumentsList)) { |
||
178 | $methodArgs[] = $argumentsList[$param->name]; |
||
179 | } |
||
180 | // check if the provided arguemnt is optional or not |
||
181 | if (!$param->isOptional() && !array_key_exists($param->name, $argumentsList)) { |
||
182 | throw new Exception(sprintf("The argument '%s' is required for method '%s' in class '%s'.", $param->name, $method, get_class($object))); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | return call_user_func_array([$object, $method], $methodArgs); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Get all actions from a given controller. |
||
191 | * |
||
192 | * @param Controller $controller |
||
193 | * @return array |
||
194 | * @since 1.0.19 |
||
195 | */ |
||
196 | public static function getActions(Controller $controller) |
||
209 | |||
210 | /** |
||
211 | * Get all controllers for a given luya Module |
||
212 | * |
||
213 | * @param Module $module |
||
214 | * @return array |
||
215 | * @since 1.0.19 |
||
216 | */ |
||
217 | public static function getControllers(Module $module) |
||
239 | |||
240 | /** |
||
241 | * Namify a controller file |
||
242 | * |
||
243 | * @param string $prefix |
||
244 | * @param string $file |
||
245 | * @return string |
||
246 | * @since 1.0.19 |
||
247 | */ |
||
248 | private static function fileToName($prefix, $file) |
||
252 | } |
||
253 |