Repository navigation
Expand file tree
/
Copy pathValueReader.php
More file actions
108 lines (85 loc) · 3.24 KB
/
Copy pathValueReader.php
File metadata and controls
108 lines (85 loc) · 3.24 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
<?php
declare(strict_types=1);
namespace TinyBlocks\Mapper\Internal\Deserialization;
use ReflectionNamedType;
use ReflectionType;
use TinyBlocks\Mapper\Exceptions\UnmappableSource;
use TinyBlocks\Mapper\Internal\Deserialization\Resolvers\ResolverChain;
use TinyBlocks\Mapper\Internal\Deserialization\Resolvers\ResolverFactory;
use TinyBlocks\Mapper\Internal\Engine;
use TinyBlocks\Mapper\Internal\Metadata\ClassDescriptor;
use TinyBlocks\Mapper\Internal\Metadata\Descriptors;
use TinyBlocks\Mapper\Internal\Source;
use TinyBlocks\Mapper\IterableMappable;
use TinyBlocks\Mapper\NamingStrategy;
final readonly class ValueReader
{
private ResolverChain $chain;
private Hydrator $hydrator;
public function __construct(private Engine $engine, NamingStrategy $naming, bool $rejectUnknownKeys)
{
$this->chain = ResolverFactory::build(engine: $engine, reader: $this);
$this->hydrator = new Hydrator(
naming: $naming,
valueReader: $this,
rejectUnknownKeys: $rejectUnknownKeys
);
}
public function read(string $type, mixed $source): object
{
$descriptor = Descriptors::of(type: $type);
if ($descriptor->hasScalarCodec()) {
return $this->chain->resolve(value: $source, descriptor: $descriptor);
}
$normalized = Source::normalize(source: $source);
if (is_a($type, IterableMappable::class, true)) {
return $this->buildIterable(source: $normalized, descriptor: $descriptor);
}
return $this->hydrator->build(source: $normalized, descriptor: $descriptor);
}
public function resolve(?ReflectionType $type, mixed $value): mixed
{
if (!$type instanceof ReflectionNamedType) {
return $value;
}
if ($type->isBuiltin() || (is_null($value) && $type->allowsNull())) {
return $value;
}
return $this->resolveClass(class: $type->getName(), value: $value);
}
private function resolveClass(string $class, mixed $value): object
{
if ($value instanceof $class) {
return $value;
}
if (is_null($value)) {
$template = 'Cannot resolve %s from null.';
throw new UnmappableSource(message: sprintf($template, $class));
}
if ($this->engine->hasMapping(type: $class)) {
return $this->engine->read(type: $class, source: $value);
}
return $this->chain->resolve(value: $value, descriptor: Descriptors::of(type: $class));
}
private function buildIterable(array $source, ClassDescriptor $descriptor): object
{
$type = $descriptor->type;
$elementType = $descriptor->elementType;
$elements = is_null($elementType)
? $source
: array_map(
fn(mixed $element): object => $this->engine->read(type: $elementType, source: $element),
$source
);
return $type::createFrom(elements: $elements);
}
/**
* @template T of object
* @param class-string<T> $type
* @return T
*/
public function reflectionRead(string $type, array $source): object
{
return $this->hydrator->build(source: $source, descriptor: Descriptors::of(type: $type));
}
}