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