Created
February 6, 2023 17:24
-
-
Save kmuenkel/5635dc4952c2b16e8df2ad91f2ca6679 to your computer and use it in GitHub Desktop.
Compile arguments in a Laravel fashion for closure bindings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| if (!function_exists('array_is_assoc')) { | |
| /** | |
| * Determine if the given array is associative or numerically indexed | |
| * | |
| * @param array $array | |
| * @return bool | |
| */ | |
| function array_is_assoc(array $array) | |
| { | |
| return ($keys = array_keys($array)) !== array_keys($keys); | |
| } | |
| } | |
| if (!function_exists('compile_arguments')) { | |
| /** | |
| * Convert argument array to parameter-name/value pairs | |
| * | |
| * @param string|string[] $function | |
| * @param array $args | |
| * @param array $defaults | |
| * @return array | |
| */ | |
| function compile_arguments($function, array $args = [], array $defaults = []) | |
| { | |
| $function = class_exists($function) ? [$function, '__construct'] : $function; | |
| list($names, $signatureDefaults) = get_parameter_definitions($function); | |
| $defaults = array_is_assoc($defaults) ? $defaults : array_combine($defaults, $names); | |
| $defaults = array_merge($signatureDefaults, $defaults); | |
| $order = array_flip($names); | |
| $definitions = array_merge($order, $defaults); | |
| $given = array_slice($names, 0, count($args)); | |
| $args = array_is_assoc($args) ? $args : array_combine($given, $args); | |
| return array_merge($definitions, $args); | |
| } | |
| } | |
| if (!function_exists('get_parameter_definitions')) { | |
| /** | |
| * Extract the parameter names from the target function or method | |
| * | |
| * @param string|string[] $function | |
| * @return array | |
| */ | |
| function get_parameter_definitions($function) | |
| { | |
| list($class, $function) = array_pad((array)$function, -2, null); | |
| $defaults = $parameterNames = []; | |
| try { | |
| $reflection = $class ? new ReflectionMethod($class, $function) : new ReflectionFunction($function); | |
| foreach ($reflection->getParameters() as $param) { | |
| if ($param->isDefaultValueAvailable()) { | |
| $defaults[$param->name] = $param->getDefaultValue(); | |
| } | |
| $parameterNames[] = $param->name; | |
| } | |
| } catch (ReflectionException $e) { | |
| // | |
| } | |
| return [$parameterNames, $defaults]; | |
| } | |
| } | |
| if (!function_exists('get_children_of')) { | |
| /** | |
| * @param $classNameOrObject | |
| * @return string[] | |
| */ | |
| function get_children_of($classNameOrObject) | |
| { | |
| return array_filter(get_declared_classes(), function ($declaredClass) use ($classNameOrObject) { | |
| return is_subclass_of($declaredClass, $classNameOrObject); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment