TwitterUserMentionSerializer::unserialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Twitter\Serializer;
4
5
use Assert\Assertion;
6
use Twitter\Object\TwitterUserMention;
7
use Twitter\TwitterSerializable;
8
use Twitter\TwitterSerializer;
9
10
class TwitterUserMentionSerializer 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 TwitterUserMention $object */
34 6
        Assertion::true($this->canSerialize($object), 'object must be an instance of TwitterUserMention');
35
36 3
        $userMention = new \stdClass();
37 3
        $userMention->id = $object->getId();
38 3
        $userMention->screen_name = $object->getScreenName();
39 3
        $userMention->name = $object->getName();
40 3
        $userMention->indices = $this->entityIndicesSerializer->serialize($object->getIndices());
41
42 3
        return $userMention;
43
    }
44
45
    /**
46
     * @param  \stdClass $obj
47
     * @param  array     $context
48
     * @return TwitterUserMention
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 TwitterUserMention::create(
55 3
            $obj->id,
56 3
            $obj->screen_name,
57 3
            $obj->name,
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 TwitterUserMention;
69
    }
70
71
    /**
72
     * @param  \stdClass $object
73
     * @return boolean
74
     */
75 6
    public function canUnserialize($object)
76
    {
77 6
        return isset($object->id) && isset($object->indices);
78
    }
79
80
    /**
81
     * @return TwitterUserMentionSerializer
82
     */
83 21
    public static function build()
84
    {
85 21
        return new self(
86 21
            TwitterEntityIndicesSerializer::build()
87 14
        );
88
    }
89
}
90