[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Utils/ -> XPath.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2020 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Utils;
   9  
  10  use InvalidArgumentException;
  11  
  12  abstract class XPath
  13  {
  14      /**
  15      * Export a literal as an XPath expression
  16      *
  17      * @param  mixed  $value Literal, e.g. "foo"
  18      * @return string        XPath expression, e.g. "'foo'"
  19      */
  20  	public static function export($value)
  21      {
  22          $callback = get_called_class() . '::export' . ucfirst(gettype($value));
  23          if (!is_callable($callback))
  24          {
  25              throw new InvalidArgumentException(__METHOD__ . '() cannot export non-scalar values');
  26          }
  27  
  28          return $callback($value);
  29      }
  30  
  31      /**
  32      * Export given boolean value
  33      *
  34      * @param  bool   $value
  35      * @return string
  36      */
  37  	protected static function exportBoolean(bool $value): string
  38      {
  39          return ($value) ? 'true()' : 'false()';
  40      }
  41  
  42      /**
  43      * Export given float value
  44      *
  45      * @param  float  $value
  46      * @return string
  47      */
  48  	protected static function exportDouble(float $value): string
  49      {
  50          if (!is_finite($value))
  51          {
  52              throw new InvalidArgumentException(__METHOD__ . '() cannot export irrational numbers');
  53          }
  54  
  55          // Avoid locale issues by using sprintf()
  56          return preg_replace('(\\.?0+$)', '', sprintf('%F', $value));
  57      }
  58  
  59      /**
  60      * Export given integer value
  61      *
  62      * @param  integer $value
  63      * @return string
  64      */
  65  	protected static function exportInteger(int $value): string
  66      {
  67          return (string) $value;
  68      }
  69  
  70      /**
  71      * Export a string as an XPath expression
  72      *
  73      * @param  string $str Literal, e.g. "foo"
  74      * @return string      XPath expression, e.g. "'foo'"
  75      */
  76  	protected static function exportString(string $str): string
  77      {
  78          // foo becomes 'foo'
  79          if (strpos($str, "'") === false)
  80          {
  81              return "'" . $str . "'";
  82          }
  83  
  84          // d'oh becomes "d'oh"
  85          if (strpos($str, '"') === false)
  86          {
  87              return '"' . $str . '"';
  88          }
  89  
  90          // This string contains both ' and ". XPath 1.0 doesn't have a mechanism to escape quotes,
  91          // so we have to get creative and use concat() to join chunks in single quotes and chunks
  92          // in double quotes
  93          $toks = [];
  94          $c    = '"';
  95          $pos  = 0;
  96          while ($pos < strlen($str))
  97          {
  98              $spn = strcspn($str, $c, $pos);
  99              if ($spn)
 100              {
 101                  $toks[] = $c . substr($str, $pos, $spn) . $c;
 102                  $pos   += $spn;
 103              }
 104              $c = ($c === '"') ? "'" : '"';
 105          }
 106  
 107          return 'concat(' . implode(',', $toks) . ')';
 108      }
 109  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1