[ 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\Collections; 9 10 use DOMDocument; 11 use DOMElement; 12 use InvalidArgumentException; 13 14 class XmlFileDefinitionCollection extends SiteDefinitionCollection 15 { 16 /** 17 * @var array Known config types [<name regexp>, <value regexp>, <type>] 18 */ 19 protected $configTypes = [ 20 ['(^defaultValue$)', '(^(?:0|[1-9][0-9]+)$)D', 'castToInt'], 21 ['(height$|width$)', '(^(?:0|[1-9][0-9]+)$)D', 'castToInt'], 22 ['(^required$)', '(^(?:fals|tru)e$)Di', 'castToBool'] 23 ]; 24 25 /** 26 * Constructor 27 * 28 * @param string $path Path to site definitions' dir 29 */ 30 public function __construct($path) 31 { 32 if (!file_exists($path) || !is_dir($path)) 33 { 34 throw new InvalidArgumentException('Invalid site directory'); 35 } 36 foreach (glob($path . '/*.xml') as $filepath) 37 { 38 $siteId = basename($filepath, '.xml'); 39 $this->add($siteId, $this->getConfigFromXmlFile($filepath)); 40 } 41 } 42 43 /** 44 * Cast given config value to the appropriate type 45 * 46 * @param string $name Name of the config value 47 * @param string $value Config value in string form 48 * @return mixed Config value in appropriate type 49 */ 50 protected function castConfigValue($name, $value) 51 { 52 foreach ($this->configTypes as list($nameRegexp, $valueRegexp, $methodName)) 53 { 54 if (preg_match($nameRegexp, $name) && preg_match($valueRegexp, $value)) 55 { 56 return $this->$methodName($value); 57 } 58 } 59 60 return $value; 61 } 62 63 /** 64 * Cast given config value to a boolean 65 * 66 * @param string $value 67 * @return bool 68 */ 69 protected function castToBool($value) 70 { 71 return (strtolower($value) === 'true'); 72 } 73 74 /** 75 * Cast given config value to an integer 76 * 77 * @param string $value 78 * @return integer 79 */ 80 protected function castToInt($value) 81 { 82 return (int) $value; 83 } 84 85 /** 86 * Convert known config values to the appropriate type 87 * 88 * Will cast properties whose name is "defaultValue" or ends in "height" or "width" to integers 89 * 90 * @param array $config Original config 91 * @return array Converted config 92 */ 93 protected function convertValueTypes(array $config) 94 { 95 foreach ($config as $k => $v) 96 { 97 if (is_array($v)) 98 { 99 $config[$k] = $this->convertValueTypes($v); 100 } 101 elseif (is_string($v)) 102 { 103 $config[$k] = $this->castConfigValue($k, $v); 104 } 105 } 106 107 return $config; 108 } 109 110 /** 111 * Replace arrays that contain a single element with the element itself 112 * 113 * @param array $config 114 * @return array 115 */ 116 protected function flattenConfig(array $config) 117 { 118 foreach ($config as $k => $v) 119 { 120 if (is_array($v) && count($v) === 1) 121 { 122 $config[$k] = end($v); 123 } 124 } 125 126 return $config; 127 } 128 129 /** 130 * Extract a site's config from its XML file 131 * 132 * @param string $filepath Path to the XML file 133 * @return mixed 134 */ 135 protected function getConfigFromXmlFile($filepath) 136 { 137 $dom = new DOMDocument; 138 $dom->loadXML(file_get_contents($filepath), LIBXML_NOCDATA); 139 140 return $this->getElementConfig($dom->documentElement); 141 } 142 143 /** 144 * Extract a site's config from its XML representation 145 * 146 * @param DOMElement $element Current node 147 * @return mixed 148 */ 149 protected function getElementConfig(DOMElement $element) 150 { 151 $config = []; 152 foreach ($element->attributes as $attribute) 153 { 154 $config[$attribute->name][] = $attribute->value; 155 } 156 foreach ($element->childNodes as $childNode) 157 { 158 if ($childNode instanceof DOMElement) 159 { 160 $config[$childNode->nodeName][] = $this->getValueFromElement($childNode); 161 } 162 } 163 164 return $this->flattenConfig($this->convertValueTypes($config)); 165 } 166 167 /** 168 * Extract a value from given element 169 * 170 * @param DOMElement $element 171 * @return mixed 172 */ 173 protected function getValueFromElement(DOMElement $element) 174 { 175 return (!$element->attributes->length && $element->childNodes->length === 1 && $element->firstChild->nodeType === XML_TEXT_NODE) 176 ? $element->nodeValue 177 : $this->getElementConfig($element); 178 } 179 }
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 |