1 | <?php |
||
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( |
|
36 | |||
37 | /** |
||
38 | * @param TwitterSerializable $object |
||
39 | * @return \stdClass |
||
40 | */ |
||
41 | 6 | public function serialize(TwitterSerializable $object) |
|
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) |
|
94 | |||
95 | /** |
||
96 | * @param \stdClass $object |
||
97 | * @return boolean |
||
98 | */ |
||
99 | 6 | public function canUnserialize($object) |
|
103 | |||
104 | /** |
||
105 | * @param $object |
||
106 | * |
||
107 | 6 | * @return bool |
|
108 | */ |
||
109 | 6 | private function hasPrefix($object) |
|
113 | |||
114 | /** |
||
115 | * @return TwitterDirectMessageSerializer |
||
116 | */ |
||
117 | public static function build() |
||
124 | } |
||
125 |