[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package s9e\TextFormatter 5 * @copyright Copyright (c) 2010-2022 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 10 use s9e\TextFormatter\Configurator\Helpers\AVTHelper; 11 12 abstract class TemplateGenerator 13 { 14 /** 15 * @var array Attributes used to generate current template 16 */ 17 protected $attributes; 18 19 /** 20 * @var array Default attributes 21 */ 22 protected $defaultAttributes = [ 23 'height' => 360, 24 'padding-height' => 0, 25 'style' => [], 26 'width' => 640 27 ]; 28 29 /** 30 * Build the template representing the embedded content 31 * 32 * @return string 33 */ 34 abstract protected function getContentTemplate(); 35 36 /** 37 * Build a template based on a list of attributes 38 * 39 * @param array $attributes 40 * @return string 41 */ 42 public function getTemplate(array $attributes) 43 { 44 $this->attributes = $attributes + $this->defaultAttributes; 45 46 return ($this->needsWrapper()) ? $this->getWrappedTemplate() : $this->getUnwrappedTemplate(); 47 } 48 49 /** 50 * Format an attribute value to be used in an XPath expression 51 * 52 * @param string $expr Original value 53 * @return string Formatted value 54 */ 55 protected function expr($expr) 56 { 57 $expr = trim($expr, '{}'); 58 59 return (preg_match('(^[@$]?[-\\w]+$)D', $expr)) ? $expr : "($expr)"; 60 } 61 62 /** 63 * Generate xsl:attributes elements from an array 64 * 65 * @param array $attributes Array of [name => value] where value can be XSL code 66 * @return string XSL source 67 */ 68 protected function generateAttributes(array $attributes) 69 { 70 if (isset($attributes['style']) && is_array($attributes['style'])) 71 { 72 $attributes['style'] = $this->generateStyle($attributes['style']); 73 } 74 75 ksort($attributes); 76 $xsl = ''; 77 foreach ($attributes as $attrName => $attrValue) 78 { 79 $innerXML = (strpos($attrValue, '<xsl:') !== false) ? $attrValue : AVTHelper::toXSL($attrValue); 80 81 $xsl .= '<xsl:attribute name="' . htmlspecialchars($attrName, ENT_QUOTES, 'UTF-8') . '">' . $innerXML . '</xsl:attribute>'; 82 } 83 84 return $xsl; 85 } 86 87 /** 88 * Generate a CSS declaration based on an array of CSS properties 89 * 90 * @param array $properties Property name => property value 91 * @return string 92 */ 93 protected function generateStyle(array $properties) 94 { 95 ksort($properties); 96 97 $style = ''; 98 foreach ($properties as $name => $value) 99 { 100 $style .= $name . ':' . $value . ';'; 101 } 102 103 return trim($style, ';'); 104 } 105 106 /** 107 * Generate and return the padding declaration used in the responsive wrapper 108 * 109 * @return string 110 */ 111 protected function getResponsivePadding() 112 { 113 $height = $this->expr($this->attributes['height']); 114 $paddingHeight = $this->expr($this->attributes['padding-height']); 115 $width = $this->expr($this->attributes['width']); 116 117 // Create the padding declaration for the fixed ratio 118 $css = 'padding-bottom:<xsl:value-of select="100*(' . $height . '+' . $paddingHeight . ')div' . $width . '"/>%'; 119 120 // Add the padding declaration for the computed ratio if applicable 121 if (!empty($this->attributes['padding-height'])) 122 { 123 // NOTE: there needs to be whitespace around tokens in calc() 124 $css .= ';padding-bottom:calc(<xsl:value-of select="100*' . $height . ' div' . $width . '"/>% + ' . $paddingHeight . 'px)'; 125 } 126 127 // If the width is dynamic, use a conditional to protect against divisions by zero 128 if (strpos($width, '@') !== false) 129 { 130 $css = '<xsl:if test="@width>0">' . $css . '</xsl:if>'; 131 } 132 133 return $css; 134 } 135 136 /** 137 * Generate and return a responsive template for the embedded content 138 * 139 * @return string 140 */ 141 protected function getUnwrappedTemplate() 142 { 143 $this->attributes['style']['width'] = '100%'; 144 $this->attributes['style']['height'] = $this->attributes['height'] . 'px'; 145 $this->attributes['style']['max-width'] = '100%'; 146 147 if (isset($this->attributes['max-width'])) 148 { 149 $this->attributes['style']['max-width'] = $this->attributes['max-width'] . 'px'; 150 } 151 elseif ($this->attributes['width'] !== '100%') 152 { 153 $property = ($this->hasDynamicWidth()) ? 'width' : 'max-width'; 154 $this->attributes['style'][$property] = $this->attributes['width'] . 'px'; 155 } 156 157 if ($this->attributes['style']['width'] === $this->attributes['style']['max-width']) 158 { 159 unset($this->attributes['style']['max-width']); 160 } 161 162 return $this->getContentTemplate(); 163 } 164 165 /** 166 * Generate and return a template for the embedded content, complete with a responsive wrapper 167 * 168 * @return string 169 */ 170 protected function getWrappedTemplate() 171 { 172 $this->attributes['style']['width'] = '100%'; 173 $this->attributes['style']['height'] = '100%'; 174 $this->attributes['style']['position'] = 'absolute'; 175 $this->attributes['style']['left'] = '0'; 176 177 $outerStyle = 'display:inline-block;width:100%;max-width:' . $this->attributes['width'] . 'px'; 178 $innerStyle = 'display:block;overflow:hidden;position:relative;' . $this->getResponsivePadding(); 179 180 $template = '<span>' . $this->generateAttributes(['style' => $outerStyle]); 181 $template .= '<span>' . $this->generateAttributes(['style' => $innerStyle]); 182 $template .= $this->getContentTemplate(); 183 $template .= '</span></span>'; 184 185 return $template; 186 } 187 188 /** 189 * Test whether current template has a dynamic height 190 * 191 * @return bool 192 */ 193 protected function hasDynamicHeight() 194 { 195 return (isset($this->attributes['onload']) && strpos($this->attributes['onload'], '.height') !== false); 196 } 197 198 /** 199 * Test whether current template has a dynamic width 200 * 201 * @return bool 202 */ 203 protected function hasDynamicWidth() 204 { 205 return (isset($this->attributes['onload']) && strpos($this->attributes['onload'], '.width') !== false); 206 } 207 208 /** 209 * Merge two array of attributes 210 * 211 * @param array $defaultAttributes 212 * @param array $newAttributes 213 * @return array 214 */ 215 protected function mergeAttributes(array $defaultAttributes, array $newAttributes) 216 { 217 $attributes = array_merge($defaultAttributes, $newAttributes); 218 if (isset($defaultAttributes['style'], $newAttributes['style'])) 219 { 220 // Re-add the default attributes that were lost (but not replaced) in the merge 221 $attributes['style'] += $defaultAttributes['style']; 222 } 223 224 return $attributes; 225 } 226 227 /** 228 * Test whether current template needs a wrapper to be responsive 229 * 230 * @return bool 231 */ 232 protected function needsWrapper() 233 { 234 return ($this->attributes['width'] !== '100%' && !$this->hasDynamicHeight()); 235 } 236 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |