CodeSandbox.php
| 1 | <?php |
| 2 | /** |
| 3 | * CodeSandbox.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 | * CodeSandbox Provider |
| 19 | * @link https://codesandbox.io |
| 20 | */ |
| 21 | class CodeSandbox extends ProviderAdapter implements ProviderInterface |
| 22 | { |
| 23 | /** inline {@inheritdoc} */ |
| 24 | protected $endpoint = 'https://codesandbox.io/oembed?format=json'; |
| 25 | |
| 26 | /** inline {@inheritdoc} */ |
| 27 | protected static $hosts = [ |
| 28 | '*.codesandbox.io' |
| 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('~codesandbox\.io/(s|embed)/(?:[^/]+)$~i', (string) $url)); |
| 38 | } |
| 39 | |
| 40 | /** inline {@inheritdoc} */ |
| 41 | public function normalizeUrl(Url $url) |
| 42 | { |
| 43 | $url->convertToHttps(); |
| 44 | $url->removeWWW(); |
| 45 | $url->removeQueryString(); |
| 46 | $url->removeLastSlash(); |
| 47 | |
| 48 | if (preg_match('~/embed/~', (string) $url)) { |
| 49 | $url->overwrite(str_replace('/embed/', '/s/', $url)); |
| 50 | } |
| 51 | |
| 52 | return $url; |
| 53 | } |
| 54 | |
| 55 | /** inline {@inheritdoc} */ |
| 56 | public function getFakeResponse() |
| 57 | { |
| 58 | $embedUrl = str_replace('/s/', '/embed/', $this->url); |
| 59 | |
| 60 | $attr = []; |
| 61 | $attr[] = 'width="{width}"'; |
| 62 | $attr[] = 'height="{height}"'; |
| 63 | $attr[] = 'src="' . $embedUrl . '"'; |
| 64 | $attr[] = 'style="width:{width}px; height:{height}px; border:0; border-radius: 4px; overflow:hidden;"'; |
| 65 | $attr[] = 'sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"'; |
| 66 | |
| 67 | return [ |
| 68 | 'type' => 'rich', |
| 69 | 'provider_name' => 'CodeSandbox', |
| 70 | 'provider_url' => 'http://codesandbox.io/', |
| 71 | 'title' => 'Unknown title', |
| 72 | 'html' => '<iframe ' . implode(' ', $attr). '></iframe>', |
| 73 | ]; |
| 74 | } |
| 75 | |
| 76 | } |
| 77 |