Repository navigation
Expand file tree
/
Copy pathGenericDomainObjectFactory.php
More file actions
125 lines (104 loc) · 3.51 KB
/
Copy pathGenericDomainObjectFactory.php
File metadata and controls
125 lines (104 loc) · 3.51 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace MsgPhp\Domain\Factory;
use MsgPhp\Domain\Exception\InvalidClass;
use MsgPhp\Domain\Infrastructure\Uid\DomainUuid;
use Symfony\Component\VarExporter\Exception\ClassNotFoundException;
use Symfony\Component\VarExporter\Instantiator;
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class GenericDomainObjectFactory implements DomainObjectFactory
{
/** @var array<class-string, class-string> */
private $classMapping;
/** @var null|DomainObjectFactory */
private $factory;
/**
* @param array<class-string, class-string> $classMapping
*/
public function __construct(array $classMapping = [])
{
$this->classMapping = $classMapping;
}
public function setNestedFactory(?DomainObjectFactory $factory): void
{
$this->factory = $factory;
}
/**
* @template T
*
* @param class-string<T> $class
*
* @return T
*/
public function create(string $class, array $context = []): object
{
$class = $this->getClass($class, $context);
if (is_a($class, DomainUuid::class, true) && \array_key_exists('value', $context)) {
/** @var T */
return $class::fromString(...$this->resolveArguments($class, 'fromString', $context));
}
if (!class_exists($class)) {
throw InvalidClass::create($class);
}
/** @var T */
return new $class(...$this->resolveArguments($class, '__construct', $context));
}
/**
* @template T
*
* @param class-string<T> $class
*
* @return T
*/
public function reference(string $class, array $context = []): object
{
\assert(class_exists(Instantiator::class), new \LogicException('Method "'.__METHOD__.'()" requires "symfony/var-exporter".'));
$class = $this->getClass($class, $context);
$properties = [];
foreach ($context as $key => $value) {
if (property_exists($class, $key)) {
$properties[$key] = $value;
continue;
}
}
try {
/** @var T */
return Instantiator::instantiate($class, $properties);
} catch (ClassNotFoundException $e) {
throw InvalidClass::create($class);
}
}
public function getClass(string $class, array $context = []): string
{
return $this->classMapping[$class] ?? $class;
}
/**
* @param class-string $class
*
* @return array<int, mixed>
*/
private function resolveArguments(string $class, string $method, array $context): array
{
$arguments = [];
foreach (ClassMethodResolver::resolve($class, $method) as $argument => $metadata) {
if (\array_key_exists($argument, $context)) {
$given = true;
$value = $context[$argument];
} elseif (!$metadata['required']) {
$given = false;
$value = $metadata['default'];
} else {
throw new \LogicException('No value available for argument $'.$argument.' in class method "'.$class.'::'.$method.'()".');
}
$type = $metadata['type'];
if ($given && !\is_object($value) && (class_exists($type) || interface_exists($type, false))) {
$arguments[] = ($this->factory ?? $this)->create($type, (array) $value);
continue;
}
$arguments[] = $value;
}
return $arguments;
}
}