ResponsiveEmbeds.php
101 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ResponsiveEmbeds.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\Html; |
| 14 | |
| 15 | /** |
| 16 | * Class Responsable of converting html into responsive html. |
| 17 | */ |
| 18 | class ResponsiveEmbeds |
| 19 | { |
| 20 | /** |
| 21 | * Attempts to transform the html key of the response |
| 22 | * with a responsive alternative. |
| 23 | * |
| 24 | * @param array $response |
| 25 | * @return array |
| 26 | */ |
| 27 | public function transform(array $response) |
| 28 | { |
| 29 | if (!empty($response['html']) && isset($response['embera_provider_name'], $response['type'])) { |
| 30 | $response['html_pre_responsive'] = $response['html']; |
| 31 | $response['html'] = $this->removeWidthHeightAttributes($response['html']); |
| 32 | $response['html'] = $this->removeWidthHeightStyles($response['html']); |
| 33 | $response['html'] = $this->addClasses($response['html'], strtolower($response['type'])); |
| 34 | $response['html'] = $this->wrapInsideDiv($response['html'], strtolower($response['type']), $response['embera_provider_name']); |
| 35 | } |
| 36 | |
| 37 | return $response; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Removes the Width and Height attributes from a tag |
| 42 | * |
| 43 | * @param string $text |
| 44 | * @return string |
| 45 | * |
| 46 | */ |
| 47 | protected function removeWidthHeightAttributes($text) |
| 48 | { |
| 49 | return preg_replace('~(width|height)="\d*"\s~i', '', $text); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Removes the Width/Height from inline styles |
| 54 | * |
| 55 | * @param string $text |
| 56 | * @return string |
| 57 | */ |
| 58 | protected function removeWidthHeightStyles($text) |
| 59 | { |
| 60 | return preg_replace('~(width|height|max-width|max-height):[0-9 ]+[a-z]{2};?~i', '',$text); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Adds item classes to an html tag |
| 65 | * |
| 66 | * @param string $text |
| 67 | * @param string $type |
| 68 | * @return string |
| 69 | */ |
| 70 | protected function addClasses($text, $type) |
| 71 | { |
| 72 | $class= 'embera-embed-responsive-item embera-embed-responsive-item-' . $type; |
| 73 | if (preg_match('~class="(.+?)"~i', $text)) { |
| 74 | return preg_replace('~class="(.+?)"~i', 'class="$1 ' . $class . '"', $text); |
| 75 | } |
| 76 | |
| 77 | foreach (['iframe', 'img'] as $tag) { |
| 78 | if (preg_match('~<' . $tag . '~i', $text)) { |
| 79 | return str_ireplace('<' . $tag, '<' . $tag . ' class="' . $class . '"', $text); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return $text; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Wraps the whole html inside a new div with custom attributes. |
| 88 | * |
| 89 | * @param string $text |
| 90 | * @param string $type |
| 91 | * @param string $providerName |
| 92 | * @return string |
| 93 | */ |
| 94 | protected function wrapInsideDiv($text, $type, $providerName) |
| 95 | { |
| 96 | $providerName = strtolower($providerName); |
| 97 | return '<div class="embera-embed-responsive embera-embed-responsive-' . $type . ' embera-embed-responsive-provider-' . $providerName . '">' . $text . '</div>'; |
| 98 | } |
| 99 | |
| 100 | } |
| 101 |