GeographUk.php
| 1 | <?php |
| 2 | /** |
| 3 | * GeographUk.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 | * GeographUk Provider |
| 19 | * @link https://www.geograph.org.uk |
| 20 | */ |
| 21 | class GeographUk extends ProviderAdapter implements ProviderInterface |
| 22 | { |
| 23 | /** inline {@inheritdoc} */ |
| 24 | protected $endpoint = 'https://api.geograph.org.uk/api/oembed?format=json'; |
| 25 | |
| 26 | /** inline {@inheritdoc} */ |
| 27 | protected static $hosts = [ |
| 28 | '*.geograph.org.uk', '*.geograph.co.uk', '*.geograph.ie' |
| 29 | ]; |
| 30 | |
| 31 | /** inline {@inheritdoc} */ |
| 32 | protected $httpsSupport = true; |
| 33 | |
| 34 | /** inline {@inheritdoc} */ |
| 35 | public function validateUrl(Url $url) |
| 36 | { |
| 37 | return (bool) (preg_match('~/(photo|geophotos)/([^/]+)~i', (string) $url)); |
| 38 | } |
| 39 | |
| 40 | /** inline {@inheritdoc} */ |
| 41 | public function normalizeUrl(Url $url) |
| 42 | { |
| 43 | $url->convertToHttps(); |
| 44 | $url->removeQueryString(); |
| 45 | $url->removeLastSlash(); |
| 46 | |
| 47 | return $url; |
| 48 | } |
| 49 | |
| 50 | /** inline {@inheritdoc} */ |
| 51 | public function modifyResponse(array $response = []) |
| 52 | { |
| 53 | if (!empty($response['html'])) { |
| 54 | return $response; |
| 55 | } |
| 56 | |
| 57 | $check = [ |
| 58 | 'url', 'thumbnail_url', 'thumbnail_width', 'thumbnail_height', 'title' |
| 59 | ]; |
| 60 | |
| 61 | foreach ($check as $c) { |
| 62 | if (!isset($response[$c])) { |
| 63 | return $response; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $html = []; |
| 68 | $html[] = '<a href="' . $response['url'] . '" target="_blank">'; |
| 69 | $html[] = '<img class="geograph" src="' . $response['thumbnail_url'] . '" width="' . $response['thumbnail_width'] . '" height="' . $response['thumbnail_height'] . '" alt="" title="" >'; |
| 70 | $html[] = '</a>'; |
| 71 | |
| 72 | $response['html'] = implode('', $html); |
| 73 | |
| 74 | return $response; |
| 75 | } |
| 76 | |
| 77 | } |
| 78 |