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