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