|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Soluble\MediaTools\Video\Info; |
|
6
|
|
|
|
|
7
|
|
|
use Soluble\MediaTools\Video\Exception\InvalidArgumentException; |
|
8
|
|
|
|
|
9
|
|
|
class AspectRatio |
|
10
|
|
|
{ |
|
11
|
|
|
public const DEFAULT_PROPORTION_SEPARATOR = ':'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var float |
|
15
|
|
|
*/ |
|
16
|
|
|
private $x; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var float |
|
20
|
|
|
*/ |
|
21
|
|
|
private $y; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $separator; |
|
27
|
|
|
|
|
28
|
5 |
|
public function __construct(float $x, float $y, string $separator = self::DEFAULT_PROPORTION_SEPARATOR) |
|
29
|
|
|
{ |
|
30
|
5 |
|
$this->x = $x; |
|
31
|
5 |
|
$this->y = $y; |
|
32
|
5 |
|
$this->separator = $separator; |
|
33
|
5 |
|
} |
|
34
|
|
|
|
|
35
|
2 |
|
public function getX(): float |
|
36
|
|
|
{ |
|
37
|
2 |
|
return $this->x; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
public function getY(): float |
|
41
|
|
|
{ |
|
42
|
2 |
|
return $this->y; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $proportions |
|
47
|
|
|
* |
|
48
|
|
|
* @throws InvalidArgumentException |
|
49
|
|
|
*/ |
|
50
|
5 |
|
public static function createFromString(string $proportions, string $separator = self::DEFAULT_PROPORTION_SEPARATOR): self |
|
51
|
|
|
{ |
|
52
|
5 |
|
if (mb_substr_count($proportions, $separator) !== 1) { |
|
53
|
2 |
|
throw new InvalidArgumentException(sprintf( |
|
54
|
2 |
|
'Cannot parse given proportions: \'%s\' with separator \'%s\' (missing or multiple occurences)', |
|
55
|
2 |
|
$proportions, |
|
56
|
2 |
|
$separator |
|
57
|
|
|
)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
[$x, $y] = explode($separator, $proportions); |
|
61
|
|
|
|
|
62
|
3 |
|
if (!is_numeric($x) || !is_numeric($y)) { |
|
63
|
1 |
|
throw new InvalidArgumentException(sprintf( |
|
64
|
1 |
|
'Cannot parse given proportions: \'%s\', x and y must be valid numerics', |
|
65
|
1 |
|
$proportions |
|
66
|
|
|
)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
return new self((float) $x, (float) $y); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
public function getString(string $separator = self::DEFAULT_PROPORTION_SEPARATOR, int $maxDecimals = null): string |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
4 |
|
return sprintf( |
|
75
|
4 |
|
'%s%s%s', |
|
76
|
4 |
|
$this->getFloatAsString($this->x, $maxDecimals), |
|
77
|
4 |
|
$this->separator, |
|
78
|
4 |
|
$this->getFloatAsString($this->y, $maxDecimals) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function __toString(): string |
|
83
|
|
|
{ |
|
84
|
1 |
|
return $this->getString(self::DEFAULT_PROPORTION_SEPARATOR); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
4 |
|
private function getFloatAsString(float $number, int $maxDecimals = null): string |
|
88
|
|
|
{ |
|
89
|
4 |
|
$n = (string) $number; |
|
90
|
|
|
|
|
91
|
4 |
|
if ($n === (string) ((int) ($number))) { |
|
92
|
4 |
|
return $n; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
if ($maxDecimals === null) { |
|
96
|
1 |
|
return $n; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return (string) $this->truncateFloat($number, $maxDecimals); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
private function truncateFloat(float $number, int $decimals): float |
|
103
|
|
|
{ |
|
104
|
1 |
|
$power = 10 ** $decimals; |
|
105
|
1 |
|
if ($number > 0) { |
|
106
|
1 |
|
return floor($number * $power) / $power; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return ceil($number * $power) / $power; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.