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