1 | <?php |
||
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( |
|
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 = []) |
|
126 | |||
127 | /** |
||
128 | * @param TwitterSerializable $object |
||
129 | * @return boolean |
||
130 | */ |
||
131 | 9 | public function canSerialize(TwitterSerializable $object) |
|
135 | |||
136 | /** |
||
137 | * @param \stdClass $obj |
||
138 | * @param array $context |
||
139 | * @return boolean |
||
140 | */ |
||
141 | 6 | public function canUnserialize($obj, array $context = []) |
|
145 | |||
146 | /** |
||
147 | * @return TweetSerializer |
||
148 | */ |
||
149 | 12 | public static function build() |
|
158 | } |
||
159 |