Skip to content

Commit 13caf44

Browse files
committed
Merge pull request #10
* pr-10: feat: reflection creation via array spec
2 parents 1015b06 + f3c4abf commit 13caf44

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Utils/Instance.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* Creates object instances.
1919
*
2020
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
21+
*
22+
* @since 2.x create reflection via array spec
2123
*/
2224
final class Instance
2325
{
@@ -51,6 +53,9 @@ public static function reflection($fqcnOrObject): \ReflectionClass
5153
* if __$fqcn__ is a string and starts with "!", a \ReflectionClass
5254
* object is returned.
5355
*
56+
* if __$fqcn__ is an array with the key "!", a \ReflectionClass
57+
* object is created from the value of that key (which must be an FQCN or an object)
58+
*
5459
* if __$fqcn__ is an array, the first element is used as FQCN and
5560
* all other elements are used as constructor arguments - other
5661
* arguments passed in are ignored.
@@ -59,12 +64,18 @@ public static function reflection($fqcnOrObject): \ReflectionClass
5964
* @param mixed ...$arguments
6065
*
6166
* @return object
67+
*
68+
* @since 2.x Create reflection via array spec (['!' => FQCN/Object])
6269
*/
6370
public static function create($fqcn, ...$arguments): object
6471
{
6572
if (is_array($fqcn)) {
66-
$arguments = array_slice($fqcn, 1);
67-
$fqcn = reset($fqcn);
73+
if (isset($fqcn['!']) && is_string($fqcn['!'])) {
74+
return self::reflection($fqcn['!']);
75+
} else {
76+
$arguments = array_slice($fqcn, 1);
77+
$fqcn = reset($fqcn);
78+
}
6879
}
6980

7081
if (!is_string($fqcn)) {

test/TestUtilsTest/Utils/InstanceTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public function __construct(...$args)
6969
[$fqcn, [], $fqcn],
7070
[$fqcn, ['arg1'], $fqcn, 'arg1'],
7171
[$fqcn, ['arg1'], [$fqcn, 'arg1'], 'arg2'],
72-
[\ReflectionClass::class, false, "!$fqcn"]
72+
[\ReflectionClass::class, false, "!$fqcn"],
73+
[\ReflectionClass::class, false, ['!' => $fqcn]],
7374
];
7475
}
7576

0 commit comments

Comments
 (0)