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\HydratorContainer; |
11
|
|
|
use Thunder\Serializard\NormalizerContainer\NormalizerContainer; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
final class SerializardFacade |
17
|
|
|
{ |
18
|
|
|
/** @var NormalizerContainer */ |
19
|
|
|
private $normalizers; |
20
|
|
|
/** @var HydratorContainer */ |
21
|
|
|
private $hydrators; |
22
|
|
|
/** @var FormatContainer */ |
23
|
|
|
private $formats; |
24
|
|
|
|
25
|
2 |
|
public function __construct() |
26
|
|
|
{ |
27
|
2 |
|
$this->normalizers = new NormalizerContainer(); |
28
|
2 |
|
$this->hydrators = new HydratorContainer(); |
29
|
|
|
|
30
|
2 |
|
$formats = new FormatContainer(); |
31
|
2 |
|
$formats->add('json', new JsonFormat()); |
32
|
2 |
|
$formats->add('array', new ArrayFormat()); |
33
|
2 |
|
$formats->add('yaml', new YamlFormat()); |
34
|
2 |
|
$formats->add('xml', new XmlFormat()); |
35
|
2 |
|
$this->formats = $formats; |
36
|
2 |
|
} |
37
|
|
|
|
38
|
1 |
|
public function addFormat($alias, FormatInterface $format) |
39
|
|
|
{ |
40
|
1 |
|
$this->formats->add($alias, $format); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
1 |
|
public function addNormalizer($class, $root, $handler) |
44
|
|
|
{ |
45
|
1 |
|
$this->normalizers->add($class, $root, $handler); |
46
|
1 |
|
} |
47
|
|
|
|
48
|
1 |
|
public function addHydrator($class, $root, $handler) |
49
|
|
|
{ |
50
|
1 |
|
$this->hydrators->add($class, $root, $handler); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
2 |
|
public function serialize($var, $format) |
54
|
|
|
{ |
55
|
2 |
|
return $this->getFormat($format)->serialize($var, $this->normalizers); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function unserialize($var, $class, $format) |
59
|
|
|
{ |
60
|
1 |
|
return $this->getFormat($format)->unserialize($var, $class, $this->hydrators); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
View Code Duplication |
private function getFormat($alias) |
|
|
|
|
64
|
|
|
{ |
65
|
2 |
|
$format = $this->formats->get($alias); |
66
|
|
|
|
67
|
2 |
|
if(false === $format instanceof FormatInterface) { |
68
|
1 |
|
throw new \RuntimeException(sprintf('No registered format for alias %s!', $alias)); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $format; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.