Skip to content

Commit c2396c5

Browse files
committed
Cleanup.
1 parent 7a29c64 commit c2396c5

3 files changed

Lines changed: 60 additions & 80 deletions

File tree

‎src/MediaEmbed.php‎

Lines changed: 51 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
use MediaEmbed\Provider\ProviderLoaderInterface;
1616
use URLify;
1717

18-
if (!defined('DS')) {
19-
define('DS', DIRECTORY_SEPARATOR);
20-
}
21-
2218
/**
2319
* A utility that generates HTML embed tags for audio or video located on a given URL.
2420
* It also parses and validates given media URLs.
@@ -29,21 +25,25 @@
2925
class MediaEmbed {
3026

3127
/**
32-
* @var array<string>
28+
* Last URL match result.
29+
*
30+
* @var array<string>|null
3331
*/
34-
protected ?array $_match = null;
32+
protected ?array $lastMatch = null;
3533

3634
/**
35+
* Registered provider configurations.
36+
*
3737
* @var array<string, array<string, mixed>>
3838
*/
39-
protected array $_hosts = [];
39+
protected array $providers = [];
4040

4141
/**
42-
* See MediaObject for details
42+
* Configuration options passed to MediaObject.
4343
*
4444
* @var array<string, mixed>
4545
*/
46-
public array $config = [];
46+
protected array $config = [];
4747

4848
/**
4949
* HTTP client for fetching remote content.
@@ -97,12 +97,12 @@ public function __construct(
9797
$stubs = $providerLoader->load();
9898
} else {
9999
if ($stubsPath === null) {
100-
$stubsPath = dirname(__DIR__) . DS . 'data' . DS . 'stubs.php';
100+
$stubsPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'stubs.php';
101101
}
102102
$loader = new PhpFileLoader($stubsPath);
103103
$stubs = $loader->load();
104104
}
105-
$this->setHosts($stubs);
105+
$this->registerProviders($stubs);
106106
$this->config = $config + $this->config;
107107

108108
// Load custom providers from config file if specified
@@ -132,10 +132,10 @@ public function parseId(string $id, string $host, array $config = []): ?MediaObj
132132
return null;
133133
}
134134

135-
if (empty($this->_hosts[$host])) {
135+
if (empty($this->providers[$host])) {
136136
return null;
137137
}
138-
$stub = $this->_hosts[$host];
138+
$stub = $this->providers[$host];
139139
$config += $this->config;
140140

141141
$stub['id'] = $id;
@@ -160,21 +160,16 @@ public function parseIdOrFail(string $id, string $host, array $config = []): Med
160160
throw new InvalidUrlException('', 'ID and host are required.');
161161
}
162162

163-
if (empty($this->_hosts[$host])) {
163+
if (empty($this->providers[$host])) {
164164
throw new ProviderNotFoundException($host);
165165
}
166-
$stub = $this->_hosts[$host];
166+
$stub = $this->providers[$host];
167167
$config += $this->config;
168168

169169
$stub['id'] = $id;
170170
$stub['reverse'] = true;
171171

172-
$object = $this->object($stub, $config);
173-
if ($object === null) {
174-
throw new ProviderNotFoundException($host);
175-
}
176-
177-
return $object;
172+
return $this->object($stub, $config);
178173
}
179174

180175
/**
@@ -193,15 +188,15 @@ public function parseUrl(string $url, array $config = []): ?MediaObject {
193188
}
194189

195190
$stub = $result->providerStub;
196-
$this->_match = $result->matches;
191+
$this->lastMatch = $result->matches;
197192

198193
if (!empty($stub['fetch-match'])) {
199-
if (!$this->_parseLink($url, $stub['fetch-match'])) {
194+
if (!$this->parseLink($url, $stub['fetch-match'])) {
200195
return null;
201196
}
202197
}
203198

204-
$stub['match'] = $this->_match;
199+
$stub['match'] = $this->lastMatch;
205200

206201
return $this->object($stub, $config + $this->config);
207202
}
@@ -222,22 +217,17 @@ public function parseUrlOrFail(string $url, array $config = []): MediaObject {
222217
}
223218

224219
$stub = $result->providerStub;
225-
$this->_match = $result->matches;
220+
$this->lastMatch = $result->matches;
226221

227222
if (!empty($stub['fetch-match'])) {
228-
if (!$this->_parseLink($url, $stub['fetch-match'])) {
223+
if (!$this->parseLink($url, $stub['fetch-match'])) {
229224
throw new FetchException($url, sprintf('Failed to fetch and match content from URL: %s', $url));
230225
}
231226
}
232227

233-
$stub['match'] = $this->_match;
234-
235-
$object = $this->object($stub, $config + $this->config);
236-
if ($object === null) {
237-
throw new InvalidUrlException($url);
238-
}
228+
$stub['match'] = $this->lastMatch;
239229

240-
return $object;
230+
return $this->object($stub, $config + $this->config);
241231
}
242232

243233
/**
@@ -247,7 +237,7 @@ public function parseUrlOrFail(string $url, array $config = []): MediaObject {
247237
*/
248238
public function getUrlMatcher(): UrlMatcher {
249239
if ($this->urlMatcher === null) {
250-
$this->urlMatcher = new UrlMatcher($this->_hosts);
240+
$this->urlMatcher = new UrlMatcher($this->providers);
251241
}
252242

253243
return $this->urlMatcher;
@@ -260,7 +250,7 @@ public function getUrlMatcher(): UrlMatcher {
260250
* @param string $regex
261251
* @return bool
262252
*/
263-
protected function _parseLink(string $url, string $regex): bool {
253+
protected function parseLink(string $url, string $regex): bool {
264254
$content = $this->httpClient->get($url);
265255
if (!$content) {
266256
return false;
@@ -272,7 +262,7 @@ protected function _parseLink(string $url, string $regex): bool {
272262
}
273263

274264
if (preg_match('~' . $regex . '~imu', $source, $match)) {
275-
$this->_match = $match;
265+
$this->lastMatch = $match;
276266

277267
return true;
278268
}
@@ -281,24 +271,22 @@ protected function _parseLink(string $url, string $regex): bool {
281271
}
282272

283273
/**
284-
* Set custom stubs overwriting the default ones.
274+
* Register multiple provider configurations.
285275
*
286-
* @param array<string, array<string, mixed>> $stubs Same format as in the stubs.php file.
287-
* @param bool $reset If default ones should be resetted/removed.
288-
* @return $this
276+
* @param array<array<string, mixed>> $stubs Provider configuration arrays.
277+
* @param bool $reset Whether to reset existing providers.
278+
* @return void
289279
*/
290-
public function setHosts(array $stubs, bool $reset = false) {
280+
protected function registerProviders(array $stubs, bool $reset = false): void {
291281
if ($reset) {
292-
$this->_hosts = [];
282+
$this->providers = [];
293283
}
294284
foreach ($stubs as $stub) {
295-
$slug = $this->_slug($stub['name']);
296-
$this->_hosts[$slug] = $stub;
285+
$slug = $this->slug($stub['name']);
286+
$this->providers[$slug] = $stub;
297287
}
298288

299289
$this->urlMatcher = null; // Reset matcher when hosts change
300-
301-
return $this;
302290
}
303291

304292
/**
@@ -350,7 +338,7 @@ public function loadProvidersFromLoader(ProviderLoaderInterface $loader, bool $r
350338
}
351339

352340
$providers = $loader->load();
353-
$this->setHosts($providers, $reset);
341+
$this->registerProviders($providers, $reset);
354342

355343
return $this;
356344
}
@@ -362,7 +350,7 @@ public function loadProvidersFromLoader(ProviderLoaderInterface $loader, bool $r
362350
public function getHosts(array $whitelist = []): array {
363351
if ($whitelist) {
364352
$res = [];
365-
foreach ($this->_hosts as $slug => $host) {
353+
foreach ($this->providers as $slug => $host) {
366354
if (!in_array($slug, $whitelist, true)) {
367355
continue;
368356
}
@@ -372,7 +360,7 @@ public function getHosts(array $whitelist = []): array {
372360
return $res;
373361
}
374362

375-
return $this->_hosts;
363+
return $this->providers;
376364
}
377365

378366
/**
@@ -382,11 +370,11 @@ public function getHosts(array $whitelist = []): array {
382370
* @return \MediaEmbed\Provider\ProviderConfig|null Provider config or null if not found.
383371
*/
384372
public function getProvider(string $alias): ?ProviderConfig {
385-
if (empty($this->_hosts[$alias])) {
373+
if (empty($this->providers[$alias])) {
386374
return null;
387375
}
388376

389-
return ProviderConfig::fromArray($this->_hosts[$alias]);
377+
return ProviderConfig::fromArray($this->providers[$alias]);
390378
}
391379

392380
/**
@@ -397,11 +385,11 @@ public function getProvider(string $alias): ?ProviderConfig {
397385
* @return \MediaEmbed\Provider\ProviderConfig Provider config.
398386
*/
399387
public function getProviderOrFail(string $alias): ProviderConfig {
400-
if (empty($this->_hosts[$alias])) {
388+
if (empty($this->providers[$alias])) {
401389
throw new ProviderNotFoundException($alias);
402390
}
403391

404-
return ProviderConfig::fromArray($this->_hosts[$alias]);
392+
return ProviderConfig::fromArray($this->providers[$alias]);
405393
}
406394

407395
/**
@@ -424,9 +412,9 @@ public function getProviders(array $whitelist = []): ProviderCollection {
424412
* @return $this
425413
*/
426414
public function addProviderConfig(ProviderConfig $config, bool $override = false) {
427-
$slug = $config->slug ?? $this->_slug($config->name);
415+
$slug = $config->slug ?? $this->slug($config->name);
428416

429-
if (!$override && isset($this->_hosts[$slug])) {
417+
if (!$override && isset($this->providers[$slug])) {
430418
return $this;
431419
}
432420

@@ -435,27 +423,22 @@ public function addProviderConfig(ProviderConfig $config, bool $override = false
435423
$array['slug'] = $slug;
436424
}
437425

438-
$this->_hosts[$slug] = $array;
426+
$this->providers[$slug] = $array;
439427
$this->urlMatcher = null; // Reset matcher when providers change
440428

441429
return $this;
442430
}
443431

444432
/**
445-
* @param array<string, mixed>|string $stub
446-
* @param array<string, mixed> $config
433+
* Create a MediaObject from provider stub data.
447434
*
448-
* @return \MediaEmbed\Object\MediaObject|null
435+
* @param array<string, mixed> $stub Provider configuration array.
436+
* @param array<string, mixed> $config Additional configuration.
437+
* @return \MediaEmbed\Object\MediaObject
449438
*/
450-
public function object($stub, array $config = []): ?MediaObject {
451-
if (!is_array($stub)) {
452-
if (empty($this->_hosts[$stub])) {
453-
return null;
454-
}
455-
$stub = $this->_hosts[$stub];
456-
}
439+
protected function object(array $stub, array $config = []): MediaObject {
457440
if (!isset($stub['slug']) && !empty($stub['name'])) {
458-
$stub['slug'] = $this->_slug($stub['name']);
441+
$stub['slug'] = $this->slug($stub['name']);
459442
}
460443

461444
return new MediaObject($stub, $config);
@@ -467,7 +450,7 @@ public function object($stub, array $config = []): ?MediaObject {
467450
* @param string $text
468451
* @return string
469452
*/
470-
protected function _slug(string $text): string {
453+
protected function slug(string $text): string {
471454
return URLify::filter($text);
472455
}
473456

‎src/Provider/ProviderConfig.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ final class ProviderConfig {
1818
* @param string $name Display name of the provider.
1919
* @param string $website Provider's website URL.
2020
* @param array<string>|string $urlMatch URL matching regex pattern(s).
21-
* @param int|string $embedWidth Default embed width.
22-
* @param int|string $embedHeight Default embed height.
21+
* @param string|int $embedWidth Default embed width.
22+
* @param string|int $embedHeight Default embed height.
2323
* @param string|null $slug URL-safe identifier (auto-generated from name if not provided).
2424
* @param string|null $iframePlayer iframe src template URL.
2525
* @param string|null $imageSrc Thumbnail image URL template.

‎tests/MediaEmbedTest.php‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,16 @@ class MediaEmbedTest extends TestCase {
104104
];
105105

106106
/**
107-
* Test Generation of a basic youtube MediaObject (empty)
107+
* Test getting a provider configuration.
108108
*
109109
* @return void
110110
*/
111-
public function testObject(): void {
111+
public function testGetProvider(): void {
112112
$MediaEmbed = new MediaEmbed();
113-
$Object = $MediaEmbed->object('youtube');
114-
$this->assertTrue($Object !== null);
115-
$result = $Object->name();
116-
$this->assertSame('YouTube', $result);
117-
118-
$result = $Object->id();
119-
$this->assertSame('', $result);
113+
$provider = $MediaEmbed->getProvider('youtube');
114+
$this->assertNotNull($provider);
115+
$this->assertSame('YouTube', $provider->name);
116+
$this->assertSame('https://www.youtube.com', $provider->website);
120117
}
121118

122119
/**
@@ -198,7 +195,7 @@ public function testYoutube(): void {
198195
$icon = $Object->icon();
199196
$this->assertNotEmpty($icon);
200197

201-
$location = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DS;
198+
$location = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
202199
$filename = $Object->saveIcon($location, $icon);
203200
$this->assertSame('youtube.png', $filename);
204201

0 commit comments

Comments
 (0)