Completed
Push — master ( 8bcfe7...12792b )
by Rémi
03:26
created

TwitterEntitiesSerializer   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 3
Metric Value
wmc 37
c 7
b 0
f 3
lcom 1
cbo 7
dl 0
loc 214
ccs 97
cts 97
cp 1
rs 8.6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
C serialize() 0 58 14
C unserialize() 0 56 14
A canSerialize() 0 4 1
B canUnserialize() 0 4 6
A build() 0 11 1
1
<?php
2
3
namespace Twitter\Serializer;
4
5
use Twitter\Object\TwitterEntities;
6
use Twitter\TwitterSerializable;
7
use Twitter\TwitterSerializer;
8
9
class TwitterEntitiesSerializer implements TwitterSerializer
10
{
11
    /**
12
     * @var TwitterHashtagSerializer
13
     */
14
    private $hashtagSerializer;
15
16
    /**
17
     * @var TwitterSymbolSerializer
18
     */
19
    private $symbolSerializer;
20
21
    /**
22
     * @var TwitterUrlSerializer
23
     */
24
    private $urlSerializer;
25
26
    /**
27
     * @var TwitterUserMentionSerializer
28
     */
29
    private $userMentionSerializer;
30
31
    /**
32
     * @var TwitterMediaSerializer
33
     */
34
    private $mediaSerializer;
35
36
    /**
37
     * @var TwitterExtendedEntitySerializer
38
     */
39
    private $extendedEntitySerializer;
40
41
    /**
42
     * @param TwitterExtendedEntitySerializer $extendedEntitySerializer
43
     * @param TwitterHashtagSerializer $hashtagSerializer
44
     * @param TwitterMediaSerializer $mediaSerializer
45
     * @param TwitterSymbolSerializer $symbolSerializer
46
     * @param TwitterUrlSerializer $urlSerializer
47
     * @param TwitterUserMentionSerializer $userMentionSerializer
48
     */
49 27
    public function __construct(
50
        TwitterExtendedEntitySerializer $extendedEntitySerializer,
51
        TwitterHashtagSerializer $hashtagSerializer,
52
        TwitterMediaSerializer $mediaSerializer,
53
        TwitterSymbolSerializer $symbolSerializer,
54
        TwitterUrlSerializer $urlSerializer,
55
        TwitterUserMentionSerializer $userMentionSerializer
56
    ) {
57 27
        $this->extendedEntitySerializer = $extendedEntitySerializer;
58 27
        $this->hashtagSerializer = $hashtagSerializer;
59 27
        $this->mediaSerializer = $mediaSerializer;
60 27
        $this->symbolSerializer = $symbolSerializer;
61 27
        $this->urlSerializer = $urlSerializer;
62 27
        $this->userMentionSerializer = $userMentionSerializer;
63 27
    }
64
65
    /**
66
     * @param  TwitterSerializable $object
67
     * @return \stdClass
68
     */
69 6
    public function serialize(TwitterSerializable $object)
70
    {
71 6
        if (!$this->canSerialize($object)) {
72 3
            throw new \InvalidArgumentException('$object must be an instance of TwitterEntities');
73
        }
74
75 3
        $entities = new \stdClass();
76
77
        // Hashtags
78 3
        if ($object->getHashtags()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getHashtags() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntities.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
79 3
            $entities->hashtags = [];
80 3
            foreach ($object->getHashtags() as $hashtag) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getHashtags() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntities.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
81 3
                $entities->hashtags[] = $this->hashtagSerializer->serialize($hashtag);
82 2
            }
83 2
        }
84
85
        // Symbols
86 3
        if ($object->getSymbols()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getSymbols() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntities.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
87 3
            $entities->symbols = [];
88 3
            foreach ($object->getSymbols() as $symbol) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getSymbols() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntities.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
89 3
                $entities->symbols[] = $this->symbolSerializer->serialize($symbol);
90 2
            }
91 2
        }
92
93
        // Urls
94 3
        if ($object->getUrls()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getUrls() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntities.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
95 3
            $entities->urls = [];
96 3
            foreach ($object->getUrls() as $url) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getUrls() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntities.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
97 3
                $entities->urls[] = $this->urlSerializer->serialize($url);
98 2
            }
99 2
        }
100
101
        // User mentions
102 3
        if ($object->getUserMentions()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getUserMentions() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntities.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
103 3
            $entities->user_mentions = [];
104 3
            foreach ($object->getUserMentions() as $userMention) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getUserMentions() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntities.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
105 3
                $entities->user_mentions[] = $this->userMentionSerializer->serialize($userMention);
106 2
            }
107 2
        }
108
109
        // Media
110 3
        if ($object->getMedia()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getMedia() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntities.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
111 3
            $entities->media = [];
112 3
            foreach ($object->getMedia() as $media) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getMedia() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntities.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
113 3
                $entities->media[] = $this->mediaSerializer->serialize($media);
114 2
            }
115 2
        }
116
117
        // Extended entities
118 3
        if ($object->getExtendedEntities()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getExtendedEntities() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntities.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
119 3
            $entities->extended_entities = [];
120 3
            foreach ($object->getExtendedEntities() as $extendedEntity) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getExtendedEntities() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntities.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
121 3
                $entities->extended_entities[] = $this->extendedEntitySerializer->serialize($extendedEntity);
122 2
            }
123 2
        }
124
125 5
        return $entities;
126 2
    }
127
128
    /**
129
     * @param  \stdClass $obj
130
     * @param  array     $context
131
     * @return TwitterEntities
132
     */
133 3
    public function unserialize($obj, array $context = [])
134
    {
135
        if (!$this->canUnserialize($obj)) {
136 3
            throw new \InvalidArgumentException('$object is not unserializable');
137 3
        }
138 3
139 3
        // Hashtags
140 2
        $hashtags = [];
141 2
        if (isset($obj->hashtags)) {
142
            foreach ($obj->hashtags as $hashtag) {
143
                $hashtags[] = $this->hashtagSerializer->unserialize($hashtag);
144 3
            }
145 3
        }
146 3
147 3
        // Symbols
148 2
        $symbols = [];
149 2
        if (isset($obj->symbols)) {
150
            foreach ($obj->symbols as $symbol) {
151
                $symbols[] = $this->symbolSerializer->unserialize($symbol);
152 3
            }
153 3
        }
154 3
155 3
        // URLs
156 2
        $urls = [];
157 2
        if (isset($obj->urls)) {
158
            foreach ($obj->urls as $url) {
159
                $urls[] = $this->urlSerializer->unserialize($url);
160 3
            }
161 3
        }
162 3
163 3
        // User mentions
164 2
        $userMentions = [];
165 2
        if (isset($obj->user_mentions)) {
166
            foreach ($obj->user_mentions as $userMention) {
167
                $userMentions[] = $this->userMentionSerializer->unserialize($userMention);
168 3
            }
169 3
        }
170 3
171 3
        // Media
172 2
        $media = [];
173 2
        if (isset($obj->media)) {
174
            foreach ($obj->media as $medium) {
175
                $media[] = $this->mediaSerializer->unserialize($medium);
176 3
            }
177 3
        }
178 3
179 3
        // Extended entities
180 2
        $extendedEntities = [];
181 2
        if (isset($obj->extended_entities)) {
182
            foreach ($obj->extended_entities as $extendedEntity) {
183 3
                $extendedEntities[] = $this->extendedEntitySerializer->unserialize($extendedEntity);
184
            }
185
        }
186
187
        return TwitterEntities::create($hashtags, $userMentions, $urls, $media, $symbols, $extendedEntities);
188
    }
189 18
190
    /**
191 18
     * @param  TwitterSerializable $object
192 18
     * @return boolean
193 18
     */
194 18
    public function canSerialize(TwitterSerializable $object)
195 18
    {
196 18
        return $object instanceof TwitterEntities;
197 18
    }
198 12
199
    /**
200
     * @param  \stdClass $object
201
     * @return boolean
202
     */
203
    public function canUnserialize($object)
204
    {
205
        return isset($object->hashtags) ||  isset($object->symbols) ||  isset($object->urls) || isset($object->user_mentions) || isset($object->media) || isset($object->extended_entities);
206
    }
207
208
    /**
209
     * @return TwitterEntitiesSerializer
210
     */
211
    public static function build()
212
    {
213
        return new self(
214
            TwitterExtendedEntitySerializer::build(),
215
            TwitterHashtagSerializer::build(),
216
            TwitterMediaSerializer::build(),
217
            TwitterSymbolSerializer::build(),
218
            TwitterUrlSerializer::build(),
219
            TwitterUserMentionSerializer::build()
220
        );
221
    }
222
}
223