1515use MediaEmbed \Provider \ProviderLoaderInterface ;
1616use 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.
2925class 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
0 commit comments