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
|
15 |
|
public function __construct( |
28
|
|
|
TwitterUserSerializer $userSerializer, |
29
|
|
|
TwitterEntitiesSerializer $twitterEntitiesSerializer |
30
|
|
|
) { |
31
|
15 |
|
$this->userSerializer = $userSerializer; |
32
|
15 |
|
$this->twitterEntitiesSerializer = $twitterEntitiesSerializer; |
33
|
15 |
|
} |
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
|
3 |
|
$dm = new \stdClass(); |
46
|
3 |
|
$dm->id = (string)$object->getId(); |
|
|
|
|
47
|
3 |
|
$dm->sender = $this->userSerializer->serialize($object->getSender()); |
|
|
|
|
48
|
3 |
|
$dm->recipient = $this->userSerializer->serialize($object->getRecipient()); |
|
|
|
|
49
|
3 |
|
$dm->text = $object->getText(); |
|
|
|
|
50
|
3 |
|
$dm->created_at = $object->getDate()->setTimezone(new \DateTimeZone('UTC'))->format(TwitterDate::FORMAT); |
|
|
|
|
51
|
3 |
|
$dm->entities = $object->getEntities()?$this->twitterEntitiesSerializer->serialize($object->getEntities()):null; |
|
|
|
|
52
|
|
|
|
53
|
3 |
|
$dmObject = new \stdClass(); |
54
|
|
|
$dmObject->direct_message = $dm; |
55
|
|
|
|
56
|
|
|
return $dmObject; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param \stdClass $directMessage |
61
|
3 |
|
* @param array $context |
62
|
|
|
* @return TwitterDirectMessage |
63
|
3 |
|
*/ |
64
|
3 |
|
public function unserialize($directMessage, array $context = []) |
65
|
3 |
|
{ |
66
|
3 |
|
if (!$this->canUnserialize($directMessage)) { |
67
|
3 |
|
throw new \InvalidArgumentException('$object is not unserializable'); |
68
|
3 |
|
} |
69
|
3 |
|
|
70
|
2 |
|
$dm = $directMessage->direct_message; |
71
|
|
|
return TwitterDirectMessage::create( |
72
|
|
|
TwitterMessageId::create($dm->id), |
73
|
|
|
$this->userSerializer->unserialize($dm->sender), |
74
|
|
|
$this->userSerializer->unserialize($dm->recipient), |
75
|
|
|
$dm->text, |
76
|
6 |
|
new \DateTimeImmutable($dm->created_at), |
77
|
|
|
$dm->entities?$this->twitterEntitiesSerializer->unserialize($dm->entities):null |
78
|
6 |
|
); |
79
|
6 |
|
} |
80
|
6 |
|
|
81
|
4 |
|
/** |
82
|
|
|
* @param TwitterSerializable $object |
83
|
|
|
* @return boolean |
84
|
|
|
*/ |
85
|
|
|
public function canSerialize(TwitterSerializable $object) |
86
|
|
|
{ |
87
|
|
|
return $object instanceof TwitterDirectMessage; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param \stdClass $object |
92
|
|
|
* @return boolean |
93
|
|
|
*/ |
94
|
|
|
public function canUnserialize($object) |
95
|
|
|
{ |
96
|
|
|
return (isset($object->direct_message)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return TwitterDirectMessageSerializer |
101
|
|
|
*/ |
102
|
|
|
public static function build() |
103
|
|
|
{ |
104
|
|
|
return new self( |
105
|
|
|
TwitterUserSerializer::build(), |
106
|
|
|
TwitterEntitiesSerializer::build() |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: