[ Index ]

PHP Cross Reference of phpBB-3.3.14-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-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\JavaScript;
   9  
  10  use RuntimeException;
  11  use s9e\TextFormatter\Configurator\Items\Regexp;
  12  use s9e\TextFormatter\Configurator\JavaScript\Code;
  13  use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
  14  use s9e\TextFormatter\Configurator\JavaScript\Encoder;
  15  
  16  class Encoder
  17  {
  18      /**
  19      * @var callable[]
  20      */
  21      public $objectEncoders;
  22  
  23      /**
  24      * @var callable[]
  25      */
  26      public $typeEncoders;
  27  
  28      /**
  29      * Constructor
  30      *
  31      * Will set up the default encoders
  32      */
  33  	public function __construct()
  34      {
  35          $ns = 's9e\\TextFormatter\\Configurator\\';
  36          $this->objectEncoders = [
  37              $ns . 'Items\\Regexp'           => [$this, 'encodeRegexp'],
  38              $ns . 'JavaScript\\Code'        => [$this, 'encodeCode'],
  39              $ns . 'JavaScript\\ConfigValue' => [$this, 'encodeConfigValue'],
  40              $ns . 'JavaScript\\Dictionary'  => [$this, 'encodeDictionary']
  41          ];
  42          $this->typeEncoders = [
  43              'array'   => [$this, 'encodeArray'],
  44              'boolean' => [$this, 'encodeBoolean'],
  45              'double'  => [$this, 'encodeScalar'],
  46              'integer' => [$this, 'encodeScalar'],
  47              'object'  => [$this, 'encodeObject'],
  48              'string'  => [$this, 'encodeScalar']
  49          ];
  50      }
  51  
  52      /**
  53      * Encode a value into JavaScript
  54      *
  55      * @param  mixed  $value
  56      * @return string
  57      */
  58  	public function encode($value)
  59      {
  60          $type = gettype($value);
  61          if (!isset($this->typeEncoders[$type]))
  62          {
  63              throw new RuntimeException('Cannot encode ' . $type . ' value');
  64          }
  65  
  66          return $this->typeEncoders[$type]($value);
  67      }
  68  
  69      /**
  70      * Encode an array to JavaScript
  71      *
  72      * @param  array  $array
  73      * @return string
  74      */
  75  	protected function encodeArray(array $array)
  76      {
  77          return ($this->isIndexedArray($array)) ? $this->encodeIndexedArray($array) : $this->encodeAssociativeArray($array);
  78      }
  79  
  80      /**
  81      * Encode an associative array to JavaScript
  82      *
  83      * @param  array  $array
  84      * @param  bool   $preserveNames
  85      * @return string
  86      */
  87  	protected function encodeAssociativeArray(array $array, $preserveNames = false)
  88      {
  89          ksort($array);
  90          $src = '{';
  91          $sep = '';
  92          foreach ($array as $k => $v)
  93          {
  94              $src .= $sep . $this->encodePropertyName("$k", $preserveNames) . ':' . $this->encode($v);
  95              $sep = ',';
  96          }
  97          $src .= '}';
  98  
  99          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  }


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