diff --git a/src/Utils/Instance.php b/src/Utils/Instance.php index 7975046..710bda0 100644 --- a/src/Utils/Instance.php +++ b/src/Utils/Instance.php @@ -18,6 +18,8 @@ * Creates object instances. * * @author Mathias Gelhausen + * + * @since 2.x create reflection via array spec */ final class Instance { @@ -51,6 +53,9 @@ public static function reflection($fqcnOrObject): \ReflectionClass * if __$fqcn__ is a string and starts with "!", a \ReflectionClass * object is returned. * + * if __$fqcn__ is an array with the key "!", a \ReflectionClass + * object is created from the value of that key (which must be an FQCN or an object) + * * if __$fqcn__ is an array, the first element is used as FQCN and * all other elements are used as constructor arguments - other * arguments passed in are ignored. @@ -59,12 +64,18 @@ public static function reflection($fqcnOrObject): \ReflectionClass * @param mixed ...$arguments * * @return object + * + * @since 2.x Create reflection via array spec (['!' => FQCN/Object]) */ public static function create($fqcn, ...$arguments): object { if (is_array($fqcn)) { - $arguments = array_slice($fqcn, 1); - $fqcn = reset($fqcn); + if (isset($fqcn['!']) && is_string($fqcn['!'])) { + return self::reflection($fqcn['!']); + } else { + $arguments = array_slice($fqcn, 1); + $fqcn = reset($fqcn); + } } if (!is_string($fqcn)) { diff --git a/test/TestUtilsTest/Utils/InstanceTest.php b/test/TestUtilsTest/Utils/InstanceTest.php index 704e59d..0e61bc4 100644 --- a/test/TestUtilsTest/Utils/InstanceTest.php +++ b/test/TestUtilsTest/Utils/InstanceTest.php @@ -69,7 +69,8 @@ public function __construct(...$args) [$fqcn, [], $fqcn], [$fqcn, ['arg1'], $fqcn, 'arg1'], [$fqcn, ['arg1'], [$fqcn, 'arg1'], 'arg2'], - [\ReflectionClass::class, false, "!$fqcn"] + [\ReflectionClass::class, false, "!$fqcn"], + [\ReflectionClass::class, false, ['!' => $fqcn]], ]; }