1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Twitter\Serializer; |
4
|
|
|
|
5
|
|
|
use Twitter\Object\Tweet; |
6
|
|
|
use Twitter\Object\TwitterDate; |
7
|
|
|
use Twitter\TwitterMessageId; |
8
|
|
|
use Twitter\TwitterSerializable; |
9
|
|
|
use Twitter\TwitterSerializer; |
10
|
|
|
|
11
|
|
|
class TweetSerializer implements TwitterSerializer |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var TwitterUserSerializer |
15
|
|
|
*/ |
16
|
|
|
private $userSerializer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var TwitterEntitiesSerializer |
20
|
|
|
*/ |
21
|
|
|
private $twitterEntitiesSerializer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var TwitterCoordinatesSerializer |
25
|
|
|
*/ |
26
|
|
|
private $coordinatesSerializer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TwitterPlaceSerializer |
30
|
|
|
*/ |
31
|
|
|
private $placeSerializer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param TwitterUserSerializer $userSerializer |
35
|
|
|
* @param TwitterEntitiesSerializer $twitterEntitiesSerializer |
36
|
|
|
* @param TwitterCoordinatesSerializer $coordinatesSerializer |
37
|
|
|
* @param TwitterPlaceSerializer $placeSerializer |
38
|
|
|
*/ |
39
|
21 |
|
public function __construct( |
40
|
|
|
TwitterUserSerializer $userSerializer, |
41
|
|
|
TwitterEntitiesSerializer $twitterEntitiesSerializer, |
42
|
|
|
TwitterCoordinatesSerializer $coordinatesSerializer, |
43
|
|
|
TwitterPlaceSerializer $placeSerializer |
44
|
|
|
) { |
45
|
21 |
|
$this->userSerializer = $userSerializer; |
46
|
21 |
|
$this->twitterEntitiesSerializer = $twitterEntitiesSerializer; |
47
|
21 |
|
$this->coordinatesSerializer = $coordinatesSerializer; |
48
|
21 |
|
$this->placeSerializer = $placeSerializer; |
49
|
21 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param TwitterSerializable $object |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
6 |
|
public function serialize(TwitterSerializable $object) |
56
|
|
|
{ |
57
|
6 |
|
if (!$this->canSerialize($object)) { |
58
|
3 |
|
throw new \InvalidArgumentException('$object must be an instance of Tweet'); |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
$tweet = new \stdClass(); |
62
|
3 |
|
$tweet->id = (string) $object->getId(); |
|
|
|
|
63
|
3 |
|
$tweet->user = $this->userSerializer->serialize($object->getSender()); |
|
|
|
|
64
|
3 |
|
$tweet->text = $object->getText(); |
|
|
|
|
65
|
3 |
|
$tweet->lang = $object->getLang(); |
|
|
|
|
66
|
3 |
|
$tweet->created_at = $object->getDate()->setTimezone(new \DateTimeZone('UTC'))->format(TwitterDate::FORMAT); |
|
|
|
|
67
|
3 |
|
$tweet->entities = $object->getEntities()? |
|
|
|
|
68
|
3 |
|
$this->twitterEntitiesSerializer->serialize($object->getEntities()): |
|
|
|
|
69
|
3 |
|
[]; |
70
|
3 |
|
$tweet->coordinates = $object->getCoordinates()? |
|
|
|
|
71
|
3 |
|
$this->coordinatesSerializer->serialize($object->getCoordinates()): |
|
|
|
|
72
|
3 |
|
null; |
73
|
3 |
|
$tweet->place = $object->getPlace()?$this->placeSerializer->serialize($object->getPlace()):null; |
|
|
|
|
74
|
3 |
|
$tweet->in_reply_to_status_id = $object->getInReplyToStatusId(); |
|
|
|
|
75
|
3 |
|
$tweet->in_reply_to_user_id = $object->getInReplyToUserId(); |
|
|
|
|
76
|
3 |
|
$tweet->in_reply_to_screen_name = $object->getInReplyToScreenName(); |
|
|
|
|
77
|
3 |
|
$tweet->retweeted = $object->isRetweeted(); |
|
|
|
|
78
|
3 |
|
$tweet->retweet_count = $object->getRetweetCount(); |
|
|
|
|
79
|
3 |
|
$tweet->favorited = $object->isFavorited(); |
|
|
|
|
80
|
3 |
|
$tweet->favorite_count = $object->getFavoriteCount(); |
|
|
|
|
81
|
3 |
|
$tweet->truncated = $object->isTruncated(); |
|
|
|
|
82
|
3 |
|
$tweet->source = $object->getSource(); |
|
|
|
|
83
|
|
|
|
84
|
3 |
|
if ($object->getRetweetedStatus()) { |
|
|
|
|
85
|
3 |
|
$tweet->retweeted_status = $this->serialize($object->getRetweetedStatus()); |
|
|
|
|
86
|
2 |
|
} |
87
|
|
|
|
88
|
3 |
|
return $tweet; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param \stdClass $obj |
93
|
|
|
* @param array $context |
94
|
|
|
* @return \Twitter\Object\Tweet |
95
|
|
|
*/ |
96
|
3 |
|
public function unserialize($obj, array $context = []) |
97
|
|
|
{ |
98
|
3 |
|
if (!$this->canUnserialize($obj)) { |
99
|
3 |
|
throw new \InvalidArgumentException('$object is not unserializable'); |
100
|
3 |
|
} |
101
|
3 |
|
|
102
|
3 |
|
return Tweet::create( |
103
|
3 |
|
TwitterMessageId::create($obj->id), |
104
|
3 |
|
$this->userSerializer->unserialize($obj->user), |
105
|
3 |
|
$obj->text, |
106
|
3 |
|
$obj->lang, |
107
|
3 |
|
new \DateTimeImmutable($obj->created_at), |
108
|
3 |
|
$obj->entities?$this->twitterEntitiesSerializer->unserialize($obj->entities):null, |
109
|
3 |
|
$obj->coordinates?$this->coordinatesSerializer->unserialize($obj->coordinates):null, |
110
|
3 |
|
$obj->place?$this->placeSerializer->unserialize($obj->place):null, |
111
|
3 |
|
$obj->in_reply_to_status_id, |
112
|
3 |
|
$obj->in_reply_to_user_id, |
113
|
3 |
|
$obj->in_reply_to_screen_name, |
114
|
3 |
|
$obj->retweeted, |
115
|
3 |
|
$obj->retweet_count, |
116
|
3 |
|
$obj->favorited, |
117
|
2 |
|
$obj->favorite_count, |
118
|
|
|
$obj->truncated, |
119
|
|
|
$obj->source, |
120
|
|
|
(isset($obj->retweeted_status)) ? $this->unserialize($obj->retweeted_status) : null |
121
|
|
|
); |
122
|
|
|
} |
123
|
12 |
|
|
124
|
|
|
/** |
125
|
12 |
|
* @param TwitterSerializable $object |
126
|
12 |
|
* @return boolean |
127
|
12 |
|
*/ |
128
|
12 |
|
public function canSerialize(TwitterSerializable $object) |
129
|
12 |
|
{ |
130
|
8 |
|
return $object instanceof Tweet; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param \stdClass $obj |
135
|
|
|
* @param array $context |
136
|
|
|
* @return boolean |
137
|
|
|
*/ |
138
|
|
|
public function canUnserialize($obj, array $context = []) |
139
|
|
|
{ |
140
|
|
|
return isset($obj->text) && isset($obj->user); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return TweetSerializer |
145
|
|
|
*/ |
146
|
|
|
public static function build() |
147
|
|
|
{ |
148
|
|
|
return new self( |
149
|
|
|
TwitterUserSerializer::build(), |
150
|
|
|
TwitterEntitiesSerializer::build(), |
151
|
|
|
TwitterCoordinatesSerializer::build(), |
152
|
|
|
TwitterPlaceSerializer::build() |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
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: