[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/RendererGenerators/PHP/XPathConvertor/Convertors/ -> SingleByteStringFunctions.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\RendererGenerators\PHP\XPathConvertor\Convertors;
   9  
  10  class SingleByteStringFunctions extends AbstractConvertor
  11  {
  12      /**
  13      * {@inheritdoc}
  14      */
  15  	public function getMatchers(): array
  16      {
  17          $groups = 'Boolean:BooleanFunction:';
  18  
  19          return [
  20              $groups . 'Contains'      => 'contains \\( ((?&String)) , ((?&String)) \\)',
  21              $groups . 'EndsWith'      => 'ends-with \\( ((?&String)) , ((?&String)) \\)',
  22              $groups . 'NotContains'   => 'not \\( contains \\( ((?&String)) , ((?&String)) \\) \\)',
  23              $groups . 'NotEndsWith'   => 'not \\( ends-with \\( ((?&String)) , ((?&String)) \\) \\)',
  24              $groups . 'NotStartsWith' => 'not \\( starts-with \\( ((?&String)) , ((?&String)) \\) \\)',
  25              $groups . 'StartsWith'    => 'starts-with \\( ((?&String)) , ((?&String)) \\)',
  26              'Number:StringLength'     => 'string-length \\( ((?&String))? \\)'
  27          ];
  28      }
  29  
  30      /**
  31      * Convert a call to contains()
  32      *
  33      * @param  string $haystack Expression for the haystack part of the call
  34      * @param  string $needle   Expression for the needle part of the call
  35      * @return string
  36      */
  37  	public function parseContains($haystack, $needle)
  38      {
  39          return $this->generateContains($haystack, $needle, true);
  40      }
  41  
  42      /**
  43      * Convert a call to ends-with()
  44      *
  45      * @param  string $string    Expression for the string part of the call
  46      * @param  string $substring Expression for the substring part of the call
  47      * @return string
  48      */
  49  	public function parseEndsWith($string, $substring)
  50      {
  51          return $this->generateEndsWith($string, $substring, true);
  52      }
  53  
  54      /**
  55      * Convert a call to not(contains())
  56      *
  57      * @param  string $haystack Expression for the haystack part of the call
  58      * @param  string $needle   Expression for the needle part of the call
  59      * @return string
  60      */
  61  	public function parseNotContains($haystack, $needle)
  62      {
  63          return $this->generateContains($haystack, $needle, false);
  64      }
  65  
  66      /**
  67      * Convert a call to not(ends-with())
  68      *
  69      * @param  string $string    Expression for the string part of the call
  70      * @param  string $substring Expression for the substring part of the call
  71      * @return string
  72      */
  73  	public function parseNotEndsWith($string, $substring)
  74      {
  75          return $this->generateEndsWith($string, $substring, false);
  76      }
  77  
  78      /**
  79      * Convert a call to not(starts-with())
  80      *
  81      * @param  string $string    Expression for the string part of the call
  82      * @param  string $substring Expression for the substring part of the call
  83      * @return string
  84      */
  85  	public function parseNotStartsWith($string, $substring)
  86      {
  87          return $this->generateStartsWith($string, $substring, false);
  88      }
  89  
  90      /**
  91      * Convert a call to starts-with()
  92      *
  93      * @param  string $string    Expression for the string part of the call
  94      * @param  string $substring Expression for the substring part of the call
  95      * @return string
  96      */
  97  	public function parseStartsWith($string, $substring)
  98      {
  99          return $this->generateStartsWith($string, $substring, true);
 100      }
 101  
 102      /**
 103      * Convert a call to string-length()
 104      *
 105      * @param  string $expr
 106      * @return string
 107      */
 108  	public function parseStringLength($expr = '.')
 109      {
 110          return "preg_match_all('(.)su'," . $this->recurse($expr) . ')';
 111      }
 112  
 113      /**
 114      * Generate the code for a call to contains()
 115      *
 116      * @param  string $haystack Expression for the haystack part of the call
 117      * @param  string $needle   Expression for the needle part of the call
 118      * @param  bool   $bool     Return value for a positive match
 119      * @return string
 120      */
 121  	protected function generateContains($haystack, $needle, $bool)
 122      {
 123          $operator = ($bool) ? '!==' : '===';
 124  
 125          return '(strpos(' . $this->recurse($haystack) . ',' . $this->recurse($needle) . ')' . $operator . 'false)';
 126      }
 127  
 128      /**
 129      * Generate the code for a call to ends-with()
 130      *
 131      * @param  string $string    Expression for the string part of the call
 132      * @param  string $substring Expression for the substring part of the call
 133      * @param  bool   $bool      Return value for a positive match
 134      * @return string
 135      */
 136  	protected function generateEndsWith($string, $substring, $bool)
 137      {
 138          return (preg_match('(^(?:\'[^\']+\'|"[^"]+")$)D', $substring))
 139               ? $this->generateEndsWithLiteral($string, $substring, $bool)
 140               : $this->generateEndsWithExpression($string, $substring, $bool);
 141      }
 142  
 143      /**
 144      * Generate the code for a call to ends-with() where the second argument is a literal string
 145      *
 146      * @param  string $string    Expression for the string part of the call
 147      * @param  string $substring Expression for a literal substring
 148      * @param  bool   $bool      Return value for a positive match
 149      * @return string
 150      */
 151  	protected function generateEndsWithLiteral($string, $substring, $bool)
 152      {
 153          $operator = ($bool) ? '===' : '!==';
 154  
 155          return '(substr(' . $this->recurse($string) . ',-' . (strlen($substring) - 2) . ')' . $operator . $this->recurse($substring) . ')';
 156      }
 157  
 158      /**
 159      * Generate the code for a call to ends-with()
 160      *
 161      * @param  string $string    Expression for the string part of the call
 162      * @param  string $substring Expression for the substring part of the call
 163      * @param  bool   $bool      Return value for a positive match
 164      * @return string
 165      */
 166  	protected function generateEndsWithExpression($string, $substring, $bool)
 167      {
 168          $operator = ($bool) ? '' : '!';
 169  
 170          return $operator . "preg_match('('.preg_quote(" . $this->recurse($substring) . ").'$)D'," . $this->recurse($string) . ')';
 171      }
 172  
 173      /**
 174      * Generate the code for a call to starts-with()
 175      *
 176      * @param  string $string    Expression for the string part of the call
 177      * @param  string $substring Expression for the substring part of the call
 178      * @param  bool   $bool      Return value for a positive match
 179      * @return string
 180      */
 181  	protected function generateStartsWith($string, $substring, $bool)
 182      {
 183          $operator = ($bool) ? '===' : '!==';
 184  
 185          return '(strpos(' . $this->recurse($string) . ',' . $this->recurse($substring) . ')' . $operator . '0)';
 186      }
 187  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1