[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/JavaScript/ -> Encoder.php (source)

   1  <?php
   2  
   3  /*
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2019 The s9e Authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\JavaScript;
   9  use RuntimeException;
  10  use s9e\TextFormatter\Configurator\Items\Regexp;
  11  use s9e\TextFormatter\Configurator\JavaScript\Code;
  12  use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
  13  use s9e\TextFormatter\Configurator\JavaScript\Encoder;
  14  class Encoder
  15  {
  16      public $objectEncoders;
  17      public $typeEncoders;
  18  	public function __construct()
  19      {
  20          $ns = 's9e\\TextFormatter\\Configurator\\';
  21          $this->objectEncoders = [
  22              $ns . 'Items\\Regexp'           => [$this, 'encodeRegexp'],
  23              $ns . 'JavaScript\\Code'        => [$this, 'encodeCode'],
  24              $ns . 'JavaScript\\ConfigValue' => [$this, 'encodeConfigValue'],
  25              $ns . 'JavaScript\\Dictionary'  => [$this, 'encodeDictionary']
  26          ];
  27          $this->typeEncoders = [
  28              'array'   => [$this, 'encodeArray'],
  29              'boolean' => [$this, 'encodeBoolean'],
  30              'double'  => [$this, 'encodeScalar'],
  31              'integer' => [$this, 'encodeScalar'],
  32              'object'  => [$this, 'encodeObject'],
  33              'string'  => [$this, 'encodeScalar']
  34          ];
  35      }
  36  	public function encode($value)
  37      {
  38          $type = \gettype($value);
  39          if (!isset($this->typeEncoders[$type]))
  40              throw new RuntimeException('Cannot encode ' . $type . ' value');
  41          return $this->typeEncoders[$type]($value);
  42      }
  43  	protected function encodeArray(array $array)
  44      {
  45          return ($this->isIndexedArray($array)) ? $this->encodeIndexedArray($array) : $this->encodeAssociativeArray($array);
  46      }
  47  	protected function encodeAssociativeArray(array $array, $preserveNames = \false)
  48      {
  49          \ksort($array);
  50          $src = '{';
  51          $sep = '';
  52          foreach ($array as $k => $v)
  53          {
  54              $src .= $sep . $this->encodePropertyName("$k", $preserveNames) . ':' . $this->encode($v);
  55              $sep = ',';
  56          }
  57          $src .= '}';
  58          return $src;
  59      }
  60  	protected function encodeBoolean($value)
  61      {
  62          return ($value) ? '!0' : '!1';
  63      }
  64  	protected function encodeCode(Code $code)
  65      {
  66          return (string) $code;
  67      }
  68  	protected function encodeConfigValue(ConfigValue $configValue)
  69      {
  70          return ($configValue->isDeduplicated()) ? $configValue->getVarName() : $this->encode($configValue->getValue());
  71      }
  72  	protected function encodeDictionary(Dictionary $dict)
  73      {
  74          return $this->encodeAssociativeArray($dict->getArrayCopy(), \true);
  75      }
  76  	protected function encodeIndexedArray(array $array)
  77      {
  78          return '[' . \implode(',', \array_map([$this, 'encode'], $array)) . ']';
  79      }
  80  	protected function encodeObject($object)
  81      {
  82          foreach ($this->objectEncoders as $className => $callback)
  83              if ($object instanceof $className)
  84                  return $callback($object);
  85          throw new RuntimeException('Cannot encode instance of ' . \get_class($object));
  86      }
  87  	protected function encodePropertyName($name, $preserveNames)
  88      {
  89          return ($preserveNames || !$this->isLegalProp($name)) ? \json_encode($name) : $name;
  90      }
  91  	protected function encodeRegexp(Regexp $regexp)
  92      {
  93          return $regexp->getJS();
  94      }
  95  	protected function encodeScalar($value)
  96      {
  97          return \json_encode($value);
  98      }
  99  	protected function isIndexedArray(array $array)
 100      {
 101          if (empty($array))
 102              return \true;
 103          if (isset($array[0]) && \array_keys($array) === \range(0, \count($array) - 1))
 104              return \true;
 105          return \false;
 106      }
 107  	protected function isLegalProp($name)
 108      {
 109          $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'];
 110          if (\in_array($name, $reserved, \true))
 111              return \false;
 112          return (bool) \preg_match('#^(?![0-9])[$_\\pL][$_\\pL\\pNl]+$#Du', $name);
 113      }
 114  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1