forked from Crell/Serde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deserializer.php
47 lines (39 loc) · 1.25 KB
/
Deserializer.php
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
<?php
declare(strict_types=1);
namespace Crell\Serde;
use Crell\AttributeUtils\ClassAnalyzer;
use Crell\Serde\Attributes\Field;
use Crell\Serde\Formatter\Deformatter;
use Crell\Serde\PropertyHandler\Importer;
// This exists mainly just to create a closure over the format and formatter.
// But that does simplify a number of functions.
class Deserializer
{
/**
* @param Importer[] $importers
* @param array<string|null> $scopes
*/
public function __construct(
public readonly ClassAnalyzer $analyzer,
protected readonly array $importers,
public readonly Deformatter $deformatter,
public readonly TypeMapper $typeMapper,
public readonly array $scopes = [],
) {}
public function deserialize(mixed $decoded, Field $field): mixed
{
$writer = $this->findImporter($field);
$result = $writer->importValue($this, $field, $decoded);
return $result;
}
protected function findImporter(Field $field): Importer
{
$format = $this->deformatter->format();
foreach ($this->importers as $w) {
if ($w->canImport($field, $format)) {
return $w;
}
}
throw NoImporterFound::create($field->phpType, $format);
}
}