1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP Exif Filesize ValueObject |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/PHPExif/php-exif-common for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2016 Tom Van Herreweghe <[email protected]> |
7
|
|
|
* @license http://github.com/PHPExif/php-exif-common/blob/master/LICENSE MIT License |
8
|
|
|
* @category PHPExif |
9
|
|
|
* @package Common |
10
|
|
|
* @codeCoverageIgnore |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace PHPExif\Common\Data\ValueObject; |
14
|
|
|
|
15
|
|
|
use \InvalidArgumentException; |
16
|
|
|
use \JsonSerializable; |
17
|
|
|
use \RuntimeException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Filesize class |
21
|
|
|
* |
22
|
|
|
* A value object to describe the Filesize |
23
|
|
|
* |
24
|
|
|
* @category PHPExif |
25
|
|
|
* @package Common |
26
|
|
|
*/ |
27
|
|
|
class Filesize implements JsonSerializable |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The filesize in bytes |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
private $bytes; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param int $bytes |
38
|
|
|
* |
39
|
|
|
* @throws InvalidArgumentException If given bytes are not an integer |
40
|
|
|
*/ |
41
|
|
|
public function __construct($bytes) |
42
|
|
|
{ |
43
|
|
|
if (!is_int($bytes)) { |
44
|
|
|
throw new InvalidArgumentException('Given amount in bytes must be an integer'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->setBytes($bytes); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Sets the bytes |
52
|
|
|
* |
53
|
|
|
* @param int $bytes |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
private function setBytes($bytes) |
58
|
|
|
{ |
59
|
|
|
$this->bytes = $bytes; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Getter for bytes |
64
|
|
|
* |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
|
|
public function getBytes() |
68
|
|
|
{ |
69
|
|
|
return $this->bytes; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the filesize in KiloBytes |
74
|
|
|
* |
75
|
|
|
* @return float |
76
|
|
|
*/ |
77
|
|
|
public function getKiloBytes() |
78
|
|
|
{ |
79
|
|
|
return ($this->getBytes() / 1024); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns the filesize in MegaBytes |
84
|
|
|
* |
85
|
|
|
* @return float |
86
|
|
|
*/ |
87
|
|
|
public function getMegaBytes() |
88
|
|
|
{ |
89
|
|
|
return ($this->getKiloBytes() / 1024); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns the filesize in GigaBytes |
94
|
|
|
* |
95
|
|
|
* @return float |
96
|
|
|
*/ |
97
|
|
|
public function getGigaBytes() |
98
|
|
|
{ |
99
|
|
|
return ($this->getMegaBytes() / 1024); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Creates instance from a megabyte string notation |
104
|
|
|
* Examples: |
105
|
|
|
* |
106
|
|
|
* - '5MB' |
107
|
|
|
* - '5 MB' |
108
|
|
|
* - '5.4MB' |
109
|
|
|
* - '5.4 MB' |
110
|
|
|
* |
111
|
|
|
* @param string $filesize |
112
|
|
|
* |
113
|
|
|
* @return Filesize |
114
|
|
|
*/ |
115
|
|
View Code Duplication |
public static function fromMegaBytesString($filesize) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
if (!is_string($filesize)) { |
118
|
|
|
throw new InvalidArgumentException('Given filesize must be a string'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!preg_match('#^([0-9]*\.[0-9]+|[0-9]*)\s?MB$#', $filesize, $matches)) { |
122
|
|
|
throw new RuntimeException('Given filesize is not in a valid format. Need: "<float> MB"'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$megabytes = $matches[1]; |
126
|
|
|
|
127
|
|
|
if (($filtered = filter_var($megabytes, FILTER_VALIDATE_INT)) !== false) { |
128
|
|
|
$megabytes = $filtered; |
129
|
|
|
} else { |
130
|
|
|
$megabytes = (float) $megabytes; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return self::fromMegaBytes($megabytes); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Creates new instance from megabyte amount |
138
|
|
|
* Examples: |
139
|
|
|
* |
140
|
|
|
* - 5 |
141
|
|
|
* - 5.4 |
142
|
|
|
* |
143
|
|
|
* @param int|float $megabytes |
144
|
|
|
* |
145
|
|
|
* @return Filesize |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public static function fromMegaBytes($megabytes) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
if (!is_int($megabytes) && !is_float($megabytes)) { |
150
|
|
|
throw new InvalidArgumentException('Given amount must be an integer or a float'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$bytes = $megabytes * pow(1024, 2); |
154
|
|
|
|
155
|
|
|
return new self((int) round($bytes, 0)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Creates instance from a kilobyte string notation |
160
|
|
|
* Examples: |
161
|
|
|
* |
162
|
|
|
* - '5KB' |
163
|
|
|
* - '5 KB' |
164
|
|
|
* - '5.4KB' |
165
|
|
|
* - '5.4 KB' |
166
|
|
|
* |
167
|
|
|
* @param string $filesize |
168
|
|
|
* |
169
|
|
|
* @return Filesize |
170
|
|
|
*/ |
171
|
|
View Code Duplication |
public static function fromKiloBytesString($filesize) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
if (!is_string($filesize)) { |
174
|
|
|
throw new InvalidArgumentException('Given filesize must be a string'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if (!preg_match('#^([0-9]*\.[0-9]+|[0-9]*)\s?KB$#', $filesize, $matches)) { |
178
|
|
|
throw new RuntimeException('Given filesize is not in a valid format. Need: "<float> KB"'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$kilobytes = $matches[1]; |
182
|
|
|
|
183
|
|
|
if (($filtered = filter_var($kilobytes, FILTER_VALIDATE_INT)) !== false) { |
184
|
|
|
$kilobytes = $filtered; |
185
|
|
|
} else { |
186
|
|
|
$kilobytes = (float) $kilobytes; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return self::fromKiloBytes($kilobytes); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Creates new instance from kilobyte amount |
194
|
|
|
* Examples: |
195
|
|
|
* |
196
|
|
|
* - 5 |
197
|
|
|
* - 5.4 |
198
|
|
|
* |
199
|
|
|
* @param int|float $kilobytes |
200
|
|
|
* |
201
|
|
|
* @return Filesize |
202
|
|
|
*/ |
203
|
|
View Code Duplication |
public static function fromKiloBytes($kilobytes) |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
if (!is_int($kilobytes) && !is_float($kilobytes)) { |
206
|
|
|
throw new InvalidArgumentException('Given amount must be an integer or a float'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$bytes = $kilobytes * 1024; |
210
|
|
|
|
211
|
|
|
return new self((int) round($bytes, 0)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Creates a new instance from given Filesize object |
216
|
|
|
* |
217
|
|
|
* @param Filesize $filesize |
218
|
|
|
* |
219
|
|
|
* @return Filesize |
220
|
|
|
*/ |
221
|
|
|
public static function fromFilesize(Filesize $filesize) |
222
|
|
|
{ |
223
|
|
|
return new self( |
224
|
|
|
$filesize->getBytes() |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @inheritDoc |
230
|
|
|
* |
231
|
|
|
* @return int |
232
|
|
|
*/ |
233
|
|
|
public function jsonSerialize() |
234
|
|
|
{ |
235
|
|
|
return $this->getBytes(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Returns string representation |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
public function __toString() |
244
|
|
|
{ |
245
|
|
|
return (string) $this->getBytes(); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.