Completed
Push — master ( 45165d...5e44c4 )
by Alexey
04:08 queued 11s
created

Developer::asArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 0
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 12
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author   Ne-Lexa
7
 * @license  MIT
8
 *
9
 * @see      https://github.com/Ne-Lexa/google-play-scraper
10
 */
11
12
namespace Nelexa\GPlay\Model;
13
14
use Nelexa\GPlay\GPlayApps;
15
use Nelexa\GPlay\Model\Builder\DeveloperBuilder;
16
17
/**
18
 * Contains data on the application developer in the Google Play store.
19
 *
20
 * @see GPlayApps::getDeveloperInfo() Returns information about the
21
 *     developer: name, icon, cover, description and website address.
22
 * @see GPlayApps::getDeveloperInfoForLocales() Returns information
23
 *     about the developer for the locale array.
24
 * @see GPlayApps::getAppInfo() Returns detailed information about the
25
 *     Android application from the Google Play store.
26
 * @see GPlayApps::getAppsInfo() Returns detailed information about
27
 *     many android packages.
28
 * @see GPlayApps::getAppInLocales() Returns detailed information
29
 *     about an application from the Google Play store for an array of locales.
30
 * @see GPlayApps::getAppInfoForAvailableLocales() Returns detailed
31
 *     information about the application in all available locales.
32
 */
33
class Developer implements \JsonSerializable
34
{
35
    use JsonSerializableTrait;
36
37
    /** @var string Developer id */
38
    private $id;
39
40
    /** @var string url Developer page url in Google Play store */
41
    private $url;
42
43
    /** @var string Developer name */
44
    private $name;
45
46
    /** @var string|null Description of the developer */
47
    private $description;
48
49
    /** @var string|null Developer website */
50
    private $website;
51
52
    /** @var GoogleImage|null Developer icon */
53
    private $icon;
54
55
    /** @var GoogleImage|null Developer cover */
56
    private $cover;
57
58
    /** @var string|null Developer email */
59
    private $email;
60
61
    /** @var string|null Developer address */
62
    private $address;
63
64
    /**
65
     * Creates an object with information about the developer of Google Play.
66
     *
67
     * @param DeveloperBuilder $builder developer builder
68
     *
69
     * @internal
70
     */
71 55
    public function __construct(DeveloperBuilder $builder)
72
    {
73 55
        $this->id = $builder->getId();
74 55
        $this->url = $builder->getUrl();
75 55
        $this->name = $builder->getName();
76 55
        $this->description = $builder->getDescription();
77 55
        $this->website = $builder->getWebsite();
78 55
        $this->icon = $builder->getIcon();
79 55
        $this->cover = $builder->getCover();
80 55
        $this->email = $builder->getEmail();
81 55
        $this->address = $builder->getAddress();
82
83 55
        if (empty($this->id)) {
84 1
            throw new \InvalidArgumentException(
85
                'Developer id cannot be null or empty. ' .
86 1
                'Solution: $developerBuilder->setId(...);'
87
            );
88
        }
89
90 55
        if (empty($this->url)) {
91 1
            throw new \InvalidArgumentException(
92
                'Developer url cannot be null or empty. ' .
93 1
                'Solution: $developerBuilder->setUrl(...);'
94
            );
95
        }
96
97 55
        if (empty($this->name)) {
98 1
            throw new \InvalidArgumentException(
99
                'Developer name cannot be null or empty. ' .
100 1
                'Solution: $developerBuilder->setName(...);'
101
            );
102
        }
103 55
    }
104
105
    /**
106
     * Returns developer id.
107
     *
108
     * @return string developer id
109
     */
110 5
    public function getId(): string
111
    {
112 5
        return $this->id;
113
    }
114
115
    /**
116
     * Returns the URL of the developer’s page in Google Play.
117
     *
118
     * @return string developer page url
119
     */
120 3
    public function getUrl(): string
121
    {
122 3
        return $this->url;
123
    }
124
125
    /**
126
     * Returns the name of the developer.
127
     *
128
     * @return string Developer name
129
     */
130 3
    public function getName(): string
131
    {
132 3
        return $this->name;
133
    }
134
135
    /**
136
     * Returns a description of the developer.
137
     *
138
     * @return string|null description of the developer or `null`
139
     */
140 3
    public function getDescription(): ?string
141
    {
142 3
        return $this->description;
143
    }
144
145
    /**
146
     * Returns the developer's website.
147
     *
148
     * @return string|null developer website or `null`
149
     */
150 3
    public function getWebsite(): ?string
151
    {
152 3
        return $this->website;
153
    }
154
155
    /**
156
     * Returns the developer icon.
157
     *
158
     * @return GoogleImage|null developer icon or `null`
159
     */
160 3
    public function getIcon(): ?GoogleImage
161
    {
162 3
        return $this->icon;
163
    }
164
165
    /**
166
     * Returns the developer cover.
167
     *
168
     * @return GoogleImage|null developer cover or `null`
169
     */
170 3
    public function getCover(): ?GoogleImage
171
    {
172 3
        return $this->cover;
173
    }
174
175
    /**
176
     * Returns developer email.
177
     *
178
     * @return string|null developer email or `null`
179
     */
180 3
    public function getEmail(): ?string
181
    {
182 3
        return $this->email;
183
    }
184
185
    /**
186
     * Returns the address of the developer.
187
     *
188
     * @return string|null developer address or `null`
189
     */
190 3
    public function getAddress(): ?string
191
    {
192 3
        return $this->address;
193
    }
194
195
    /**
196
     * Creates a new developer builder.
197
     *
198
     * @return DeveloperBuilder developer builder
199
     *
200
     * @internal
201
     */
202 55
    public static function newBuilder(): DeveloperBuilder
203
    {
204 55
        return new DeveloperBuilder();
205
    }
206
207
    /**
208
     * Checks for equality of developer.
209
     *
210
     * @param Developer $otherDeveloper developer with which is compared
211
     *
212
     * @return bool `true` if the contents of the objects being changed are the same
213
     *              and `false` if the objects contain different data
214
     *
215
     * @internal
216
     */
217
    public function equals(self $otherDeveloper): bool
218
    {
219
        if ($this->id !== $otherDeveloper->id) {
220
            return false;
221
        }
222
223
        if ($this->name !== $otherDeveloper->name) {
224
            return false;
225
        }
226
227
        if ($this->description !== $otherDeveloper->description) {
228
            return false;
229
        }
230
231
        if ($this->icon !== null && $otherDeveloper->icon !== null) {
232
            if ($this->icon->getOriginalSizeUrl() !== $otherDeveloper->icon->getOriginalSizeUrl()) {
233
                return false;
234
            }
235
        } elseif ($this->icon !== $otherDeveloper->icon) {
236
            return false;
237
        }
238
239
        if ($this->cover !== null && $otherDeveloper->cover !== null) {
240
            if ($this->cover->getOriginalSizeUrl() !== $otherDeveloper->cover->getOriginalSizeUrl()) {
241
                return false;
242
            }
243
        } elseif ($this->cover !== $otherDeveloper->cover) {
244
            return false;
245
        }
246
247
        return true;
248
    }
249
250
    /**
251
     * Returns class properties as an array.
252
     *
253
     * @return array class properties as an array
254
     */
255
    public function asArray(): array
256
    {
257
        return [
258
            'id' => $this->id,
259
            'url' => $this->url,
260
            'name' => $this->name,
261
            'description' => $this->description,
262
            'website' => $this->website,
263
            'icon' => $this->icon !== null ? $this->icon->getUrl() : null,
264
            'cover' => $this->cover !== null ? $this->cover->getUrl() : null,
265
            'email' => $this->email,
266
            'address' => $this->address,
267
        ];
268
    }
269
}
270