[ Index ]

PHP Cross Reference of phpBB-3.3.9-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/Helpers/ -> FilterSyntaxMatcher.php (source)

   1  <?php declare(strict_types=1);
   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\Helpers;
   9  
  10  use s9e\TextFormatter\Configurator\Items\Regexp;
  11  use s9e\TextFormatter\Configurator\RecursiveParser\AbstractRecursiveMatcher;
  12  
  13  class FilterSyntaxMatcher extends AbstractRecursiveMatcher
  14  {
  15      /**
  16      * {@inheritdoc}
  17      */
  18  	public function getMatchers(): array
  19      {
  20          return [
  21              'Array' => [
  22                  'groups' => ['FilterArg', 'Literal'],
  23                  'regexp' => '\\[ ((?&ArrayElements))? \\]',
  24              ],
  25              'ArrayElement' => [
  26                  'regexp' => '(?:((?&Scalar)) => )?((?&Literal))',
  27              ],
  28              'ArrayElements' => [
  29                  'regexp' => '((?&ArrayElement))(?: , ((?&ArrayElements)))?',
  30              ],
  31              'DoubleQuotedString' => [
  32                  'groups' => ['FilterArg', 'Literal', 'Scalar'],
  33                  'regexp' => '"((?:[^\\\\"]|\\\\.)*)"',
  34              ],
  35              'False' => [
  36                  'groups' => ['FilterArg', 'Literal', 'Scalar'],
  37                  'regexp' => '[Ff][Aa][Ll][Ss][Ee]',
  38              ],
  39              'FilterArgs' => [
  40                  'regexp' => '((?&FilterArg))(?: , ((?&FilterArgs)))?',
  41              ],
  42              'FilterCallback' => [
  43                  'regexp' => '([#:\\\\\\w]+)(?: \\( ((?&FilterArgs)?) \\))?',
  44              ],
  45              'Float' => [
  46                  'groups' => ['FilterArg', 'Literal', 'Scalar'],
  47                  'regexp' => '([-+]?(?:\\.[0-9]+(?:_[0-9]+)*|[0-9]+(?:_[0-9]+)*\\.(?!_)[0-9]*(?:_[0-9]+)*|[0-9]+(?:_[0-9]+)*(?=[Ee]))(?:[Ee]-?[0-9]+(?:_[0-9]+)*)?)',
  48              ],
  49              'Integer' => [
  50                  'groups' => ['FilterArg', 'Literal', 'Scalar'],
  51                  'regexp' => '(-?(?:0[Bb][01]+(?:_[01]+)*|0[Oo][0-7]+(?:_[0-7]+)*|0[Xx][0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*|[0-9]+(?:_[0-9]+)*))',
  52              ],
  53              'Null' => [
  54                  'groups' => ['FilterArg', 'Literal', 'Scalar'],
  55                  'regexp' => '[Nn][Uu][Ll][Ll]',
  56              ],
  57              'Param' => [
  58                  'groups' => ['FilterArg'],
  59                  'regexp' => '\\$(\\w+(?:\\.\\w+)*)',
  60              ],
  61              'Regexp' => [
  62                  'groups' => ['FilterArg', 'Literal'],
  63                  'regexp' => '(/(?:[^\\\\/]|\\\\.)*/)([Sgimsu]*)',
  64              ],
  65              'SingleQuotedString' => [
  66                  'groups' => ['FilterArg', 'Literal', 'Scalar'],
  67                  'regexp' => "'((?:[^\\\\']|\\\\.)*)'",
  68              ],
  69              'True' => [
  70                  'groups' => ['FilterArg', 'Literal', 'Scalar'],
  71                  'regexp' => '[Tt][Rr][Uu][Ee]'
  72              ]
  73          ];
  74      }
  75  
  76      /**
  77      * @param  string $elements
  78      * @return array
  79      */
  80  	public function parseArray(string $elements = ''): array
  81      {
  82          $array = [];
  83          if ($elements !== '')
  84          {
  85              foreach ($this->recurse($elements, 'ArrayElements') as $element)
  86              {
  87                  if (array_key_exists('key', $element))
  88                  {
  89                      $array[$element['key']] = $element['value'];
  90                  }
  91                  else
  92                  {
  93                      $array[] = $element['value'];
  94                  }
  95              }
  96          }
  97  
  98          return $array;
  99      }
 100  
 101      /**
 102      * @param  string $key
 103      * @param  string $value
 104      * @return array
 105      */
 106  	public function parseArrayElement(string $key, string $value): array
 107      {
 108          $element = ['value' => $this->recurse($value, 'Literal')];
 109          if ($key !== '')
 110          {
 111              $element['key'] = $this->recurse($key, 'Scalar');
 112          }
 113  
 114          return $element;
 115      }
 116  
 117      /**
 118      * @param  string $firstElement
 119      * @param  string $otherElements
 120      * @return array
 121      */
 122  	public function parseArrayElements(string $firstElement, string $otherElements = null)
 123      {
 124          $elements = [$this->recurse($firstElement, 'ArrayElement')];
 125          if (isset($otherElements))
 126          {
 127              $elements = array_merge($elements, $this->recurse($otherElements, 'ArrayElements'));
 128          }
 129  
 130          return $elements;
 131      }
 132  
 133      /**
 134      * @param  string $str
 135      * @return string
 136      */
 137  	public function parseDoubleQuotedString(string $str): string
 138      {
 139          /**
 140          * @link https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double
 141          */
 142          return preg_replace_callback(
 143              '(\\\\([nrtvef\\\\$"]|[0-7]{1,3}|x[0-9A-Fa-f]{1,2}|u\\{[0-9A-Fa-f]+\\}))',
 144              function ($m)
 145              {
 146                  if ($m[1] === 'e')
 147                  {
 148                      return "\e";
 149                  }
 150                  if ($m[1][0] === 'u')
 151                  {
 152                      return html_entity_decode('&#x' . substr($m[1], 2, -1) . ';', ENT_QUOTES, 'utf-8');
 153                  }
 154  
 155                  return stripcslashes($m[0]);
 156              },
 157              $str
 158          );
 159      }
 160  
 161      /**
 162      * @return bool
 163      */
 164  	public function parseFalse(): bool
 165      {
 166          return false;
 167      }
 168  
 169      /**
 170      * @param  string $callback
 171      * @param  string $args
 172      * @return array
 173      */
 174  	public function parseFilterCallback(string $callback, string $args = null): array
 175      {
 176          $config = ['filter' => $callback];
 177          if (isset($args))
 178          {
 179              $config['params'] = ($args === '') ? [] : $this->recurse($args, 'FilterArgs');
 180          }
 181  
 182          return $config;
 183      }
 184  
 185      /**
 186      * @param  string $firstArg
 187      * @param  string $otherArgs
 188      * @return array
 189      */
 190  	public function parseFilterArgs(string $firstArg, string $otherArgs = null)
 191      {
 192          $parsedArg = $this->parser->parse($firstArg, 'FilterArg');
 193  
 194          $type = ($parsedArg['match'] === 'Param') ? 'Name' : 'Value';
 195          $args = [[$type, $parsedArg['value']]];
 196          if (isset($otherArgs))
 197          {
 198              $args = array_merge($args, $this->recurse($otherArgs, 'FilterArgs'));
 199          }
 200  
 201          return $args;
 202      }
 203  
 204      /**
 205      * @return null
 206      */
 207  	public function parseNull()
 208      {
 209          return null;
 210      }
 211  
 212      /**
 213      * @param  string $str
 214      * @return float
 215      */
 216  	public function parseFloat(string $str): float
 217      {
 218          return (float) str_replace('_', '', $str);
 219      }
 220  
 221      /**
 222      * @param  string $str
 223      * @return integer
 224      */
 225  	public function parseInteger(string $str): int
 226      {
 227          // Handle PHP 8.1's explicit octal notation
 228          if (strtolower(substr($str, 0, 2)) === '0o')
 229          {
 230              $str = '0' . substr($str, 2);
 231          }
 232  
 233          return intval(str_replace('_', '', $str), 0);
 234      }
 235  
 236      /**
 237      * @param  string $str
 238      * @return string
 239      */
 240  	public function parseParam(string $str): string
 241      {
 242          return $str;
 243      }
 244  
 245      /**
 246      * @param  string $regexp
 247      * @param  string $flags
 248      * @return Regexp
 249      */
 250  	public function parseRegexp(string $regexp, string $flags): Regexp
 251      {
 252          $regexp .= str_replace('g', '', $flags);
 253  
 254          return new Regexp($regexp, true);
 255      }
 256  
 257      /**
 258      * @param  string $str
 259      * @return string
 260      */
 261  	public function parseSingleQuotedString(string $str): string
 262      {
 263          return preg_replace("(\\\\([\\\\']))", '$1', $str);
 264      }
 265  
 266      /**
 267      * @return bool
 268      */
 269  	public function parseTrue(): bool
 270      {
 271          return true;
 272      }
 273  }


Generated: Wed Dec 7 15:09:22 2022 Cross-referenced by PHPXref 0.7.1