-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonEncoder.php
97 lines (83 loc) · 3.37 KB
/
JsonEncoder.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
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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\JsonEncoder;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use Psr\Container\ContainerInterface;
use Symfony\Component\JsonEncoder\Encode\EncoderGenerator;
use Symfony\Component\JsonEncoder\Encode\Normalizer\DateTimeNormalizer;
use Symfony\Component\JsonEncoder\Encode\Normalizer\NormalizerInterface;
use Symfony\Component\JsonEncoder\Mapping\Encode\AttributePropertyMetadataLoader;
use Symfony\Component\JsonEncoder\Mapping\Encode\DateTimeTypePropertyMetadataLoader;
use Symfony\Component\JsonEncoder\Mapping\GenericTypePropertyMetadataLoader;
use Symfony\Component\JsonEncoder\Mapping\PropertyMetadataLoader;
use Symfony\Component\JsonEncoder\Mapping\PropertyMetadataLoaderInterface;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory;
use Symfony\Component\TypeInfo\TypeResolver\StringTypeResolver;
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
/**
* @author Mathias Arlaud <[email protected]>
*
* @implements EncoderInterface<array<string, mixed>>
*
* @experimental
*/
final class JsonEncoder implements EncoderInterface
{
private EncoderGenerator $encoderGenerator;
public function __construct(
private ContainerInterface $normalizers,
PropertyMetadataLoaderInterface $propertyMetadataLoader,
string $encodersDir,
) {
$this->encoderGenerator = new EncoderGenerator($propertyMetadataLoader, $encodersDir);
}
public function encode(mixed $data, Type $type, array $options = []): \Traversable&\Stringable
{
$path = $this->encoderGenerator->generate($type, $options);
return new Encoded((require $path)($data, $this->normalizers, $options));
}
/**
* @param array<string, NormalizerInterface> $normalizers
*/
public static function create(array $normalizers = [], ?string $encodersDir = null): self
{
$encodersDir ??= sys_get_temp_dir().'/json_encoder/encoder';
$normalizers += [
'json_encoder.normalizer.date_time' => new DateTimeNormalizer(),
];
$normalizersContainer = new class($normalizers) implements ContainerInterface {
public function __construct(
private array $normalizers,
) {
}
public function has(string $id): bool
{
return isset($this->normalizers[$id]);
}
public function get(string $id): NormalizerInterface
{
return $this->normalizers[$id];
}
};
$typeContextFactory = new TypeContextFactory(class_exists(PhpDocParser::class) ? new StringTypeResolver() : null);
$propertyMetadataLoader = new GenericTypePropertyMetadataLoader(
new DateTimeTypePropertyMetadataLoader(
new AttributePropertyMetadataLoader(
new PropertyMetadataLoader(TypeResolver::create()),
$normalizersContainer,
TypeResolver::create(),
),
),
$typeContextFactory,
);
return new self($normalizersContainer, $propertyMetadataLoader, $encodersDir);
}
}