|
1
|
|
|
<?php |
|
2
|
|
|
namespace Thunder\Serializard; |
|
3
|
|
|
|
|
4
|
|
|
use Thunder\Serializard\Format\ArrayFormat; |
|
5
|
|
|
use Thunder\Serializard\Format\FormatInterface; |
|
6
|
|
|
use Thunder\Serializard\Format\JsonFormat; |
|
7
|
|
|
use Thunder\Serializard\Format\XmlFormat; |
|
8
|
|
|
use Thunder\Serializard\Format\YamlFormat; |
|
9
|
|
|
use Thunder\Serializard\FormatContainer\FormatContainer; |
|
10
|
|
|
use Thunder\Serializard\HydratorContainer\FallbackHydratorContainer; |
|
11
|
|
|
use Thunder\Serializard\NormalizerContainer\FallbackNormalizerContainer; |
|
12
|
|
|
use Thunder\Serializard\NormalizerContext\ParentNormalizerContext; |
|
13
|
|
|
use Thunder\Serializard\Utility\RootElementProviderUtility; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
final class SerializardFacade |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var FallbackNormalizerContainer */ |
|
21
|
|
|
private $normalizers; |
|
22
|
|
|
/** @var FallbackHydratorContainer */ |
|
23
|
|
|
private $hydrators; |
|
24
|
|
|
/** @var FormatContainer */ |
|
25
|
|
|
private $formats; |
|
26
|
|
|
|
|
27
|
2 |
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
2 |
|
$this->normalizers = new FallbackNormalizerContainer(); |
|
30
|
2 |
|
$this->hydrators = new FallbackHydratorContainer(); |
|
31
|
|
|
|
|
32
|
2 |
|
$formats = new FormatContainer(); |
|
33
|
2 |
|
$formats->add('json', new JsonFormat()); |
|
34
|
2 |
|
$formats->add('array', new ArrayFormat()); |
|
35
|
2 |
|
$formats->add('yaml', new YamlFormat()); |
|
36
|
2 |
|
$formats->add('xml', new XmlFormat(new RootElementProviderUtility([]))); |
|
37
|
2 |
|
$this->formats = $formats; |
|
38
|
2 |
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function addFormat($alias, FormatInterface $format) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->formats->add($alias, $format); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function addNormalizer($class, $handler) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->normalizers->add($class, $handler); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function addHydrator($class, $handler) |
|
51
|
|
|
{ |
|
52
|
1 |
|
$this->hydrators->add($class, $handler); |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function serialize($var, $format) |
|
56
|
|
|
{ |
|
57
|
2 |
|
return $this->formats->get($format)->serialize($var, $this->normalizers, new ParentNormalizerContext()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public function unserialize($var, $class, $format) |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->formats->get($format)->unserialize($var, $class, $this->hydrators); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|