Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

Regexp.php

Zuletzt modifiziert: 02.04.2025, 15:04 - Dateigröße: 3.47 KiB


001  <?php
002   
003  /**
004  * @package   s9e\TextFormatter
005  * @copyright Copyright (c) 2010-2022 The s9e authors
006  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
007  */
008  namespace s9e\TextFormatter\Configurator\Items;
009   
010  use InvalidArgumentException;
011  use s9e\TextFormatter\Configurator\ConfigProvider;
012  use s9e\TextFormatter\Configurator\FilterableConfigValue;
013  use s9e\TextFormatter\Configurator\Helpers\RegexpParser;
014  use s9e\TextFormatter\Configurator\JavaScript\Code;
015  use s9e\TextFormatter\Configurator\JavaScript\RegexpConvertor;
016   
017  class Regexp implements ConfigProvider, FilterableConfigValue
018  {
019      /**
020      * @var bool Whether this regexp should have the global flag set in JavaScript
021      */
022      protected $isGlobal;
023   
024      /**
025      * @var string JavaScript regexp, with delimiters and modifiers, e.g. "/foo/i"
026      */
027      protected $jsRegexp;
028   
029      /**
030      * @var string PCRE regexp, with delimiters and modifiers, e.g. "/foo/i"
031      */
032      protected $regexp;
033   
034      /**
035      * Constructor
036      *
037      * @param string $regexp   PCRE regexp, with delimiters and modifiers, e.g. "/foo/i"
038      * @param bool   $isGlobal Whether this regexp should have the global flag set in JavaScript
039      */
040      public function __construct($regexp, $isGlobal = false)
041      {
042          if (@preg_match($regexp, '') === false)
043          {
044              throw new InvalidArgumentException('Invalid regular expression ' . var_export($regexp, true));
045          }
046   
047          $this->regexp   = $regexp;
048          $this->isGlobal = $isGlobal;
049      }
050   
051      /**
052      * Return this regexp as a string
053      *
054      * @return string
055      */
056      public function __toString()
057      {
058          return $this->regexp;
059      }
060   
061      /**
062      * {@inheritdoc}
063      */
064      public function asConfig()
065      {
066          return $this;
067      }
068   
069      /**
070      * {@inheritdoc}
071      */
072      public function filterConfig($target)
073      {
074          return ($target === 'JS') ? new Code($this->getJS()) : (string) $this;
075      }
076   
077      /**
078      * Return the name of each capture in this regexp
079      *
080      * @return string[]
081      */
082      public function getCaptureNames()
083      {
084          return RegexpParser::getCaptureNames($this->regexp);
085      }
086   
087      /**
088      * Return this regexp's JavaScript representation
089      *
090      * @return string
091      */
092      public function getJS()
093      {
094          if (!isset($this->jsRegexp))
095          {
096              $this->jsRegexp = RegexpConvertor::toJS($this->regexp, $this->isGlobal);
097          }
098   
099          return $this->jsRegexp;
100      }
101   
102      /**
103      * Return all the named captures with a standalone regexp that matches them
104      *
105      * @return array Array of [capture name => regexp]
106      */
107      public function getNamedCaptures()
108      {
109          $captures   = [];
110          $regexpInfo = RegexpParser::parse($this->regexp);
111   
112          // Prepare the start/end of the regexp and ensure that we use the D modifier
113          $start = $regexpInfo['delimiter'] . '^';
114          $end   = '$' . $regexpInfo['delimiter'] . $regexpInfo['modifiers'];
115          if (strpos($regexpInfo['modifiers'], 'D') === false)
116          {
117              $end .= 'D';
118          }
119   
120          foreach ($this->getNamedCapturesExpressions($regexpInfo['tokens']) as $name => $expr)
121          {
122              $captures[$name] = $start . $expr . $end;
123          }
124   
125          return $captures;
126      }
127   
128      /**
129      * Return the expression used in each named capture
130      *
131      * @param  array[] $tokens
132      * @return array
133      */
134      protected function getNamedCapturesExpressions(array $tokens)
135      {
136          $exprs = [];
137          foreach ($tokens as $token)
138          {
139              if ($token['type'] !== 'capturingSubpatternStart' || !isset($token['name']))
140              {
141                  continue;
142              }
143   
144              $expr = $token['content'];
145              if (strpos($expr, '|') !== false)
146              {
147                  $expr = '(?:' . $expr . ')';
148              }
149   
150              $exprs[$token['name']] = $expr;
151          }
152   
153          return $exprs;
154      }
155   
156      /**
157      * Set this regexp's JavaScript representation
158      *
159      * @param  string $jsRegexp
160      * @return void
161      */
162      public function setJS($jsRegexp)
163      {
164          $this->jsRegexp = $jsRegexp;
165      }
166  }