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

Encoder.php

Zuletzt modifiziert: 02.04.2025, 15:04 - Dateigröße: 5.77 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\JavaScript;
009   
010  use RuntimeException;
011  use s9e\TextFormatter\Configurator\Items\Regexp;
012  use s9e\TextFormatter\Configurator\JavaScript\Code;
013  use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
014  use s9e\TextFormatter\Configurator\JavaScript\Encoder;
015   
016  class Encoder
017  {
018      /**
019      * @var callable[]
020      */
021      public $objectEncoders;
022   
023      /**
024      * @var callable[]
025      */
026      public $typeEncoders;
027   
028      /**
029      * Constructor
030      *
031      * Will set up the default encoders
032      */
033      public function __construct()
034      {
035          $ns = 's9e\\TextFormatter\\Configurator\\';
036          $this->objectEncoders = [
037              $ns . 'Items\\Regexp'           => [$this, 'encodeRegexp'],
038              $ns . 'JavaScript\\Code'        => [$this, 'encodeCode'],
039              $ns . 'JavaScript\\ConfigValue' => [$this, 'encodeConfigValue'],
040              $ns . 'JavaScript\\Dictionary'  => [$this, 'encodeDictionary']
041          ];
042          $this->typeEncoders = [
043              'array'   => [$this, 'encodeArray'],
044              'boolean' => [$this, 'encodeBoolean'],
045              'double'  => [$this, 'encodeScalar'],
046              'integer' => [$this, 'encodeScalar'],
047              'object'  => [$this, 'encodeObject'],
048              'string'  => [$this, 'encodeScalar']
049          ];
050      }
051   
052      /**
053      * Encode a value into JavaScript
054      *
055      * @param  mixed  $value
056      * @return string
057      */
058      public function encode($value)
059      {
060          $type = gettype($value);
061          if (!isset($this->typeEncoders[$type]))
062          {
063              throw new RuntimeException('Cannot encode ' . $type . ' value');
064          }
065   
066          return $this->typeEncoders[$type]($value);
067      }
068   
069      /**
070      * Encode an array to JavaScript
071      *
072      * @param  array  $array
073      * @return string
074      */
075      protected function encodeArray(array $array)
076      {
077          return ($this->isIndexedArray($array)) ? $this->encodeIndexedArray($array) : $this->encodeAssociativeArray($array);
078      }
079   
080      /**
081      * Encode an associative array to JavaScript
082      *
083      * @param  array  $array
084      * @param  bool   $preserveNames
085      * @return string
086      */
087      protected function encodeAssociativeArray(array $array, $preserveNames = false)
088      {
089          ksort($array);
090          $src = '{';
091          $sep = '';
092          foreach ($array as $k => $v)
093          {
094              $src .= $sep . $this->encodePropertyName("$k", $preserveNames) . ':' . $this->encode($v);
095              $sep = ',';
096          }
097          $src .= '}';
098   
099          return $src;
100      }
101   
102      /**
103      * Encode a boolean value into JavaScript
104      *
105      * @param  bool   $value
106      * @return string
107      */
108      protected function encodeBoolean($value)
109      {
110          return ($value) ? '!0' : '!1';
111      }
112   
113      /**
114      * Encode a Code instance into JavaScript
115      *
116      * @param  Code   $code
117      * @return string
118      */
119      protected function encodeCode(Code $code)
120      {
121          return (string) $code;
122      }
123   
124      /**
125      * Encode a ConfigValue instance into JavaScript
126      *
127      * @param  ConfigValue $configValue
128      * @return string
129      */
130      protected function encodeConfigValue(ConfigValue $configValue)
131      {
132          return ($configValue->isDeduplicated()) ? $configValue->getVarName() : $this->encode($configValue->getValue());
133      }
134   
135      /**
136      * Encode a Dictionary object into a JavaScript object
137      *
138      * @param  Dictionary $dict
139      * @return string
140      */
141      protected function encodeDictionary(Dictionary $dict)
142      {
143          return $this->encodeAssociativeArray($dict->getArrayCopy(), true);
144      }
145   
146      /**
147      * Encode an indexed array to JavaScript
148      *
149      * @param  array  $array
150      * @return string
151      */
152      protected function encodeIndexedArray(array $array)
153      {
154          return '[' . implode(',', array_map([$this, 'encode'], $array)) . ']';
155      }
156   
157      /**
158      * Encode an object into JavaScript
159      *
160      * @param  object $object
161      * @return string
162      */
163      protected function encodeObject($object)
164      {
165          foreach ($this->objectEncoders as $className => $callback)
166          {
167              if ($object instanceof $className)
168              {
169                  return $callback($object);
170              }
171          }
172   
173          throw new RuntimeException('Cannot encode instance of ' . get_class($object));
174      }
175   
176      /**
177      * Encode an object property name into JavaScript
178      *
179      * @param  string $name
180      * @param  bool   $preserveNames
181      * @return string
182      */
183      protected function encodePropertyName($name, $preserveNames)
184      {
185          return ($preserveNames || !$this->isLegalProp($name)) ? json_encode($name) : $name;
186      }
187   
188      /**
189      * Encode a Regexp object into JavaScript
190      *
191      * @param  Regexp $regexp
192      * @return string
193      */
194      protected function encodeRegexp(Regexp $regexp)
195      {
196          return $regexp->getJS();
197      }
198   
199      /**
200      * Encode a scalar value into JavaScript
201      *
202      * @param  mixed  $value
203      * @return string
204      */
205      protected function encodeScalar($value)
206      {
207          return json_encode($value);
208      }
209   
210      /**
211      * Test whether given array is a numerically indexed array
212      *
213      * @param  array $array
214      * @return bool
215      */
216      protected function isIndexedArray(array $array)
217      {
218          if (empty($array))
219          {
220              return true;
221          }
222   
223          if (isset($array[0]) && array_keys($array) === range(0, count($array) - 1))
224          {
225              return true;
226          }
227   
228          return false;
229      }
230   
231      /**
232      * Test whether a string can be used as a property name, unquoted
233      *
234      * @link http://es5.github.io/#A.1
235      *
236      * @param  string $name Property's name
237      * @return bool
238      */
239      protected function isLegalProp($name)
240      {
241          /**
242          * @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words
243          * @link http://www.crockford.com/javascript/survey.html
244          */
245          $reserved = ['abstract', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class', 'const', 'continue', 'debugger', 'default', 'delete', 'do', 'double', 'else', 'enum', 'export', 'extends', 'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if', 'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'let', 'long', 'native', 'new', 'null', 'package', 'private', 'protected', 'public', 'return', 'short', 'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'var', 'void', 'volatile', 'while', 'with'];
246   
247          if (in_array($name, $reserved, true))
248          {
249              return false;
250          }
251   
252          return (bool) preg_match('#^(?![0-9])[$_\\pL][$_\\pL\\pNl]+$#Du', $name);
253      }
254  }