[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_test/vendor/symfony/css-selector/XPath/ -> XPathExpr.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\CssSelector\XPath;
  13  
  14  /**
  15   * XPath expression translator interface.
  16   *
  17   * This component is a port of the Python cssselect library,
  18   * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  19   *
  20   * @author Jean-Fran├žois Simon <jeanfrancois.simon@sensiolabs.com>
  21   *
  22   * @internal
  23   */
  24  class XPathExpr
  25  {
  26      private $path;
  27      private $element;
  28      private $condition;
  29  
  30      public function __construct(string $path = '', string $element = '*', string $condition = '', bool $starPrefix = false)
  31      {
  32          $this->path = $path;
  33          $this->element = $element;
  34          $this->condition = $condition;
  35  
  36          if ($starPrefix) {
  37              $this->addStarPrefix();
  38          }
  39      }
  40  
  41      public function getElement(): string
  42      {
  43          return $this->element;
  44      }
  45  
  46      public function addCondition(string $condition): self
  47      {
  48          $this->condition = $this->condition ? sprintf('(%s) and (%s)', $this->condition, $condition) : $condition;
  49  
  50          return $this;
  51      }
  52  
  53      public function getCondition(): string
  54      {
  55          return $this->condition;
  56      }
  57  
  58      public function addNameTest(): self
  59      {
  60          if ('*' !== $this->element) {
  61              $this->addCondition('name() = '.Translator::getXpathLiteral($this->element));
  62              $this->element = '*';
  63          }
  64  
  65          return $this;
  66      }
  67  
  68      public function addStarPrefix(): self
  69      {
  70          $this->path .= '*/';
  71  
  72          return $this;
  73      }
  74  
  75      /**
  76       * Joins another XPathExpr with a combiner.
  77       *
  78       * @return $this
  79       */
  80      public function join(string $combiner, self $expr): self
  81      {
  82          $path = $this->__toString().$combiner;
  83  
  84          if ('*/' !== $expr->path) {
  85              $path .= $expr->path;
  86          }
  87  
  88          $this->path = $path;
  89          $this->element = $expr->element;
  90          $this->condition = $expr->condition;
  91  
  92          return $this;
  93      }
  94  
  95      public function __toString(): string
  96      {
  97          $path = $this->path.$this->element;
  98          $condition = null === $this->condition || '' === $this->condition ? '' : '['.$this->condition.']';
  99  
 100          return $path.$condition;
 101      }
 102  }