Flickr.php
| 1 | <?php |
| 2 | /** |
| 3 | * Flickr.php |
| 4 | * |
| 5 | * @package Embera |
| 6 | * @author Michael Pratt <yo@michael-pratt.com> |
| 7 | * @link http://www.michael-pratt.com/ |
| 8 | * |
| 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | * file that was distributed with this source code. |
| 11 | */ |
| 12 | |
| 13 | namespace Embera\Provider; |
| 14 | |
| 15 | use Embera\Url; |
| 16 | |
| 17 | /** |
| 18 | * Flickr Provider |
| 19 | * @link https://flickr.com |
| 20 | */ |
| 21 | class Flickr extends ProviderAdapter implements ProviderInterface |
| 22 | { |
| 23 | /** inline {@inheritdoc} */ |
| 24 | protected $endpoint = 'https://www.flickr.com/services/oembed?format=json'; |
| 25 | |
| 26 | /** inline {@inheritdoc} */ |
| 27 | protected static $hosts = [ |
| 28 | 'flickr.com', 'flic.kr' |
| 29 | ]; |
| 30 | |
| 31 | /** inline {@inheritdoc} */ |
| 32 | protected $httpsSupport = true; |
| 33 | |
| 34 | /** inline {@inheritdoc} */ |
| 35 | public function validateUrl(Url $url) |
| 36 | { |
| 37 | return (bool) ( |
| 38 | preg_match('~/photos/(?:[^/]+)/(?:[0-9]+)/?~i', (string) $url) || |
| 39 | preg_match('~flic\.kr/p/(?:[^/]+)~i', (string) $url) |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | /** inline {@inheritdoc} */ |
| 44 | public function normalizeUrl(Url $url) |
| 45 | { |
| 46 | if (preg_match('~/photos/([^/]+)/([^/]+)/?~i', (string) $url, $m)) { |
| 47 | $url->overwrite('https://www.flickr.com/photos/' . $m['1'] . '/' . $m['2'] . '/'); |
| 48 | } |
| 49 | |
| 50 | $url->convertToHttps(); |
| 51 | $url->removeQueryString(); |
| 52 | |
| 53 | return $url; |
| 54 | } |
| 55 | |
| 56 | /** inline {@inheritdoc} */ |
| 57 | public function modifyResponse(array $response = []) |
| 58 | { |
| 59 | $check = [ |
| 60 | 'url', 'thumbnail_url', 'thumbnail_width', 'thumbnail_height', 'title' |
| 61 | ]; |
| 62 | |
| 63 | foreach ($check as $c) { |
| 64 | if (!isset($response[$c])) { |
| 65 | return $response; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | $html = []; |
| 70 | $html[] = '<a href="' . $response['url'] . '" target="_blank">'; |
| 71 | $html []= '<img class="flickr" src="' . $response['thumbnail_url'] . '" width="' . $response['thumbnail_width'] . '" height="' . $response['thumbnail_height'] . '" alt="" title="" >'; |
| 72 | $html[] = '</a>'; |
| 73 | |
| 74 | $response['html_alternative'] = implode('', $html); |
| 75 | |
| 76 | return $response; |
| 77 | } |
| 78 | |
| 79 | } |
| 80 |