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