1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * Copyright (c) Ne-Lexa |
||
7 | * |
||
8 | * For the full copyright and license information, please view |
||
9 | * the LICENSE file that was distributed with this source code. |
||
10 | * |
||
11 | * @see https://github.com/Ne-Lexa/google-play-scraper |
||
12 | */ |
||
13 | |||
14 | namespace Nelexa\GPlay\Model; |
||
15 | |||
16 | /** |
||
17 | * Contains information about the image. |
||
18 | */ |
||
19 | class ImageInfo implements \JsonSerializable |
||
20 | { |
||
21 | use JsonSerializableTrait; |
||
22 | |||
23 | /** @var string Image url. */ |
||
24 | private $url; |
||
25 | |||
26 | /** @var string Local image filename. */ |
||
27 | private $filename; |
||
28 | |||
29 | /** @var string Image mime-type. */ |
||
30 | private $mimeType; |
||
31 | |||
32 | /** @var string Image file extension. */ |
||
33 | private $extension; |
||
34 | |||
35 | /** @var int Image width. */ |
||
36 | private $width; |
||
37 | |||
38 | /** @var int Image height. */ |
||
39 | private $height; |
||
40 | |||
41 | /** @var int Image file size. */ |
||
42 | private $filesize; |
||
43 | |||
44 | /** |
||
45 | * Creates an object with information about the image saved to disk. |
||
46 | * |
||
47 | * @param string $url image url |
||
48 | * @param string $filename local image filename |
||
49 | */ |
||
50 | 2 | public function __construct(string $url, string $filename) |
|
51 | { |
||
52 | 2 | $this->url = $url; |
|
53 | 2 | $imageInfo = getimagesize($filename); |
|
54 | |||
55 | 2 | if (!$imageInfo) { |
|
0 ignored issues
–
show
|
|||
56 | throw new \RuntimeException('Invalid image: ' . $filename); |
||
57 | } |
||
58 | 2 | $this->filename = $filename; |
|
59 | 2 | $this->mimeType = $imageInfo['mime']; |
|
60 | 2 | [$this->width, $this->height, $imageType] = $imageInfo; |
|
61 | 2 | $this->extension = str_replace( |
|
62 | 'jpeg', |
||
63 | 'jpg', |
||
64 | 2 | image_type_to_extension($imageType, false) |
|
65 | ); |
||
66 | 2 | $this->filesize = filesize($filename); |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Returns the url of the image. |
||
71 | * |
||
72 | * @return string image url |
||
73 | */ |
||
74 | public function getUrl(): string |
||
75 | { |
||
76 | return $this->url; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Returns the path to save the image file. |
||
81 | * |
||
82 | * @return string image filename |
||
83 | */ |
||
84 | public function getFilename(): string |
||
85 | { |
||
86 | return $this->filename; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Returns the mime type of the image. |
||
91 | * |
||
92 | * @return string image mime-type |
||
93 | */ |
||
94 | public function getMimeType(): string |
||
95 | { |
||
96 | return $this->mimeType; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Returns the image file extension. |
||
101 | * |
||
102 | * @return string image file extension |
||
103 | */ |
||
104 | public function getExtension(): string |
||
105 | { |
||
106 | return $this->extension; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Returns the width of the image. |
||
111 | * |
||
112 | * @return int image width |
||
113 | */ |
||
114 | 1 | public function getWidth(): int |
|
115 | { |
||
116 | 1 | return $this->width; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Returns the height of the image. |
||
121 | * |
||
122 | * @return int image height |
||
123 | */ |
||
124 | 1 | public function getHeight(): int |
|
125 | { |
||
126 | 1 | return $this->height; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Returns the size of the image file. |
||
131 | * |
||
132 | * @return int image file size |
||
133 | */ |
||
134 | public function getFilesize(): int |
||
135 | { |
||
136 | return $this->filesize; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Returns class properties as an array. |
||
141 | * |
||
142 | * @return array class properties as an array |
||
143 | */ |
||
144 | public function asArray(): array |
||
145 | { |
||
146 | return [ |
||
147 | 'url' => $this->url, |
||
148 | 'path' => $this->filename, |
||
149 | 'mimeType' => $this->mimeType, |
||
150 | 'extension' => $this->extension, |
||
151 | 'width' => $this->width, |
||
152 | 'height' => $this->height, |
||
153 | ]; |
||
154 | } |
||
155 | } |
||
156 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.