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