1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Twitter\Serializer; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use Twitter\Object\TwitterCoordinates; |
7
|
|
|
use Twitter\TwitterSerializable; |
8
|
|
|
use Twitter\TwitterSerializer; |
9
|
|
|
|
10
|
|
|
class TwitterCoordinatesSerializer implements TwitterSerializer |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param TwitterSerializable $object |
14
|
|
|
* @return \stdClass |
15
|
|
|
*/ |
16
|
6 |
|
public function serialize(TwitterSerializable $object) |
17
|
|
|
{ |
18
|
|
|
/* @var TwitterCoordinates $object */ |
19
|
6 |
|
Assertion::true($this->canSerialize($object), 'object must be an instance of TwitterCoordinates'); |
20
|
|
|
|
21
|
3 |
|
$coords = new \stdClass(); |
22
|
3 |
|
$coords->coordinates = [$object->getLongitude(), $object->getLatitude()]; |
23
|
3 |
|
$coords->type = $object->getType(); |
24
|
|
|
|
25
|
3 |
|
return $coords; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \stdClass $obj |
30
|
|
|
* @param array $context |
31
|
|
|
* @return TwitterCoordinates |
32
|
|
|
*/ |
33
|
6 |
|
public function unserialize($obj, array $context = []) |
34
|
|
|
{ |
35
|
6 |
|
Assertion::true($this->canUnserialize($obj), 'object is not unserializable'); |
36
|
|
|
|
37
|
3 |
|
$coords = $obj->coordinates; |
38
|
|
|
|
39
|
3 |
|
return TwitterCoordinates::create($coords[0], $coords[1], $obj->type); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param TwitterSerializable $object |
44
|
|
|
* @return boolean |
45
|
|
|
*/ |
46
|
6 |
|
public function canSerialize(TwitterSerializable $object) |
47
|
|
|
{ |
48
|
6 |
|
return $object instanceof TwitterCoordinates; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param \stdClass $object |
53
|
|
|
* @return boolean |
54
|
|
|
*/ |
55
|
6 |
|
public function canUnserialize($object) |
56
|
|
|
{ |
57
|
6 |
|
return isset($object->coordinates); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return TwitterCoordinatesSerializer |
62
|
|
|
*/ |
63
|
15 |
|
public static function build() |
64
|
|
|
{ |
65
|
15 |
|
return new self(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|