|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chubbyphp\Serialization; |
|
6
|
|
|
|
|
7
|
|
|
use Chubbyphp\Serialization\Encoder\EncoderInterface; |
|
8
|
|
|
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface; |
|
9
|
|
|
use Chubbyphp\Serialization\Normalizer\NormalizerInterface; |
|
10
|
|
|
|
|
11
|
|
|
final class Serializer implements SerializerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var NormalizerInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private $normalizer; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var EncoderInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $encoder; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param NormalizerInterface $normalizer |
|
25
|
|
|
* @param EncoderInterface $encoder |
|
26
|
|
|
*/ |
|
27
|
4 |
|
public function __construct(NormalizerInterface $normalizer, EncoderInterface $encoder) |
|
28
|
|
|
{ |
|
29
|
4 |
|
$this->normalizer = $normalizer; |
|
30
|
4 |
|
$this->encoder = $encoder; |
|
31
|
4 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param object $object |
|
35
|
|
|
* @param string $contentType |
|
36
|
|
|
* @param NormalizerContextInterface $context |
|
37
|
|
|
* @param string $path |
|
38
|
|
|
* |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function serialize( |
|
42
|
|
|
$object, |
|
43
|
|
|
string $contentType, |
|
44
|
|
|
NormalizerContextInterface $context = null, |
|
45
|
|
|
$path = '' |
|
46
|
|
|
): string { |
|
47
|
1 |
|
return $this->encoder->encode($this->normalizer->normalize($object, $context, $path), $contentType); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param object $object |
|
52
|
|
|
* @param NormalizerContextInterface|null $context |
|
53
|
|
|
* @param string $path |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
* |
|
57
|
|
|
* @throws SerializerLogicException |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function normalize($object, NormalizerContextInterface $context = null, string $path = ''): array |
|
60
|
|
|
{ |
|
61
|
1 |
|
return $this->normalizer->normalize($object, $context, $path); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string[] |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function getContentTypes(): array |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->encoder->getContentTypes(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $data |
|
74
|
|
|
* @param string $contentType |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
* |
|
78
|
|
|
* @throws SerializerLogicException |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function encode(array $data, string $contentType): string |
|
81
|
|
|
{ |
|
82
|
1 |
|
return $this->encoder->encode($data, $contentType); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|