SongLink.php
| 1 | <?php |
| 2 | /** |
| 3 | * SongLink.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 | * SongLink Provider |
| 19 | * @link https://song.link |
| 20 | */ |
| 21 | class SongLink extends ProviderAdapter implements ProviderInterface |
| 22 | { |
| 23 | /** inline {@inheritdoc} */ |
| 24 | protected $endpoint = 'https://song.link/oembed?format=json'; |
| 25 | |
| 26 | /** inline {@inheritdoc} */ |
| 27 | protected static $hosts = [ |
| 28 | 'song.link', |
| 29 | 'album.link', |
| 30 | 'artist.link', |
| 31 | 'playlist.link', |
| 32 | 'pods.link', |
| 33 | 'mylink.page', |
| 34 | 'odesli.co', |
| 35 | ]; |
| 36 | |
| 37 | /** inline {@inheritdoc} */ |
| 38 | protected $httpsSupport = true; |
| 39 | |
| 40 | /** inline {@inheritdoc} */ |
| 41 | public function validateUrl(Url $url) |
| 42 | { |
| 43 | return (bool) (preg_match('~((song|album|artist|playlist|pods)\.link|mylink\.page|odesli\.co)/([^/]+)~i', (string) $url)); |
| 44 | } |
| 45 | |
| 46 | /** inline {@inheritdoc} */ |
| 47 | public function normalizeUrl(Url $url) |
| 48 | { |
| 49 | $url->convertToHttps(); |
| 50 | $url->removeQueryString(); |
| 51 | $url->removeLastSlash(); |
| 52 | |
| 53 | return $url; |
| 54 | } |
| 55 | |
| 56 | /** inline {@inheritdoc} */ |
| 57 | public function getFakeResponse() |
| 58 | { |
| 59 | $embedUrl = 'https://embed.song.link/?url=' . urlencode((string) $this->url); |
| 60 | |
| 61 | $attr = []; |
| 62 | $attr[] = 'width="{width}"'; |
| 63 | $attr[] = 'height="{height}"'; |
| 64 | $attr[] = 'src="' . $embedUrl . '"'; |
| 65 | $attr[] = 'frameborder="0"'; |
| 66 | $attr[] = 'allowtransparency allowfullscreen'; |
| 67 | $attr[] = 'sandbox="allow-same-origin allow-scripts allow-presentation allow-popups allow-popups-to-escape-sandbox"'; |
| 68 | |
| 69 | return [ |
| 70 | 'type' => 'rich', |
| 71 | 'provider_name' => 'SongLink', |
| 72 | 'provider_url' => 'https://song.link', |
| 73 | 'title' => 'Unknown title', |
| 74 | 'html' => '<iframe ' . implode(' ', $attr). '></iframe>', |
| 75 | ]; |
| 76 | } |
| 77 | |
| 78 | } |
| 79 |