22
33namespace Embed \Adapters ;
44
5- use Embed \Http \Uri ;
5+ use Embed \Http \Url ;
66use Embed \Http \Response ;
77use Embed \Http \ImageResponse ;
88use Embed \Http \DispatcherInterface ;
9- use Embed \Providers \ProviderInterface ;
9+ use Embed \DataInterface ;
10+ use Embed \Providers \Provider ;
1011use Embed \Bag ;
1112
1213/**
1314 * Base class extended by all adapters.
15+ *
16+ * @property null|string $title
17+ * @property null|string $description
18+ * @property null|string $url
19+ * @property null|string $type
20+ * @property array $tags
21+ * @property array $feeds
22+ * @property array $images
23+ * @property array $imagesUrls
24+ * @property null|string $image
25+ * @property null|int $imageWidth
26+ * @property null|int $imageHeight
27+ * @property null|string $code
28+ * @property null|int $width
29+ * @property null|int $height
30+ * @property null|float $aspectRatio
31+ * @property null|string $authorName
32+ * @property null|string $authorUrl
33+ * @property array $providerIcons
34+ * @property array $providerIconsUrls
35+ * @property null|string $providerIcon
36+ * @property null|string $providerName
37+ * @property null|string $providerUrl
38+ * @property null|string $publishedTime
1439 */
15- abstract class Adapter
40+ abstract class Adapter implements DataInterface
1641{
1742 protected $ config ;
1843 protected $ dispatcher ;
1944 protected $ response ;
2045 protected $ providers = [];
2146
2247 /**
23- * {@inheritdoc}
48+ * Checks whether the response is valid to this Adapter.
49+ *
50+ * @param Response $response
51+ *
52+ * @return bool
53+ */
54+ abstract public static function check (Response $ response );
55+
56+ /**
57+ * Constructor.
58+ *
59+ * @param Response $response
60+ * @param array $config
61+ * @param DispatcherInterface $dispatcher
2462 */
2563 public function __construct (Response $ response , array $ config , DispatcherInterface $ dispatcher )
2664 {
@@ -54,23 +92,32 @@ public function __get($name)
5492 }
5593
5694 /**
57- * {@inheritdoc}
95+ * Returns the dispatcher used.
96+ *
97+ * @return DispatcherInterface
5898 */
5999 public function getDispatcher ()
60100 {
61101 return $ this ->dispatcher ;
62102 }
63103
64104 /**
65- * {@inheritdoc}
105+ * Returns the main response instance.
106+ *
107+ * @return Response
66108 */
67109 public function getResponse ()
68110 {
69111 return $ this ->response ;
70112 }
71113
72114 /**
73- * {@inheritdoc}
115+ * Get a config value.
116+ *
117+ * @param string $name
118+ * @param mixed $default
119+ *
120+ * @return string|null
74121 */
75122 public function getConfig ($ name , $ default = null )
76123 {
@@ -80,7 +127,9 @@ public function getConfig($name, $default = null)
80127 }
81128
82129 /**
83- * {@inheritdoc}
130+ * Get all providers.
131+ *
132+ * @return array
84133 */
85134 public function getProviders ()
86135 {
@@ -94,7 +143,7 @@ public function getTitle()
94143 {
95144 $ default = $ this ->url ;
96145
97- return $ this ->getFirstFromProviders (function ($ provider ) {
146+ return $ this ->getFirstFromProviders (function (Provider $ provider ) {
98147 return $ provider ->getTitle ();
99148 }, $ default );
100149 }
@@ -104,7 +153,7 @@ public function getTitle()
104153 */
105154 public function getDescription ()
106155 {
107- return $ this ->getFirstFromProviders (function ($ provider ) {
156+ return $ this ->getFirstFromProviders (function (Provider $ provider ) {
108157 return $ provider ->getDescription ();
109158 });
110159 }
@@ -150,7 +199,7 @@ public function getType()
150199 */
151200 public function getTags ()
152201 {
153- return $ this ->getAllFromProviders (function ($ provider ) {
202+ return $ this ->getAllFromProviders (function (Provider $ provider ) {
154203 return $ provider ->getTags ();
155204 });
156205 }
@@ -160,7 +209,7 @@ public function getTags()
160209 */
161210 public function getFeeds ()
162211 {
163- return $ this ->getAllFromProviders (function ($ provider ) {
212+ return $ this ->getAllFromProviders (function (Provider $ provider ) {
164213 return $ provider ->getFeeds ();
165214 });
166215 }
@@ -193,7 +242,7 @@ public function getCode()
193242 }
194243
195244 //Use only html5 codes
196- $ html5 = array_filter ($ codes , function ($ code ) {
245+ $ html5 = array_filter ($ codes , function (array $ code ) {
197246 return strpos ($ code ['code ' ], '</object> ' ) === false && strpos ($ code ['code ' ], '</embed> ' ) === false ;
198247 });
199248
@@ -215,9 +264,9 @@ public function getCode()
215264 */
216265 public function getUrl ()
217266 {
218- $ default = (string ) $ this ->getResponse ()->getUri ();
267+ $ default = (string ) $ this ->getResponse ()->getUrl ();
219268
220- return $ this ->getFirstFromProviders (function ($ provider ) {
269+ return $ this ->getFirstFromProviders (function (Provider $ provider ) {
221270 return $ provider ->getUrl ();
222271 }, $ default );
223272 }
@@ -227,7 +276,7 @@ public function getUrl()
227276 */
228277 public function getAuthorName ()
229278 {
230- return $ this ->getFirstFromProviders (function ($ provider ) {
279+ return $ this ->getFirstFromProviders (function (Provider $ provider ) {
231280 return $ provider ->getAuthorName ();
232281 });
233282 }
@@ -237,7 +286,7 @@ public function getAuthorName()
237286 */
238287 public function getAuthorUrl ()
239288 {
240- return $ this ->getFirstFromProviders (function ($ provider ) {
289+ return $ this ->getFirstFromProviders (function (Provider $ provider ) {
241290 return $ provider ->getAuthorUrl ();
242291 });
243292 }
@@ -248,8 +297,8 @@ public function getAuthorUrl()
248297 public function getProviderIconsUrls ()
249298 {
250299 $ urls = [
251- $ this ->getResponse ()->getUri ()->getAbsolute ('/favicon.ico ' ),
252- $ this ->getResponse ()->getUri ()->getAbsolute ('/favicon.png ' ),
300+ $ this ->getResponse ()->getUrl ()->getAbsolute ('/favicon.ico ' ),
301+ $ this ->getResponse ()->getUrl ()->getAbsolute ('/favicon.png ' ),
253302 ];
254303
255304 foreach ($ this ->providers as $ provider ) {
@@ -264,15 +313,20 @@ public function getProviderIconsUrls()
264313 }
265314
266315 /**
267- * {@inheritdoc}
316+ * Gets all icon provider urls found
317+ * It returns also the width, height and mime-type.
318+ *
319+ * @return array
268320 */
269321 public function getProviderIcons ()
270322 {
271323 return $ this ->dispatchImagesInfo ($ this ->providerIconsUrls );
272324 }
273325
274326 /**
275- * {@inheritdoc}
327+ * Gets the best icon provider
328+ *
329+ * @return string|null
276330 */
277331 public function getProviderIcon ()
278332 {
@@ -296,9 +350,9 @@ public function getProviderIcon()
296350 */
297351 public function getProviderName ()
298352 {
299- $ default = $ this ->getResponse ()->getUri ()->getDomain ();
353+ $ default = $ this ->getResponse ()->getUrl ()->getDomain ();
300354
301- return $ this ->getFirstFromProviders (function ($ provider ) {
355+ return $ this ->getFirstFromProviders (function (Provider $ provider ) {
302356 return $ provider ->getProviderName ();
303357 }, $ default );
304358 }
@@ -308,10 +362,10 @@ public function getProviderName()
308362 */
309363 public function getProviderUrl ()
310364 {
311- $ uri = $ this ->getResponse ()->getUri ();
312- $ default = $ uri ->getScheme ().':// ' .$ uri ->getDomain (true );
365+ $ url = $ this ->getResponse ()->getUrl ();
366+ $ default = $ url ->getScheme ().':// ' .$ url ->getDomain (true );
313367
314- return $ this ->getFirstFromProviders (function ($ provider ) {
368+ return $ this ->getFirstFromProviders (function (Provider $ provider ) {
315369 return $ provider ->getProviderUrl ();
316370 }, $ default );
317371 }
@@ -321,39 +375,36 @@ public function getProviderUrl()
321375 */
322376 public function getImagesUrls ()
323377 {
324- $ urls = [];
325-
326- foreach ($ this ->providers as $ provider ) {
327- foreach ($ provider ->getImagesUrls () as $ url ) {
328- if (!in_array ($ url , $ urls , true )) {
329- $ urls [] = $ url ;
330- }
331- }
332- }
378+ $ urls = $ this ->getAllFromProviders (function (Provider $ provider ) {
379+ return $ provider ->getImagesUrls ();
380+ });
333381
334382 $ blacklist = $ this ->getConfig ('images_blacklist ' );
335383
336384 if (!empty ($ blacklist )) {
337385 $ urls = array_filter ($ urls , function ($ url ) use ($ blacklist ) {
338- $ uri = Uri::create ($ url );
339-
340- return !$ uri ->match ($ blacklist );
386+ return !Url::create ($ url )->match ($ blacklist );
341387 });
342388 }
343389
344390 return array_values ($ urls );
345391 }
346392
347393 /**
348- * {@inheritdoc}
394+ * Gets all images found in the webpage
395+ * It returns also the width, height and mime-type.
396+ *
397+ * @return array
349398 */
350399 public function getImages ()
351400 {
352401 return $ this ->dispatchImagesInfo ($ this ->imagesUrls );
353402 }
354403
355404 /**
356- * {@inheritdoc}
405+ * Gets the best image
406+ *
407+ * @return string|null
357408 */
358409 public function getImage ()
359410 {
@@ -393,7 +444,9 @@ public function getImage()
393444 }
394445
395446 /**
396- * {@inheritdoc}
447+ * Gets the image width.
448+ *
449+ * @return int|null
397450 */
398451 public function getImageWidth ()
399452 {
@@ -403,7 +456,9 @@ public function getImageWidth()
403456 }
404457
405458 /**
406- * {@inheritdoc}
459+ * Gets the image height.
460+ *
461+ * @return int|null
407462 */
408463 public function getImageHeight ()
409464 {
@@ -433,7 +488,10 @@ public function getHeight()
433488 }
434489
435490 /**
436- * {@inheritdoc}
491+ * Gets the aspect ratio of the embedded widget
492+ * (useful to make it responsive).
493+ *
494+ * @return float|null
437495 */
438496 public function getAspectRatio ()
439497 {
@@ -449,7 +507,7 @@ public function getAspectRatio()
449507 */
450508 public function getPublishedTime ()
451509 {
452- return $ this ->getFirstFromProviders (function ($ provider ) {
510+ return $ this ->getFirstFromProviders (function (Provider $ provider ) {
453511 return $ provider ->getPublishedTime ();
454512 });
455513 }
@@ -459,7 +517,7 @@ public function getPublishedTime()
459517 */
460518 public function getLicense ()
461519 {
462- return $ this ->getFirstFromProviders (function ($ provider ) {
520+ return $ this ->getFirstFromProviders (function (Provider $ provider ) {
463521 return $ provider ->getLicense ();
464522 });
465523 }
@@ -469,7 +527,7 @@ public function getLicense()
469527 */
470528 public function getLinkedData ()
471529 {
472- return $ this ->getAllFromProviders (function ($ provider ) {
530+ return $ this ->getAllFromProviders (function (Provider $ provider ) {
473531 return $ provider ->getLinkedData ();
474532 });
475533 }
@@ -489,14 +547,14 @@ private function dispatchImagesInfo($urls)
489547
490548 $ requests = [];
491549
492- foreach ($ urls as $ uri ) {
493- $ requests [] = Uri ::create ($ uri );
550+ foreach ($ urls as $ url ) {
551+ $ requests [] = Url ::create ($ url );
494552 }
495553
496554 return array_map (
497555 function (ImageResponse $ response ) {
498556 return [
499- 'url ' => (string ) $ response ->getUri (),
557+ 'url ' => (string ) $ response ->getUrl (),
500558 'width ' => $ response ->getWidth (),
501559 'height ' => $ response ->getHeight (),
502560 'size ' => $ response ->getWidth () * $ response ->getHeight (),
0 commit comments