-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflector.php
More file actions
49 lines (37 loc) · 1.32 KB
/
Reflector.php
File metadata and controls
49 lines (37 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
namespace TinyBlocks\Mapper\Internal\Mappers\Object;
use ReflectionClass;
use ReflectionMethod;
final readonly class Reflector
{
private ?ReflectionMethod $constructor;
private array $parameters;
private function __construct(private ReflectionClass $reflectionClass)
{
$this->constructor = $reflectionClass->getConstructor();
$this->parameters = $this->constructor ? $this->constructor->getParameters() : [];
}
public static function reflectFrom(string $class): Reflector
{
return new Reflector(reflectionClass: new ReflectionClass($class));
}
public function getParameters(): array
{
return $this->parameters;
}
public function newInstance(array $constructorArguments): object
{
$instance = $this->constructor && $this->constructor->isPrivate()
? $this->newInstanceWithoutConstructor()
: $this->reflectionClass->newInstanceArgs($constructorArguments);
if ($this->constructor && $this->constructor->isPrivate()) {
$this->constructor->invokeArgs($instance, $constructorArguments);
}
return $instance;
}
public function newInstanceWithoutConstructor(): object
{
return $this->reflectionClass->newInstanceWithoutConstructor();
}
}