TwitterUserSerializer::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Twitter\Serializer;
4
5
use Assert\Assertion;
6
use Twitter\Object\TwitterUser;
7
use Twitter\TwitterSerializable;
8
use Twitter\TwitterSerializer;
9
10
class TwitterUserSerializer implements TwitterSerializer
11
{
12
    /**
13
     * Serialize a twitter user
14
     *
15
     * @param  TwitterSerializable $object
16
     * @return \stdClass
17
     */
18 6
    public function serialize(TwitterSerializable $object)
19
    {
20
        /* @var TwitterUser $object */
21 6
        Assertion::true($this->canSerialize($object), 'object must be an instance of TwitterUser');
22
23 3
        $user = new \stdClass();
24 3
        $user->id = $object->getId();
25 3
        $user->screen_name = $object->getScreenName();
26 3
        $user->name = $object->getName();
27 3
        $user->lang = $object->getLang();
28 3
        $user->location = $object->getLocation();
29 3
        $user->profile_background_image_url = $object->getProfileImageUrl();
30 3
        $user->profile_background_image_url_https = $object->getProfileImageUrlHttps();
31
32 3
        return $user;
33
    }
34
35
    /**
36
     * Unserialize a twitter user
37
     *
38
     * @param  \stdClass $obj
39
     * @param  array     $context
40
     * @return TwitterUser
41
     */
42 6
    public function unserialize($obj, array $context = [])
43
    {
44 6
        Assertion::true($this->canUnserialize($obj), 'object is not unserializable');
45
46 3
        return TwitterUser::create(
47 3
            $obj->id,
48 3
            $obj->screen_name,
49 3
            $obj->name,
50 3
            $obj->lang,
51 3
            $obj->location,
52 3
            $obj->profile_background_image_url,
53 3
            $obj->profile_background_image_url_https
54 2
        );
55
    }
56
57
    /**
58
     * @param  TwitterSerializable $object
59
     * @return boolean
60
     */
61 6
    public function canSerialize(TwitterSerializable $object)
62
    {
63 6
        return $object instanceof TwitterUser;
64
    }
65
66
    /**
67
     * @param  \stdClass $object
68
     * @return boolean
69
     */
70 6
    public function canUnserialize($object)
71
    {
72 6
        return isset($object->id) &&
73 6
            isset($object->screen_name) &&
74 6
            isset($object->name) &&
75 6
            isset($object->lang);
76
    }
77
78
    /**
79
     * @return TwitterUserSerializer
80
     */
81 18
    public static function build()
82
    {
83 18
        return new self();
84
    }
85
}
86