TwitterCoordinatesSerializer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 58
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 11 1
A unserialize() 0 8 1
A canSerialize() 0 4 1
A canUnserialize() 0 4 1
A build() 0 4 1
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