|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Twitter\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use Assert\Assertion; |
|
6
|
|
|
use Twitter\Object\TwitterUrl; |
|
7
|
|
|
use Twitter\TwitterSerializable; |
|
8
|
|
|
use Twitter\TwitterSerializer; |
|
9
|
|
|
|
|
10
|
|
|
class TwitterUrlSerializer implements TwitterSerializer |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var TwitterEntityIndicesSerializer |
|
14
|
|
|
*/ |
|
15
|
|
|
private $entityIndicesSerializer; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Constructor |
|
19
|
|
|
* |
|
20
|
|
|
* @param TwitterEntityIndicesSerializer $entityIndicesSerializer |
|
21
|
|
|
*/ |
|
22
|
33 |
|
public function __construct(TwitterEntityIndicesSerializer $entityIndicesSerializer) |
|
23
|
|
|
{ |
|
24
|
33 |
|
$this->entityIndicesSerializer = $entityIndicesSerializer; |
|
25
|
33 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param TwitterSerializable $object |
|
29
|
|
|
* @return \stdClass |
|
30
|
|
|
*/ |
|
31
|
6 |
|
public function serialize(TwitterSerializable $object) |
|
32
|
|
|
{ |
|
33
|
|
|
/* @var TwitterUrl $object */ |
|
34
|
6 |
|
Assertion::true($this->canSerialize($object), 'object must be an instance of TwitterUrl'); |
|
35
|
|
|
|
|
36
|
3 |
|
$url = new \stdClass(); |
|
37
|
3 |
|
$url->url = $object->getUrl(); |
|
38
|
3 |
|
$url->display_url = $object->getDisplayUrl(); |
|
39
|
3 |
|
$url->expanded_url = $object->getExpandedUrl(); |
|
40
|
3 |
|
$url->indices = $this->entityIndicesSerializer->serialize($object->getIndices()); |
|
41
|
|
|
|
|
42
|
3 |
|
return $url; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param \stdClass $obj |
|
47
|
|
|
* @param array $context |
|
48
|
|
|
* @return TwitterUrl |
|
49
|
|
|
*/ |
|
50
|
6 |
|
public function unserialize($obj, array $context = []) |
|
51
|
|
|
{ |
|
52
|
6 |
|
Assertion::true($this->canUnserialize($obj), 'object is not unserializable'); |
|
53
|
|
|
|
|
54
|
3 |
|
return TwitterUrl::create( |
|
55
|
3 |
|
$obj->url, |
|
56
|
3 |
|
$obj->display_url, |
|
57
|
3 |
|
$obj->expanded_url, |
|
58
|
3 |
|
$this->entityIndicesSerializer->unserialize($obj->indices) |
|
59
|
2 |
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param TwitterSerializable $object |
|
64
|
|
|
* @return boolean |
|
65
|
|
|
*/ |
|
66
|
6 |
|
public function canSerialize(TwitterSerializable $object) |
|
67
|
|
|
{ |
|
68
|
6 |
|
return $object instanceof TwitterUrl; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param \stdClass $object |
|
73
|
|
|
* @return boolean |
|
74
|
|
|
*/ |
|
75
|
6 |
|
public function canUnserialize($object) |
|
76
|
|
|
{ |
|
77
|
6 |
|
return isset($object->url) && isset($object->indices); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return TwitterUrlSerializer |
|
82
|
|
|
*/ |
|
83
|
21 |
|
public static function build() |
|
84
|
|
|
{ |
|
85
|
21 |
|
return new self( |
|
86
|
21 |
|
TwitterEntityIndicesSerializer::build() |
|
87
|
14 |
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|