Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Utils/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* Creates object instances.
*
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
*
* @since 2.x create reflection via array spec
*/
final class Instance
{
Expand Down Expand Up @@ -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.
Expand All @@ -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)) {
Expand Down
3 changes: 2 additions & 1 deletion test/TestUtilsTest/Utils/InstanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
];
}

Expand Down