1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Twitter\Serializer; |
4
|
|
|
|
5
|
|
|
use Twitter\Object\TwitterDelete; |
6
|
|
|
use Twitter\TwitterSerializable; |
7
|
|
|
use Twitter\TwitterSerializer; |
8
|
|
|
|
9
|
|
|
class TwitterDeleteSerializer implements TwitterSerializer |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param TwitterSerializable $object |
13
|
|
|
* @return \stdClass |
14
|
|
|
*/ |
15
|
12 |
|
public function serialize(TwitterSerializable $object) |
16
|
|
|
{ |
17
|
12 |
|
if (!$this->canSerialize($object)) { |
18
|
3 |
|
throw new \InvalidArgumentException('$object must be an instance of TwitterDelete'); |
19
|
|
|
} |
20
|
|
|
|
21
|
9 |
|
$refObject = new \stdClass(); |
22
|
9 |
|
$refObject->id = $object->getId(); |
|
|
|
|
23
|
9 |
|
$refObject->user_id = $object->getUserId(); |
|
|
|
|
24
|
|
|
|
25
|
9 |
|
$obj = new \stdClass(); |
26
|
|
|
|
27
|
9 |
|
switch ($object->getType()) { |
|
|
|
|
28
|
9 |
|
case TwitterDelete::TWEET: |
29
|
3 |
|
$obj->status = $refObject; |
30
|
3 |
|
break; |
31
|
6 |
|
case TwitterDelete::DM: |
32
|
3 |
|
$obj->direct_message = $refObject; |
33
|
3 |
|
break; |
34
|
2 |
|
default: |
35
|
3 |
|
throw new \InvalidArgumentException('Invalid delete type'); |
36
|
6 |
|
} |
37
|
|
|
|
38
|
6 |
|
if ($object->getDate()) { |
|
|
|
|
39
|
6 |
|
$obj->timestamp_ms = $object->getDate()->getTimestamp() * 1000; |
|
|
|
|
40
|
4 |
|
} |
41
|
|
|
|
42
|
6 |
|
$delete = new \stdClass(); |
43
|
6 |
|
$delete->delete = $obj; |
44
|
|
|
|
45
|
6 |
|
return $delete; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param \stdClass $obj |
50
|
|
|
* @param array $context |
51
|
|
|
* @return TwitterDelete |
52
|
|
|
*/ |
53
|
6 |
|
public function unserialize($obj, array $context = []) |
54
|
|
|
{ |
55
|
6 |
|
if (!$this->canUnserialize($obj)) { |
56
|
|
|
throw new \InvalidArgumentException('$object is not unserializable'); |
57
|
6 |
|
} |
58
|
3 |
|
|
59
|
3 |
|
$d = $obj->delete; |
60
|
2 |
|
if (isset($d->status)) { |
61
|
3 |
|
$ref = $d->status; |
62
|
3 |
|
$type = TwitterDelete::TWEET; |
63
|
|
|
} else { |
64
|
|
|
$ref = $d->direct_message; |
65
|
6 |
|
$type = TwitterDelete::DM; |
66
|
4 |
|
} |
67
|
6 |
|
|
68
|
6 |
|
return TwitterDelete::create( |
69
|
6 |
|
$type, |
70
|
4 |
|
$ref->id, |
71
|
|
|
$ref->user_id, |
72
|
|
|
(new \DateTimeImmutable())->setTimestamp((int) floor($d->timestamp_ms / 1000))?:new \DateTimeImmutable() |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
/** |
77
|
|
|
* @param TwitterSerializable $object |
78
|
6 |
|
* @return boolean |
79
|
|
|
*/ |
80
|
|
|
public function canSerialize(TwitterSerializable $object) |
81
|
|
|
{ |
82
|
|
|
return $object instanceof TwitterDelete; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param \stdClass $object |
87
|
|
|
* @return boolean |
88
|
|
|
*/ |
89
|
|
|
public function canUnserialize($object) |
90
|
|
|
{ |
91
|
|
|
return isset($object->delete); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return TwitterDeleteSerializer |
96
|
|
|
*/ |
97
|
|
|
public static function build() |
98
|
|
|
{ |
99
|
|
|
return new self(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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: