[ Index ]

PHP Cross Reference of phpBB-3.3.12-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/TemplateNormalizations/ -> AbstractNormalization.php (source)

   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\Configurator\TemplateNormalizations;
   9  
  10  use DOMAttr;
  11  use DOMComment;
  12  use DOMElement;
  13  use DOMNode;
  14  use DOMXPath;
  15  
  16  abstract class AbstractNormalization
  17  {
  18      /**
  19      * XSL namespace
  20      */
  21      const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform';
  22  
  23      /**
  24      * @var DOMDocument Document that holds the template being normalized
  25      */
  26      protected $ownerDocument;
  27  
  28      /**
  29      * @var string[] XPath queries used to retrieve nodes of interest
  30      */
  31      protected $queries = [];
  32  
  33      /**
  34      * @var DOMXPath
  35      */
  36      protected $xpath;
  37  
  38      /**
  39      * Apply this normalization rule to given template
  40      *
  41      * @param  DOMElement $template <xsl:template/> node
  42      * @return void
  43      */
  44  	public function normalize(DOMElement $template)
  45      {
  46          $this->ownerDocument = $template->ownerDocument;
  47          $this->xpath         = new DOMXPath($this->ownerDocument);
  48          $this->xpath->registerNamespace('xsl', self::XMLNS_XSL);
  49          foreach ($this->getNodes() as $node)
  50          {
  51              $this->normalizeNode($node);
  52          }
  53          $this->reset();
  54      }
  55  
  56      /**
  57      * Create an element in current template
  58      *
  59      * @param  string     $nodeName
  60      * @param  string     $textContent
  61      * @return DOMElement
  62      */
  63  	protected function createElement($nodeName, $textContent = '')
  64      {
  65          $methodName = 'createElement';
  66          $args       = [$nodeName];
  67  
  68          // Add the text content for the new element
  69          if ($textContent !== '')
  70          {
  71              $args[] = htmlspecialchars($textContent, ENT_NOQUOTES, 'UTF-8');
  72          }
  73  
  74          // Handle namespaced elements
  75          $prefix = strstr($nodeName, ':', true);
  76          if ($prefix > '')
  77          {
  78              $methodName .= 'NS';
  79              array_unshift($args, $this->ownerDocument->lookupNamespaceURI($prefix));
  80          }
  81  
  82          return call_user_func_array([$this->ownerDocument, $methodName], $args);
  83      }
  84  
  85      /**
  86      * Create an xsl:text element or a text node in current template
  87      *
  88      * @param  string  $content
  89      * @return DOMNode
  90      */
  91  	protected function createText($content)
  92      {
  93          return (trim($content) === '')
  94               ? $this->createElement('xsl:text', $content)
  95               : $this->ownerDocument->createTextNode($content);
  96      }
  97  
  98      /**
  99      * Create a text node in current template
 100      *
 101      * @param  string  $content
 102      * @return DOMText
 103      */
 104  	protected function createTextNode($content)
 105      {
 106          return $this->ownerDocument->createTextNode($content);
 107      }
 108  
 109      /**
 110      * Query and return a list of nodes of interest
 111      *
 112      * @return DOMNode[]
 113      */
 114  	protected function getNodes()
 115      {
 116          $query = implode(' | ', $this->queries);
 117  
 118          return ($query === '') ? [] : $this->xpath($query);
 119      }
 120  
 121      /**
 122      * Test whether given node is an XSL element
 123      *
 124      * @param  DOMNode $node
 125      * @param  string  $localName
 126      * @return bool
 127      */
 128  	protected function isXsl(DOMNode $node, $localName = null)
 129      {
 130          return ($node->namespaceURI === self::XMLNS_XSL && (!isset($localName) || $localName === $node->localName));
 131      }
 132  
 133      /**
 134      * Make an ASCII string lowercase
 135      *
 136      * @param  string $str Original string
 137      * @return string      Lowercased string
 138      */
 139  	protected function lowercase($str)
 140      {
 141          return strtr($str, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
 142      }
 143  
 144      /**
 145      * Normalize given attribute
 146      *
 147      * @param  DOMAttr $attribute
 148      * @return void
 149      */
 150  	protected function normalizeAttribute(DOMAttr $attribute)
 151      {
 152      }
 153  
 154      /**
 155      * Normalize given element
 156      *
 157      * @param  DOMElement $element
 158      * @return void
 159      */
 160  	protected function normalizeElement(DOMElement $element)
 161      {
 162      }
 163  
 164      /**
 165      * Normalize given node
 166      *
 167      * @param  DOMNode $node
 168      * @return void
 169      */
 170  	protected function normalizeNode(DOMNode $node)
 171      {
 172          if (!$node->parentNode)
 173          {
 174              // Ignore nodes that have been removed from the document
 175              return;
 176          }
 177          if ($node instanceof DOMElement)
 178          {
 179              $this->normalizeElement($node);
 180          }
 181          elseif ($node instanceof DOMAttr)
 182          {
 183              $this->normalizeAttribute($node);
 184          }
 185      }
 186  
 187      /**
 188      * Reset this instance's properties after usage
 189      *
 190      * @return void
 191      */
 192  	protected function reset()
 193      {
 194          $this->ownerDocument = null;
 195          $this->xpath         = null;
 196      }
 197  
 198      /**
 199      * Evaluate given XPath expression
 200      *
 201      * For convenience, $XSL is replaced with the XSL namespace URI as a string
 202      *
 203      * @param  string    $query XPath query
 204      * @param  DOMNode   $node  Context node
 205      * @return DOMNode[]
 206      */
 207  	protected function xpath($query, DOMNode $node = null)
 208      {
 209          $query = str_replace('$XSL', '"' . self::XMLNS_XSL . '"', $query);
 210  
 211          return iterator_to_array($this->xpath->query($query, $node));
 212      }
 213  }


Generated: Sun Jun 23 12:25:44 2024 Cross-referenced by PHPXref 0.7.1