|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Twitter\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use Assert\Assertion; |
|
6
|
|
|
use Twitter\Object\TwitterExtendedEntity; |
|
7
|
|
|
use Twitter\TwitterSerializable; |
|
8
|
|
|
use Twitter\TwitterSerializer; |
|
9
|
|
|
|
|
10
|
|
|
class TwitterExtendedEntitySerializer implements TwitterSerializer |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var TwitterEntityIndicesSerializer |
|
14
|
|
|
*/ |
|
15
|
|
|
private $entityIndicesSerializer; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var TwitterMediaSizeSerializer |
|
19
|
|
|
*/ |
|
20
|
|
|
private $mediaSizeSerializer; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var TwitterVariantMediaSerializer |
|
24
|
|
|
*/ |
|
25
|
|
|
private $variantMediaSerializer; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Constructor |
|
29
|
|
|
* |
|
30
|
|
|
* @param TwitterEntityIndicesSerializer $entityIndicesSerializer |
|
31
|
|
|
* @param TwitterMediaSizeSerializer $mediaSizeSerializer |
|
32
|
|
|
* @param TwitterVariantMediaSerializer $variantMediaSerializer |
|
33
|
|
|
*/ |
|
34
|
33 |
|
public function __construct( |
|
35
|
|
|
TwitterEntityIndicesSerializer $entityIndicesSerializer, |
|
36
|
|
|
TwitterMediaSizeSerializer $mediaSizeSerializer, |
|
37
|
|
|
TwitterVariantMediaSerializer $variantMediaSerializer |
|
38
|
|
|
) { |
|
39
|
33 |
|
$this->entityIndicesSerializer = $entityIndicesSerializer; |
|
40
|
33 |
|
$this->mediaSizeSerializer = $mediaSizeSerializer; |
|
41
|
33 |
|
$this->variantMediaSerializer = $variantMediaSerializer; |
|
42
|
33 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param TwitterSerializable $object |
|
46
|
|
|
* @return \stdClass |
|
47
|
|
|
*/ |
|
48
|
6 |
|
public function serialize(TwitterSerializable $object) |
|
49
|
|
|
{ |
|
50
|
|
|
/* @var TwitterExtendedEntity $object */ |
|
51
|
6 |
|
Assertion::true($this->canSerialize($object), 'object must be an instance of TwitterExtendedEntity'); |
|
52
|
|
|
|
|
53
|
3 |
|
$extendedEntity = new \stdClass(); |
|
54
|
3 |
|
$extendedEntity->id = $object->getId(); |
|
55
|
3 |
|
$extendedEntity->media_url = $object->getMediaUrl(); |
|
56
|
3 |
|
$extendedEntity->media_url_https = $object->getMediaUrlHttps(); |
|
57
|
3 |
|
$extendedEntity->url = $object->getUrl(); |
|
58
|
3 |
|
$extendedEntity->display_url = $object->getDisplayUrl(); |
|
59
|
3 |
|
$extendedEntity->expanded_url = $object->getExpandedUrl(); |
|
60
|
3 |
|
$extendedEntity->type = $object->getType(); |
|
61
|
3 |
|
$extendedEntity->video_info = $object->getVideoInfo(); |
|
62
|
3 |
|
$extendedEntity->duration_millis = $object->getDurationMillis(); |
|
63
|
3 |
|
$extendedEntity->indices = $this->entityIndicesSerializer->serialize($object->getIndices()); |
|
64
|
|
|
|
|
65
|
3 |
|
$extendedEntity->sizes = []; |
|
66
|
3 |
|
foreach ($object->getSizes() as $size) { |
|
67
|
3 |
|
$extendedEntity->sizes[$size->getName()] = $this->mediaSizeSerializer->serialize($size); |
|
68
|
2 |
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
$extendedEntity->variants = []; |
|
71
|
3 |
|
foreach ($object->getVariants() as $variant) { |
|
72
|
3 |
|
$extendedEntity->variants[] = $this->variantMediaSerializer->serialize($variant); |
|
73
|
2 |
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
return $extendedEntity; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param \stdClass $obj |
|
80
|
|
|
* @param array $context |
|
81
|
|
|
* @return TwitterExtendedEntity |
|
82
|
|
|
*/ |
|
83
|
6 |
|
public function unserialize($obj, array $context = []) |
|
84
|
|
|
{ |
|
85
|
6 |
|
Assertion::true($this->canUnserialize($obj), 'object is not unserializable'); |
|
86
|
|
|
|
|
87
|
3 |
|
$sizesObjects = []; |
|
88
|
3 |
|
if ($obj->sizes) { |
|
89
|
3 |
|
foreach ($obj->sizes as $sizeName => $sizeObj) { |
|
90
|
3 |
|
$sizesObjects[$sizeName] = $this->mediaSizeSerializer->unserialize( |
|
91
|
3 |
|
$sizeObj, |
|
92
|
3 |
|
[TwitterMediaSizeSerializer::NAME_VAR => $sizeName] |
|
93
|
2 |
|
); |
|
94
|
2 |
|
} |
|
95
|
2 |
|
} |
|
96
|
|
|
|
|
97
|
3 |
|
$variantObjects = []; |
|
98
|
3 |
|
if ($obj->variants) { |
|
99
|
3 |
|
foreach ($obj->variants as $variant) { |
|
100
|
3 |
|
$variantObjects[] = $this->variantMediaSerializer->unserialize($variant); |
|
101
|
2 |
|
} |
|
102
|
2 |
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
return TwitterExtendedEntity::create( |
|
105
|
3 |
|
$obj->id, |
|
106
|
3 |
|
$obj->media_url, |
|
107
|
3 |
|
$obj->media_url_https, |
|
108
|
3 |
|
$obj->url, |
|
109
|
3 |
|
$obj->display_url, |
|
110
|
3 |
|
$obj->expanded_url, |
|
111
|
3 |
|
$sizesObjects, |
|
112
|
3 |
|
$obj->type, |
|
113
|
3 |
|
$obj->video_info, |
|
114
|
3 |
|
$obj->duration_millis, |
|
115
|
3 |
|
$variantObjects, |
|
116
|
3 |
|
$this->entityIndicesSerializer->unserialize($obj->indices) |
|
117
|
2 |
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param TwitterSerializable $object |
|
122
|
|
|
* @return boolean |
|
123
|
|
|
*/ |
|
124
|
6 |
|
public function canSerialize(TwitterSerializable $object) |
|
125
|
|
|
{ |
|
126
|
6 |
|
return $object instanceof TwitterExtendedEntity; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param \stdClass $object |
|
131
|
|
|
* @return boolean |
|
132
|
|
|
*/ |
|
133
|
6 |
|
public function canUnserialize($object) |
|
134
|
|
|
{ |
|
135
|
6 |
|
return isset($object->duration_millis); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return TwitterExtendedEntitySerializer |
|
140
|
|
|
*/ |
|
141
|
21 |
|
public static function build() |
|
142
|
|
|
{ |
|
143
|
21 |
|
return new self( |
|
144
|
21 |
|
TwitterEntityIndicesSerializer::build(), |
|
145
|
21 |
|
TwitterMediaSizeSerializer::build(), |
|
146
|
21 |
|
TwitterVariantMediaSerializer::build() |
|
147
|
14 |
|
); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|