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