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