|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Twitter\Serializer; |
|
4
|
|
|
|
|
5
|
|
|
use Assert\Assertion; |
|
6
|
|
|
use Twitter\Object\TwitterSymbol; |
|
7
|
|
|
use Twitter\TwitterSerializable; |
|
8
|
|
|
use Twitter\TwitterSerializer; |
|
9
|
|
|
|
|
10
|
|
|
class TwitterSymbolSerializer 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 TwitterSymbol $object */ |
|
34
|
6 |
|
Assertion::true($this->canSerialize($object), 'object must be an instance of TwitterSymbol'); |
|
35
|
|
|
|
|
36
|
3 |
|
$symbol = new \stdClass(); |
|
37
|
3 |
|
$symbol->text = $object->getText(); |
|
38
|
3 |
|
$symbol->indices = $this->entityIndicesSerializer->serialize($object->getIndices()); |
|
39
|
|
|
|
|
40
|
3 |
|
return $symbol; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param \stdClass $obj |
|
45
|
|
|
* @param array $context |
|
46
|
|
|
* @return TwitterSymbol |
|
47
|
|
|
*/ |
|
48
|
6 |
|
public function unserialize($obj, array $context = []) |
|
49
|
|
|
{ |
|
50
|
6 |
|
Assertion::true($this->canUnserialize($obj), 'object is not unserializable'); |
|
51
|
|
|
|
|
52
|
3 |
|
return TwitterSymbol::create( |
|
53
|
3 |
|
$obj->text, |
|
54
|
3 |
|
$this->entityIndicesSerializer->unserialize($obj->indices) |
|
55
|
2 |
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param TwitterSerializable $object |
|
60
|
|
|
* @return boolean |
|
61
|
|
|
*/ |
|
62
|
6 |
|
public function canSerialize(TwitterSerializable $object) |
|
63
|
|
|
{ |
|
64
|
6 |
|
return $object instanceof TwitterSymbol; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param \stdClass $object |
|
69
|
|
|
* @return boolean |
|
70
|
|
|
*/ |
|
71
|
6 |
|
public function canUnserialize($object) |
|
72
|
|
|
{ |
|
73
|
6 |
|
return isset($object->text) && isset($object->indices); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return TwitterSymbolSerializer |
|
78
|
|
|
*/ |
|
79
|
21 |
|
public static function build() |
|
80
|
|
|
{ |
|
81
|
21 |
|
return new self( |
|
82
|
21 |
|
TwitterEntityIndicesSerializer::build() |
|
83
|
14 |
|
); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|