Skip to content

Commit d3208b3

Browse files
committed
Preserve extra provider config metadata
1 parent 7b6336c commit d3208b3

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎src/Provider/ProviderConfig.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ final class ProviderConfig {
2727
* @param string|null $fetchMatch Secondary HTTP fetch regex.
2828
* @param bool $supportsTimestamp Whether provider supports timestamps.
2929
* @param string|null $timestampParam Embed query parameter for timestamp values.
30+
* @param array<string, mixed> $extra Extra provider metadata.
3031
*/
3132
public function __construct(
3233
public readonly string $name,
@@ -41,6 +42,7 @@ public function __construct(
4142
public readonly ?string $fetchMatch = null,
4243
public readonly bool $supportsTimestamp = false,
4344
public readonly ?string $timestampParam = null,
45+
public readonly array $extra = [],
4446
) {
4547
}
4648

@@ -69,6 +71,21 @@ public static function fromArray(array $data): self {
6971
throw ProviderConfigException::missingField('embed-height', $data);
7072
}
7173

74+
$knownKeys = [
75+
'name',
76+
'website',
77+
'url-match',
78+
'embed-width',
79+
'embed-height',
80+
'slug',
81+
'iframe-player',
82+
'image-src',
83+
'id',
84+
'fetch-match',
85+
'supports-timestamp',
86+
'timestamp-param',
87+
];
88+
7289
return new self(
7390
name: $data['name'],
7491
website: $data['website'],
@@ -82,6 +99,7 @@ public static function fromArray(array $data): self {
8299
fetchMatch: $data['fetch-match'] ?? null,
83100
supportsTimestamp: !empty($data['supports-timestamp']),
84101
timestampParam: $data['timestamp-param'] ?? null,
102+
extra: array_diff_key($data, array_flip($knownKeys)),
85103
);
86104
}
87105

@@ -98,6 +116,7 @@ public function toArray(): array {
98116
'embed-width' => $this->embedWidth,
99117
'embed-height' => $this->embedHeight,
100118
];
119+
$array += $this->extra;
101120

102121
if ($this->slug !== null) {
103122
$array['slug'] = $this->slug;

‎tests/Provider/ProviderConfigTest.php‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,31 @@ public function testFromArrayPreservesPercentageDimensions(): void {
7070
$this->assertSame('400', $config->embedHeight);
7171
}
7272

73+
public function testFromArrayPreservesExtraProviderMetadata(): void {
74+
$data = [
75+
'name' => 'ExtraProvider',
76+
'website' => 'https://extra.example.com',
77+
'url-match' => 'extra\\.example\\.com/([a-z0-9]+)',
78+
'embed-width' => '640',
79+
'embed-height' => '360',
80+
'iframe-player' => '//extra.example.com/embed/$2',
81+
'replace' => [
82+
'foo' => 'bar',
83+
],
84+
'status' => 'legacy',
85+
];
86+
87+
$config = ProviderConfig::fromArray($data);
88+
$array = $config->toArray();
89+
90+
$this->assertSame([
91+
'foo' => 'bar',
92+
], $config->extra['replace']);
93+
$this->assertSame('legacy', $config->extra['status']);
94+
$this->assertSame($data['replace'], $array['replace']);
95+
$this->assertSame('legacy', $array['status']);
96+
}
97+
7398
public function testFromArrayMissingName(): void {
7499
$this->expectException(ProviderConfigException::class);
75100

@@ -126,6 +151,26 @@ public function testToArray(): void {
126151
$this->assertArrayNotHasKey('timestamp-param', $array);
127152
}
128153

154+
public function testToArrayTypedFieldsWinOverExtraFields(): void {
155+
$config = new ProviderConfig(
156+
name: 'Test',
157+
website: 'https://test.com',
158+
urlMatch: 'pattern',
159+
embedWidth: 640,
160+
embedHeight: 360,
161+
iframePlayer: '//test.com/embed/$2',
162+
extra: [
163+
'name' => 'Wrong',
164+
'status' => 'legacy',
165+
],
166+
);
167+
168+
$array = $config->toArray();
169+
170+
$this->assertSame('Test', $array['name']);
171+
$this->assertSame('legacy', $array['status']);
172+
}
173+
129174
public function testGetUrlMatchPatterns(): void {
130175
$configSingle = new ProviderConfig(
131176
name: 'Test',

0 commit comments

Comments
 (0)