[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/MediaEmbed/Configurator/ -> TemplateGenerator.php (source)

   1  <?php
   2  
   3  /*
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2019 The s9e Authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Plugins\MediaEmbed\Configurator;
   9  use s9e\TextFormatter\Configurator\Helpers\AVTHelper;
  10  abstract class TemplateGenerator
  11  {
  12      protected $attributes;
  13      protected $defaultAttributes = [
  14          'height'         => 360,
  15          'padding-height' => 0,
  16          'style'          => [],
  17          'width'          => 640
  18      ];
  19      abstract protected function getContentTemplate();
  20  	public function getTemplate(array $attributes)
  21      {
  22          $this->attributes = $attributes + $this->defaultAttributes;
  23          return ($this->needsWrapper()) ? $this->getWrappedTemplate() : $this->getUnwrappedTemplate();
  24      }
  25  	protected function expr($expr)
  26      {
  27          $expr = \trim($expr, '{}');
  28          return (\preg_match('(^[@$]?[-\\w]+$)D', $expr)) ? $expr : "($expr)";
  29      }
  30  	protected function generateAttributes(array $attributes)
  31      {
  32          if (isset($attributes['style']) && \is_array($attributes['style']))
  33              $attributes['style'] = $this->generateStyle($attributes['style']);
  34          \ksort($attributes);
  35          $xsl = '';
  36          foreach ($attributes as $attrName => $attrValue)
  37          {
  38              $innerXML = (\strpos($attrValue, '<xsl:') !== \false) ? $attrValue : AVTHelper::toXSL($attrValue);
  39              $xsl .= '<xsl:attribute name="' . \htmlspecialchars($attrName, \ENT_QUOTES, 'UTF-8') . '">' . $innerXML . '</xsl:attribute>';
  40          }
  41          return $xsl;
  42      }
  43  	protected function generateStyle(array $properties)
  44      {
  45          \ksort($properties);
  46          $style = '';
  47          foreach ($properties as $name => $value)
  48              $style .= $name . ':' . $value . ';';
  49          return \trim($style, ';');
  50      }
  51  	protected function getResponsivePadding()
  52      {
  53          $height        = $this->expr($this->attributes['height']);
  54          $paddingHeight = $this->expr($this->attributes['padding-height']);
  55          $width         = $this->expr($this->attributes['width']);
  56          $css = 'padding-bottom:<xsl:value-of select="100*(' . $height . '+' . $paddingHeight . ')div' . $width . '"/>%';
  57          
  58          if (!empty($this->attributes['padding-height']))
  59              $css .= ';padding-bottom:calc(<xsl:value-of select="100*' . $height . ' div' . $width . '"/>% + ' . $paddingHeight . 'px)';
  60          if (\strpos($width, '@') !== \false)
  61              $css = '<xsl:if test="@width&gt;0">' . $css . '</xsl:if>';
  62          return $css;
  63      }
  64  	protected function getUnwrappedTemplate()
  65      {
  66          $this->attributes['style']['width']     = '100%';
  67          $this->attributes['style']['height']    = $this->attributes['height'] . 'px';
  68          $this->attributes['style']['max-width'] = '100%';
  69          if (isset($this->attributes['max-width']))
  70              $this->attributes['style']['max-width'] = $this->attributes['max-width'] . 'px';
  71          elseif ($this->attributes['width'] !== '100%')
  72          {
  73              $property = ($this->hasDynamicWidth()) ? 'width' : 'max-width';
  74              $this->attributes['style'][$property] = $this->attributes['width'] . 'px';
  75          }
  76          if ($this->attributes['style']['width'] === $this->attributes['style']['max-width'])
  77              unset($this->attributes['style']['max-width']);
  78          return $this->getContentTemplate();
  79      }
  80  	protected function getWrappedTemplate()
  81      {
  82          $this->attributes['style']['width']    = '100%';
  83          $this->attributes['style']['height']   = '100%';
  84          $this->attributes['style']['position'] = 'absolute';
  85          $this->attributes['style']['left']     = '0';
  86          $outerStyle = 'display:inline-block;width:100%;max-width:' . $this->attributes['width'] . 'px';
  87          $innerStyle = 'display:block;overflow:hidden;position:relative;' . $this->getResponsivePadding();
  88          $template  = '<span>' . $this->generateAttributes(['style' => $outerStyle]);
  89          $template .= '<span>' . $this->generateAttributes(['style' => $innerStyle]);
  90          $template .= $this->getContentTemplate();
  91          $template .= '</span></span>';
  92          return $template;
  93      }
  94  	protected function hasDynamicHeight()
  95      {
  96          return (isset($this->attributes['onload']) && \strpos($this->attributes['onload'], '.height') !== \false);
  97      }
  98  	protected function hasDynamicWidth()
  99      {
 100          return (isset($this->attributes['onload']) && \strpos($this->attributes['onload'], '.width') !== \false);
 101      }
 102  	protected function mergeAttributes(array $defaultAttributes, array $newAttributes)
 103      {
 104          $attributes = \array_merge($defaultAttributes, $newAttributes);
 105          if (isset($defaultAttributes['style'], $newAttributes['style']))
 106              $attributes['style'] += $defaultAttributes['style'];
 107          return $attributes;
 108      }
 109  	protected function needsWrapper()
 110      {
 111          return ($this->attributes['width'] !== '100%' && !$this->hasDynamicHeight());
 112      }
 113  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1