1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Twitter\Serializer; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use Twitter\Object\TwitterDate; |
7
|
|
|
use Twitter\Object\TwitterDirectMessage; |
8
|
|
|
use Twitter\Object\TwitterEntities; |
9
|
|
|
use Twitter\TwitterMessageId; |
10
|
|
|
use Twitter\TwitterSerializable; |
11
|
|
|
use Twitter\TwitterSerializer; |
12
|
|
|
|
13
|
|
|
class TwitterDirectMessageSerializer implements TwitterSerializer |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var TwitterUserSerializer |
17
|
|
|
*/ |
18
|
|
|
private $userSerializer; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var TwitterEntitiesSerializer |
22
|
|
|
*/ |
23
|
|
|
private $twitterEntitiesSerializer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param TwitterUserSerializer $userSerializer |
27
|
|
|
* @param TwitterEntitiesSerializer $twitterEntitiesSerializer |
28
|
|
|
*/ |
29
|
18 |
|
public function __construct( |
30
|
|
|
TwitterUserSerializer $userSerializer, |
31
|
|
|
TwitterEntitiesSerializer $twitterEntitiesSerializer |
32
|
|
|
) { |
33
|
18 |
|
$this->userSerializer = $userSerializer; |
34
|
18 |
|
$this->twitterEntitiesSerializer = $twitterEntitiesSerializer; |
35
|
18 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param TwitterSerializable $object |
39
|
|
|
* @return \stdClass |
40
|
|
|
*/ |
41
|
6 |
|
public function serialize(TwitterSerializable $object) |
42
|
|
|
{ |
43
|
|
|
/* @var TwitterDirectMessage $object */ |
44
|
6 |
|
Assertion::true($this->canSerialize($object), 'object must be an instance of TwitterDirectMessage'); |
45
|
3 |
|
Assertion::eq(new \DateTimeZone('UTC'), $object->getDate()->getTimezone()); |
46
|
|
|
|
47
|
3 |
|
$dm = new \stdClass(); |
48
|
3 |
|
$dm->id = (string)$object->getId(); |
49
|
3 |
|
$dm->sender = $this->userSerializer->serialize($object->getSender()); |
50
|
3 |
|
$dm->recipient = $this->userSerializer->serialize($object->getRecipient()); |
51
|
3 |
|
$dm->text = $object->getText(); |
52
|
3 |
|
$dm->created_at = $object->getDate()->format(TwitterDate::FORMAT); |
53
|
3 |
|
$dm->entities = $object->getEntities()?$this->twitterEntitiesSerializer->serialize($object->getEntities()):null; |
54
|
|
|
|
55
|
3 |
|
$dmObject = new \stdClass(); |
56
|
3 |
|
$dmObject->direct_message = $dm; |
57
|
|
|
|
58
|
3 |
|
return $dmObject; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param \stdClass $directMessage |
63
|
|
|
* @param array $context |
64
|
|
|
* @return TwitterDirectMessage |
65
|
|
|
*/ |
66
|
6 |
|
public function unserialize($directMessage, array $context = []) |
67
|
|
|
{ |
68
|
6 |
|
Assertion::true($this->canUnserialize($directMessage), 'object is not unserializable'); |
69
|
|
|
|
70
|
3 |
|
$dm = $this->hasPrefix($directMessage) ? $directMessage->direct_message : $directMessage; |
71
|
|
|
|
72
|
3 |
|
$createdAt = new \DateTimeImmutable($dm->created_at); |
73
|
3 |
|
Assertion::eq(new \DateTimeZone('UTC'), $createdAt->getTimezone()); |
74
|
|
|
|
75
|
3 |
|
return TwitterDirectMessage::create( |
76
|
3 |
|
TwitterMessageId::create($dm->id), |
77
|
3 |
|
$this->userSerializer->unserialize($dm->sender), |
78
|
3 |
|
$this->userSerializer->unserialize($dm->recipient), |
79
|
3 |
|
$dm->text, |
80
|
2 |
|
$createdAt, |
81
|
3 |
|
$this->twitterEntitiesSerializer->canUnserialize($dm->entities) ? |
82
|
3 |
|
$this->twitterEntitiesSerializer->unserialize($dm->entities) : TwitterEntities::create() |
83
|
2 |
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param TwitterSerializable $object |
88
|
|
|
* @return boolean |
89
|
|
|
*/ |
90
|
6 |
|
public function canSerialize(TwitterSerializable $object) |
91
|
|
|
{ |
92
|
6 |
|
return $object instanceof TwitterDirectMessage; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \stdClass $object |
97
|
|
|
* @return boolean |
98
|
|
|
*/ |
99
|
6 |
|
public function canUnserialize($object) |
100
|
|
|
{ |
101
|
6 |
|
return $this->hasPrefix($object) || (isset($object->sender) && isset($object->recipient)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $object |
106
|
|
|
* |
107
|
6 |
|
* @return bool |
108
|
|
|
*/ |
109
|
6 |
|
private function hasPrefix($object) |
110
|
6 |
|
{ |
111
|
6 |
|
return isset($object->direct_message); |
112
|
4 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return TwitterDirectMessageSerializer |
116
|
|
|
*/ |
117
|
|
|
public static function build() |
118
|
|
|
{ |
119
|
|
|
return new self( |
120
|
|
|
TwitterUserSerializer::build(), |
121
|
|
|
TwitterEntitiesSerializer::build() |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|