|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bangpound\oEmbed; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
|
6
|
|
|
use Symfony\Component\Serializer\Encoder\XmlEncoder; |
|
7
|
|
|
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
|
8
|
|
|
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; |
|
9
|
|
|
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; |
|
10
|
|
|
use Symfony\Component\Serializer\Serializer as SymfonySerializer; |
|
11
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
12
|
|
|
|
|
13
|
|
|
class Serializer implements SerializerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var SymfonySerializer |
|
17
|
|
|
*/ |
|
18
|
|
|
private $serializer; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $map = array( |
|
24
|
|
|
'video' => 'Bangpound\\oEmbed\\Response\\VideoResponse', |
|
25
|
|
|
'photo' => 'Bangpound\\oEmbed\\Response\\PhotoResponse', |
|
26
|
|
|
'link' => 'Bangpound\\oEmbed\\Response\\LinkResponse', |
|
27
|
|
|
'rich' => 'Bangpound\\oEmbed\\Response\\RichResponse', |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
28 |
|
public function __construct(SymfonySerializer $serializer, array $map = null) |
|
31
|
|
|
{ |
|
32
|
28 |
|
$this->serializer = $serializer; |
|
33
|
28 |
|
if (isset($map)) { |
|
34
|
8 |
|
$this->map = $map; |
|
35
|
10 |
|
} |
|
36
|
28 |
|
} |
|
37
|
|
|
|
|
38
|
24 |
|
public function deserialize($data, $type, $format, array $context = array()) |
|
39
|
|
|
{ |
|
40
|
24 |
|
if (!$type) { |
|
41
|
8 |
|
$temp = $this->serializer->decode($data, $format, $context); |
|
42
|
8 |
|
$type = isset($this->map[$temp['type']]) ? $this->map[$temp['type']] : 'Bangpound\\oEmbed\\Response\\Response'; |
|
43
|
8 |
|
} |
|
44
|
|
|
|
|
45
|
24 |
|
return $this->serializer->deserialize($data, $type, $format, $context); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
16 |
|
public function serialize($data, $format, array $context = array()) |
|
49
|
|
|
{ |
|
50
|
16 |
|
return $this->serializer->serialize($data, $format, $context); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
28 |
|
public static function create(array $map = null) |
|
54
|
|
|
{ |
|
55
|
28 |
|
$nameConverter = new CamelCaseToSnakeCaseNameConverter(); |
|
56
|
28 |
|
$serializer = new SymfonySerializer([ |
|
57
|
28 |
|
new PropertyNormalizer(null, $nameConverter), |
|
58
|
28 |
|
new GetSetMethodNormalizer(null, $nameConverter), |
|
59
|
28 |
|
], [ |
|
60
|
28 |
|
new JsonEncoder(), |
|
61
|
28 |
|
new XmlEncoder('oembed'), |
|
62
|
28 |
|
]); |
|
63
|
|
|
|
|
64
|
28 |
|
return new self($serializer, $map); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|