[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
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\Collections; 9 use DOMDocument; 10 use DOMElement; 11 use InvalidArgumentException; 12 class XmlFileDefinitionCollection extends SiteDefinitionCollection 13 { 14 protected $configTypes = [ 15 ['(^defaultValue$)', '(^[1-9][0-9]*$)D', 'castToInt'], 16 ['(height$|width$)', '(^[1-9][0-9]*$)D', 'castToInt'], 17 ['(^required$)', '(^(?:true|false)$)iD', 'castToBool'] 18 ]; 19 public function __construct($path) 20 { 21 if (!\file_exists($path) || !\is_dir($path)) 22 throw new InvalidArgumentException('Invalid site directory'); 23 foreach (\glob($path . '/*.xml') as $filepath) 24 { 25 $siteId = \basename($filepath, '.xml'); 26 $this->add($siteId, $this->getConfigFromXmlFile($filepath)); 27 } 28 } 29 protected function castConfigValue($name, $value) 30 { 31 foreach ($this->configTypes as $_d1e08ffa) 32 { 33 list($nameRegexp, $valueRegexp, $methodName) = $_d1e08ffa; 34 if (\preg_match($nameRegexp, $name) && \preg_match($valueRegexp, $value)) 35 return $this->$methodName($value); 36 } 37 return $value; 38 } 39 protected function castToBool($value) 40 { 41 return (\strtolower($value) === 'true'); 42 } 43 protected function castToInt($value) 44 { 45 return (int) $value; 46 } 47 protected function convertValueTypes(array $config) 48 { 49 foreach ($config as $k => $v) 50 if (\is_array($v)) 51 $config[$k] = $this->convertValueTypes($v); 52 else 53 $config[$k] = $this->castConfigValue($k, $v); 54 return $config; 55 } 56 protected function flattenConfig(array $config) 57 { 58 foreach ($config as $k => $v) 59 if (\is_array($v) && \count($v) === 1) 60 $config[$k] = \end($v); 61 return $config; 62 } 63 protected function getConfigFromXmlFile($filepath) 64 { 65 $dom = new DOMDocument; 66 $dom->loadXML(\file_get_contents($filepath), \LIBXML_NOCDATA); 67 return $this->getElementConfig($dom->documentElement); 68 } 69 protected function getElementConfig(DOMElement $element) 70 { 71 $config = []; 72 foreach ($element->attributes as $attribute) 73 $config[$attribute->name][] = $attribute->value; 74 foreach ($element->childNodes as $childNode) 75 if ($childNode instanceof DOMElement) 76 $config[$childNode->nodeName][] = $this->getValueFromElement($childNode); 77 return $this->flattenConfig($this->convertValueTypes($config)); 78 } 79 protected function getValueFromElement(DOMElement $element) 80 { 81 return (!$element->attributes->length && $element->childNodes->length === 1 && $element->firstChild->nodeType === \XML_TEXT_NODE) 82 ? $element->nodeValue 83 : $this->getElementConfig($element); 84 } 85 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |