1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Twitter\Serializer; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use Twitter\Object\TwitterEntities; |
7
|
|
|
use Twitter\TwitterSerializable; |
8
|
|
|
use Twitter\TwitterSerializer; |
9
|
|
|
|
10
|
|
|
class TwitterEntitiesSerializer implements TwitterSerializer |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var TwitterHashtagSerializer |
14
|
|
|
*/ |
15
|
|
|
private $hashtagSerializer; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var TwitterSymbolSerializer |
19
|
|
|
*/ |
20
|
|
|
private $symbolSerializer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var TwitterUrlSerializer |
24
|
|
|
*/ |
25
|
|
|
private $urlSerializer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var TwitterUserMentionSerializer |
29
|
|
|
*/ |
30
|
|
|
private $userMentionSerializer; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var TwitterMediaSerializer |
34
|
|
|
*/ |
35
|
|
|
private $mediaSerializer; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var TwitterExtendedEntitySerializer |
39
|
|
|
*/ |
40
|
|
|
private $extendedEntitySerializer; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param TwitterExtendedEntitySerializer $extendedEntitySerializer |
44
|
|
|
* @param TwitterHashtagSerializer $hashtagSerializer |
45
|
|
|
* @param TwitterMediaSerializer $mediaSerializer |
46
|
|
|
* @param TwitterSymbolSerializer $symbolSerializer |
47
|
|
|
* @param TwitterUrlSerializer $urlSerializer |
48
|
|
|
* @param TwitterUserMentionSerializer $userMentionSerializer |
49
|
30 |
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
TwitterExtendedEntitySerializer $extendedEntitySerializer, |
52
|
|
|
TwitterHashtagSerializer $hashtagSerializer, |
53
|
|
|
TwitterMediaSerializer $mediaSerializer, |
54
|
|
|
TwitterSymbolSerializer $symbolSerializer, |
55
|
|
|
TwitterUrlSerializer $urlSerializer, |
56
|
|
|
TwitterUserMentionSerializer $userMentionSerializer |
57
|
30 |
|
) { |
58
|
30 |
|
$this->extendedEntitySerializer = $extendedEntitySerializer; |
59
|
30 |
|
$this->hashtagSerializer = $hashtagSerializer; |
60
|
30 |
|
$this->mediaSerializer = $mediaSerializer; |
61
|
30 |
|
$this->symbolSerializer = $symbolSerializer; |
62
|
30 |
|
$this->urlSerializer = $urlSerializer; |
63
|
30 |
|
$this->userMentionSerializer = $userMentionSerializer; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param TwitterSerializable $object |
68
|
|
|
* @return \stdClass |
69
|
6 |
|
*/ |
70
|
|
|
public function serialize(TwitterSerializable $object) |
71
|
6 |
|
{ |
72
|
3 |
|
/* @var TwitterEntities $object */ |
73
|
|
|
Assertion::true($this->canSerialize($object), 'object must be an instance of TwitterEntities'); |
74
|
|
|
|
75
|
|
|
$entities = new \stdClass(); |
76
|
3 |
|
|
77
|
|
|
// Hashtags |
78
|
|
|
if ($object->getHashtags()) { |
79
|
3 |
|
$entities->hashtags = []; |
80
|
3 |
|
foreach ($object->getHashtags() as $hashtag) { |
81
|
3 |
|
$entities->hashtags[] = $this->hashtagSerializer->serialize($hashtag); |
82
|
3 |
|
} |
83
|
2 |
|
} |
84
|
2 |
|
|
85
|
|
|
// Symbols |
86
|
|
|
if ($object->getSymbols()) { |
87
|
3 |
|
$entities->symbols = []; |
88
|
3 |
|
foreach ($object->getSymbols() as $symbol) { |
89
|
3 |
|
$entities->symbols[] = $this->symbolSerializer->serialize($symbol); |
90
|
3 |
|
} |
91
|
2 |
|
} |
92
|
2 |
|
|
93
|
|
|
// Urls |
94
|
|
|
if ($object->getUrls()) { |
95
|
3 |
|
$entities->urls = []; |
96
|
3 |
|
foreach ($object->getUrls() as $url) { |
97
|
3 |
|
$entities->urls[] = $this->urlSerializer->serialize($url); |
98
|
3 |
|
} |
99
|
2 |
|
} |
100
|
2 |
|
|
101
|
|
|
// User mentions |
102
|
|
|
if ($object->getUserMentions()) { |
103
|
3 |
|
$entities->user_mentions = []; |
104
|
3 |
|
foreach ($object->getUserMentions() as $userMention) { |
105
|
3 |
|
$entities->user_mentions[] = $this->userMentionSerializer->serialize($userMention); |
106
|
3 |
|
} |
107
|
2 |
|
} |
108
|
2 |
|
|
109
|
|
|
// Media |
110
|
|
|
if ($object->getMedia()) { |
111
|
3 |
|
$entities->media = []; |
112
|
3 |
|
foreach ($object->getMedia() as $media) { |
113
|
3 |
|
$entities->media[] = $this->mediaSerializer->serialize($media); |
114
|
3 |
|
} |
115
|
2 |
|
} |
116
|
2 |
|
|
117
|
|
|
// Extended entities |
118
|
|
|
if ($object->getExtendedEntities()) { |
119
|
3 |
|
$entities->extended_entities = []; |
120
|
3 |
|
foreach ($object->getExtendedEntities() as $extendedEntity) { |
121
|
3 |
|
$entities->extended_entities[] = $this->extendedEntitySerializer->serialize($extendedEntity); |
122
|
3 |
|
} |
123
|
2 |
|
} |
124
|
2 |
|
|
125
|
|
|
return $entities; |
126
|
3 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param \stdClass $obj |
130
|
|
|
* @param array $context |
131
|
|
|
* @return TwitterEntities |
132
|
|
|
*/ |
133
|
|
|
public function unserialize($obj, array $context = []) |
134
|
6 |
|
{ |
135
|
|
|
Assertion::true($this->canUnserialize($obj), 'object is not unserializable'); |
136
|
6 |
|
|
137
|
3 |
|
// Hashtags |
138
|
|
|
$hashtags = []; |
139
|
|
|
if (isset($obj->hashtags)) { |
140
|
|
|
foreach ($obj->hashtags as $hashtag) { |
141
|
3 |
|
$hashtags[] = $this->hashtagSerializer->unserialize($hashtag); |
142
|
3 |
|
} |
143
|
3 |
|
} |
144
|
3 |
|
|
145
|
2 |
|
// Symbols |
146
|
2 |
|
$symbols = []; |
147
|
|
|
if (isset($obj->symbols)) { |
148
|
|
|
foreach ($obj->symbols as $symbol) { |
149
|
5 |
|
$symbols[] = $this->symbolSerializer->unserialize($symbol); |
150
|
3 |
|
} |
151
|
3 |
|
} |
152
|
3 |
|
|
153
|
2 |
|
// URLs |
154
|
2 |
|
$urls = []; |
155
|
|
|
if (isset($obj->urls)) { |
156
|
|
|
foreach ($obj->urls as $url) { |
157
|
3 |
|
$urls[] = $this->urlSerializer->unserialize($url); |
158
|
3 |
|
} |
159
|
3 |
|
} |
160
|
3 |
|
|
161
|
2 |
|
// User mentions |
162
|
2 |
|
$userMentions = []; |
163
|
|
|
if (isset($obj->user_mentions)) { |
164
|
|
|
foreach ($obj->user_mentions as $userMention) { |
165
|
3 |
|
$userMentions[] = $this->userMentionSerializer->unserialize($userMention); |
166
|
3 |
|
} |
167
|
3 |
|
} |
168
|
3 |
|
|
169
|
2 |
|
// Media |
170
|
2 |
|
$media = []; |
171
|
|
|
if (isset($obj->media)) { |
172
|
|
|
foreach ($obj->media as $medium) { |
173
|
3 |
|
$media[] = $this->mediaSerializer->unserialize($medium); |
174
|
3 |
|
} |
175
|
3 |
|
} |
176
|
3 |
|
|
177
|
2 |
|
// Extended entities |
178
|
2 |
|
$extendedEntities = []; |
179
|
|
|
if (isset($obj->extended_entities)) { |
180
|
|
|
foreach ($obj->extended_entities as $extendedEntity) { |
181
|
3 |
|
$extendedEntities[] = $this->extendedEntitySerializer->unserialize($extendedEntity); |
182
|
3 |
|
} |
183
|
3 |
|
} |
184
|
3 |
|
|
185
|
2 |
|
return TwitterEntities::create($hashtags, $userMentions, $urls, $media, $symbols, $extendedEntities); |
186
|
2 |
|
} |
187
|
|
|
|
188
|
3 |
|
/** |
189
|
|
|
* @param TwitterSerializable $object |
190
|
|
|
* @return boolean |
191
|
|
|
*/ |
192
|
|
|
public function canSerialize(TwitterSerializable $object) |
193
|
|
|
{ |
194
|
|
|
return $object instanceof TwitterEntities; |
195
|
6 |
|
} |
196
|
|
|
|
197
|
6 |
|
/** |
198
|
|
|
* @param \stdClass $object |
199
|
|
|
* @return boolean |
200
|
|
|
*/ |
201
|
|
|
public function canUnserialize($object) |
202
|
|
|
{ |
203
|
|
|
return isset($object->hashtags) || isset($object->symbols) || isset($object->urls) || |
204
|
6 |
|
isset($object->user_mentions) || isset($object->media) || isset($object->extended_entities); |
205
|
|
|
} |
206
|
6 |
|
|
207
|
6 |
|
/** |
208
|
|
|
* @return TwitterEntitiesSerializer |
209
|
|
|
*/ |
210
|
|
|
public static function build() |
211
|
|
|
{ |
212
|
|
|
return new self( |
213
|
18 |
|
TwitterExtendedEntitySerializer::build(), |
214
|
|
|
TwitterHashtagSerializer::build(), |
215
|
18 |
|
TwitterMediaSerializer::build(), |
216
|
18 |
|
TwitterSymbolSerializer::build(), |
217
|
18 |
|
TwitterUrlSerializer::build(), |
218
|
18 |
|
TwitterUserMentionSerializer::build() |
219
|
18 |
|
); |
220
|
18 |
|
} |
221
|
|
|
} |
222
|
|
|
|