GettyImages.php
| 1 | <?php |
| 2 | /** |
| 3 | * GettyImages.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 | * GettyImages Provider |
| 19 | * @link https://gty.im |
| 20 | * @link https://developers.gettyimages.com/api/oembed/ |
| 21 | */ |
| 22 | class GettyImages extends ProviderAdapter implements ProviderInterface |
| 23 | { |
| 24 | /** inline {@inheritdoc} */ |
| 25 | protected $endpoint = 'https://embed.gettyimages.com/oembed?format=json&caller=embera'; |
| 26 | |
| 27 | /** inline {@inheritdoc} */ |
| 28 | protected static $hosts = [ |
| 29 | 'gty.im', '*.gettyimages.com', '*.gettyimages.com.mx', '*.gettyimages.com.au', '*.gettyimages.com.be', |
| 30 | '*.gettyimages.com.br', '*.gettyimages.com.ca', '*.gettyimages.com.de', '*.gettyimages.es', '*.gettyimages.fr', |
| 31 | '*.gettyimages.in', '*.gettyimages.ie', '*.gettyimages.it', '*.gettyimages.nl', '*.gettyimages.no', '*.gettyimages.co.nz', |
| 32 | '*.gettyimages.at', '*.gettyimages.pt', '*.gettyimages.ch', '*.gettyimages.fi', '*.gettyimages.se', '*.gettyimages.ae', |
| 33 | '*.gettyimages.co.uk', '*.gettyimages.hk', '*.gettyimages.co.jp' |
| 34 | ]; |
| 35 | |
| 36 | /** inline {@inheritdoc} */ |
| 37 | protected $allowedParams = [ 'maxwidth', 'maxheight', 'caller', 'caption', 'tld' ]; |
| 38 | |
| 39 | /** inline {@inheritdoc} */ |
| 40 | protected $httpsSupport = false; |
| 41 | |
| 42 | /** inline {@inheritdoc} */ |
| 43 | public function validateUrl(Url $url) |
| 44 | { |
| 45 | return (bool) (preg_match('~gty\.im/([^/]+)$~i', (string) $url)); |
| 46 | } |
| 47 | |
| 48 | /** inline {@inheritdoc} */ |
| 49 | public function normalizeUrl(Url $url) |
| 50 | { |
| 51 | $url->removeQueryString(); |
| 52 | |
| 53 | if (preg_match('~/detail/(?:(?:[^/]+)/(?:[^/]+)/)?([^/]+)$~i', (string) $url, $matches)) { |
| 54 | $url->overwrite('https://gty.im/' . $matches['1']); |
| 55 | } |
| 56 | |
| 57 | $url->removeLastSlash(); |
| 58 | $url->convertToHttps(); |
| 59 | $url->removeWWW(); |
| 60 | |
| 61 | return $url; |
| 62 | } |
| 63 | |
| 64 | /** inline {@inheritdoc} */ |
| 65 | public function modifyResponse(array $response = []) |
| 66 | { |
| 67 | if (!empty($response['html'])) { |
| 68 | return $response; |
| 69 | } |
| 70 | |
| 71 | $check = [ |
| 72 | 'url', 'thumbnail_url', 'thumbnail_width', 'thumbnail_height', 'title' |
| 73 | ]; |
| 74 | |
| 75 | foreach ($check as $c) { |
| 76 | if (!isset($response[$c])) { |
| 77 | return $response; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | $html = []; |
| 82 | $html[] = '<a href="' . $response['url'] . '" target="_blank">'; |
| 83 | $html []= '<img class="gettyimages-oembed" src="' . $response['thumbnail_url'] . '" width="' . $response['thumbnail_width'] . '" height="' . $response['thumbnail_height'] . '" alt="" title="" >'; |
| 84 | $html[] = '</a>'; |
| 85 | |
| 86 | $response['html'] = implode('', $html); |
| 87 | |
| 88 | return $response; |
| 89 | } |
| 90 | |
| 91 | } |
| 92 |