1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Soluble\MediaTools\Video\Info\Util; |
6
|
|
|
|
7
|
|
|
use Soluble\MediaTools\Video\Exception\UnexpectedMetadataException; |
8
|
|
|
use Webmozart\Assert\Assert; |
9
|
|
|
|
10
|
|
|
class MetadataTypeSafeReader |
11
|
|
|
{ |
12
|
|
|
/** @var array<string, mixed> */ |
13
|
|
|
private $streamMetadata; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param array<string, mixed> $streamMetadata |
17
|
|
|
*/ |
18
|
4 |
|
public function __construct(array $streamMetadata) |
19
|
|
|
{ |
20
|
4 |
|
$this->streamMetadata = $streamMetadata; |
21
|
4 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $key metadata key |
25
|
|
|
* |
26
|
|
|
* @throws UnexpectedMetadataException |
27
|
|
|
*/ |
28
|
3 |
|
public function getKeyIntValue(string $key): int |
29
|
|
|
{ |
30
|
|
|
try { |
31
|
3 |
|
Assert::integerish( |
32
|
3 |
|
$this->streamMetadata[$key] ?? '', |
33
|
3 |
|
"The ffprobe/videoInfo metadata '$key' is expected to be an integer. Got: %s" |
34
|
|
|
); |
35
|
1 |
|
} catch (\Throwable $e) { |
36
|
1 |
|
throw new UnexpectedMetadataException($e->getMessage()); |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
return (int) $this->streamMetadata[$key]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $key metadata key |
44
|
|
|
* |
45
|
|
|
* @throws UnexpectedMetadataException |
46
|
|
|
*/ |
47
|
3 |
|
public function getKeyIntOrNullValue(string $key): ?int |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
3 |
|
Assert::nullOrIntegerish( |
51
|
3 |
|
$this->streamMetadata[$key] ?? null, |
52
|
3 |
|
"The ffprobe/videoInfo metadata '$key' is expected to be an integer or null. Got: %s" |
53
|
|
|
); |
54
|
|
|
} catch (\Throwable $e) { |
55
|
|
|
throw new UnexpectedMetadataException($e->getMessage()); |
56
|
|
|
} |
57
|
3 |
|
if (isset($this->streamMetadata[$key])) { |
58
|
2 |
|
return (int) $this->streamMetadata[$key]; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $key metadata key |
66
|
|
|
* |
67
|
|
|
* @throws UnexpectedMetadataException |
68
|
|
|
*/ |
69
|
3 |
|
public function getKeyFloatValue(string $key): float |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
3 |
|
Assert::numeric( |
73
|
3 |
|
$this->streamMetadata[$key] ?? '', |
74
|
3 |
|
"The ffprobe/videoInfo metadata '$key' is expected to be a float. Got: %s" |
75
|
|
|
); |
76
|
1 |
|
} catch (\Throwable $e) { |
77
|
1 |
|
throw new UnexpectedMetadataException($e->getMessage()); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
return (float) $this->streamMetadata[$key]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $key metadata key |
85
|
|
|
* |
86
|
|
|
* @throws UnexpectedMetadataException |
87
|
|
|
*/ |
88
|
1 |
|
public function getKeyFloatOrNullValue(string $key): ?float |
89
|
|
|
{ |
90
|
|
|
try { |
91
|
1 |
|
Assert::nullOrNumeric( |
92
|
1 |
|
$this->streamMetadata[$key] ?? null, |
93
|
1 |
|
"The ffprobe/videoInfo metadata '$key' is expected to be a float or null. Got: %s" |
94
|
|
|
); |
95
|
|
|
} catch (\Throwable $e) { |
96
|
|
|
throw new UnexpectedMetadataException($e->getMessage()); |
97
|
|
|
} |
98
|
1 |
|
if (isset($this->streamMetadata[$key])) { |
99
|
1 |
|
return (float) $this->streamMetadata[$key]; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $key metadata key |
107
|
|
|
* |
108
|
|
|
* @throws UnexpectedMetadataException |
109
|
|
|
*/ |
110
|
3 |
|
public function getKeyStringOrNullValue(string $key): ?string |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
3 |
|
Assert::nullOrString( |
114
|
3 |
|
$this->streamMetadata[$key] ?? null, |
115
|
3 |
|
"The ffprobe/videoInfo metadata '$key' is expected to be a string or null. Got: %s" |
116
|
|
|
); |
117
|
1 |
|
} catch (\Throwable $e) { |
118
|
1 |
|
throw new UnexpectedMetadataException($e->getMessage()); |
119
|
|
|
} |
120
|
2 |
|
if (isset($this->streamMetadata[$key])) { |
121
|
2 |
|
return (string) $this->streamMetadata[$key]; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $key metadata key |
129
|
|
|
* |
130
|
|
|
* @throws UnexpectedMetadataException |
131
|
|
|
*/ |
132
|
2 |
|
public function getKeyStringValue(string $key): string |
133
|
|
|
{ |
134
|
|
|
try { |
135
|
2 |
|
Assert::string( |
136
|
2 |
|
$this->streamMetadata[$key] ?? null, |
137
|
2 |
|
"The ffprobe/videoInfo metadata '$key' is expected to be a string. Got: %s" |
138
|
|
|
); |
139
|
1 |
|
} catch (\Throwable $e) { |
140
|
1 |
|
throw new UnexpectedMetadataException($e->getMessage()); |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
return (string) $this->streamMetadata[$key]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|