[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/zendframework/zend-code/src/Generator/ -> ValueGenerator.php (source)

   1  <?php
   2  /**
   3   * Zend Framework (http://framework.zend.com/)
   4   *
   5   * @link      http://github.com/zendframework/zf2 for the canonical source repository
   6   * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   7   * @license   http://framework.zend.com/license/new-bsd New BSD License
   8   */
   9  
  10  namespace Zend\Code\Generator;
  11  
  12  use Zend\Stdlib\ArrayObject;
  13  
  14  class ValueGenerator extends AbstractGenerator
  15  {
  16      /**#@+
  17       * Constant values
  18       */
  19      const TYPE_AUTO     = 'auto';
  20      const TYPE_BOOLEAN  = 'boolean';
  21      const TYPE_BOOL     = 'bool';
  22      const TYPE_NUMBER   = 'number';
  23      const TYPE_INTEGER  = 'integer';
  24      const TYPE_INT      = 'int';
  25      const TYPE_FLOAT    = 'float';
  26      const TYPE_DOUBLE   = 'double';
  27      const TYPE_STRING   = 'string';
  28      const TYPE_ARRAY    = 'array';
  29      const TYPE_CONSTANT = 'constant';
  30      const TYPE_NULL     = 'null';
  31      const TYPE_OBJECT   = 'object';
  32      const TYPE_OTHER    = 'other';
  33      /**#@-*/
  34  
  35      const OUTPUT_MULTIPLE_LINE = 'multipleLine';
  36      const OUTPUT_SINGLE_LINE   = 'singleLine';
  37  
  38      /**
  39       * @var mixed
  40       */
  41      protected $value = null;
  42  
  43      /**
  44       * @var string
  45       */
  46      protected $type = self::TYPE_AUTO;
  47  
  48      /**
  49       * @var int
  50       */
  51      protected $arrayDepth = 0;
  52  
  53      /**
  54       * @var string
  55       */
  56      protected $outputMode = self::OUTPUT_MULTIPLE_LINE;
  57  
  58      /**
  59       * @var array
  60       */
  61      protected $allowedTypes = null;
  62      /**
  63       * Autodetectable constants
  64       * @var ArrayObject
  65       */
  66      protected $constants = null;
  67  
  68      /**
  69       * @param mixed       $value
  70       * @param string      $type
  71       * @param string      $outputMode
  72       * @param ArrayObject $constants
  73       */
  74      public function __construct($value = null, $type = self::TYPE_AUTO, $outputMode = self::OUTPUT_MULTIPLE_LINE, ArrayObject $constants = null)
  75      {
  76          // strict check is important here if $type = AUTO
  77          if ($value !== null) {
  78              $this->setValue($value);
  79          }
  80          if ($type !== self::TYPE_AUTO) {
  81              $this->setType($type);
  82          }
  83          if ($outputMode !== self::OUTPUT_MULTIPLE_LINE) {
  84              $this->setOutputMode($outputMode);
  85          }
  86          if ($constants !== null) {
  87              $this->constants = $constants;
  88          } else {
  89              $this->constants = new ArrayObject();
  90          }
  91      }
  92  
  93      /**
  94       * Init constant list by defined and magic constants
  95       */
  96      public function initEnvironmentConstants()
  97      {
  98          $constants   = array(
  99              '__DIR__',
 100              '__FILE__',
 101              '__LINE__',
 102              '__CLASS__',
 103              '__TRAIT__',
 104              '__METHOD__',
 105              '__FUNCTION__',
 106              '__NAMESPACE__',
 107              '::'
 108          );
 109          $constants = array_merge($constants, array_keys(get_defined_constants()), $this->constants->getArrayCopy());
 110          $this->constants->exchangeArray($constants);
 111      }
 112  
 113      /**
 114       * Add constant to list
 115       *
 116       * @param string $constant
 117       *
 118       * @return $this
 119       */
 120      public function addConstant($constant)
 121      {
 122          $this->constants->append($constant);
 123  
 124          return $this;
 125      }
 126  
 127      /**
 128       * Delete constant from constant list
 129       *
 130       * @param string $constant
 131       *
 132       * @return bool
 133       */
 134      public function deleteConstant($constant)
 135      {
 136          if (($index = array_search($constant, $this->constants->getArrayCopy())) !== false) {
 137              $this->constants->offsetUnset($index);
 138          }
 139  
 140          return $index !== false;
 141      }
 142  
 143      /**
 144       * Return constant list
 145       *
 146       * @return ArrayObject
 147       */
 148      public function getConstants()
 149      {
 150          return $this->constants;
 151      }
 152  
 153      /**
 154       * @return bool
 155       */
 156      public function isValidConstantType()
 157      {
 158          if ($this->type == self::TYPE_AUTO) {
 159              $type = $this->getAutoDeterminedType($this->value);
 160          } else {
 161              $type = $this->type;
 162          }
 163  
 164          // valid types for constants
 165          $scalarTypes = array(
 166              self::TYPE_BOOLEAN,
 167              self::TYPE_BOOL,
 168              self::TYPE_NUMBER,
 169              self::TYPE_INTEGER,
 170              self::TYPE_INT,
 171              self::TYPE_FLOAT,
 172              self::TYPE_DOUBLE,
 173              self::TYPE_STRING,
 174              self::TYPE_CONSTANT,
 175              self::TYPE_NULL
 176          );
 177  
 178          return in_array($type, $scalarTypes);
 179      }
 180  
 181      /**
 182       * @param  mixed $value
 183       * @return ValueGenerator
 184       */
 185      public function setValue($value)
 186      {
 187          $this->value = $value;
 188          return $this;
 189      }
 190  
 191      /**
 192       * @return mixed
 193       */
 194      public function getValue()
 195      {
 196          return $this->value;
 197      }
 198  
 199      /**
 200       * @param  string $type
 201       * @return ValueGenerator
 202       */
 203      public function setType($type)
 204      {
 205          $this->type = (string) $type;
 206          return $this;
 207      }
 208  
 209      /**
 210       * @return string
 211       */
 212      public function getType()
 213      {
 214          return $this->type;
 215      }
 216  
 217      /**
 218       * @param  int $arrayDepth
 219       * @return ValueGenerator
 220       */
 221      public function setArrayDepth($arrayDepth)
 222      {
 223          $this->arrayDepth = (int) $arrayDepth;
 224          return $this;
 225      }
 226  
 227      /**
 228       * @return int
 229       */
 230      public function getArrayDepth()
 231      {
 232          return $this->arrayDepth;
 233      }
 234  
 235      /**
 236       * @param  string $type
 237       * @return string
 238       */
 239      protected function getValidatedType($type)
 240      {
 241          $types = array(
 242              self::TYPE_AUTO,
 243              self::TYPE_BOOLEAN,
 244              self::TYPE_BOOL,
 245              self::TYPE_NUMBER,
 246              self::TYPE_INTEGER,
 247              self::TYPE_INT,
 248              self::TYPE_FLOAT,
 249              self::TYPE_DOUBLE,
 250              self::TYPE_STRING,
 251              self::TYPE_ARRAY,
 252              self::TYPE_CONSTANT,
 253              self::TYPE_NULL,
 254              self::TYPE_OBJECT,
 255              self::TYPE_OTHER
 256          );
 257  
 258          if (in_array($type, $types)) {
 259              return $type;
 260          }
 261  
 262          return self::TYPE_AUTO;
 263      }
 264  
 265      /**
 266       * @param  mixed $value
 267       * @return string
 268       */
 269      public function getAutoDeterminedType($value)
 270      {
 271          switch (gettype($value)) {
 272              case 'boolean':
 273                  return self::TYPE_BOOLEAN;
 274              case 'string':
 275                  foreach ($this->constants as $constant) {
 276                      if (strpos($value, $constant) !== false) {
 277                          return self::TYPE_CONSTANT;
 278                      }
 279                  }
 280                  return self::TYPE_STRING;
 281              case 'double':
 282              case 'float':
 283              case 'integer':
 284                  return self::TYPE_NUMBER;
 285              case 'array':
 286                  return self::TYPE_ARRAY;
 287              case 'NULL':
 288                  return self::TYPE_NULL;
 289              case 'object':
 290              case 'resource':
 291              case 'unknown type':
 292              default:
 293                  return self::TYPE_OTHER;
 294          }
 295      }
 296  
 297      /**
 298       * @throws Exception\RuntimeException
 299       * @return string
 300       */
 301      public function generate()
 302      {
 303          $type = $this->type;
 304  
 305          if ($type != self::TYPE_AUTO) {
 306              $type = $this->getValidatedType($type);
 307          }
 308  
 309          $value = $this->value;
 310  
 311          if ($type == self::TYPE_AUTO) {
 312              $type = $this->getAutoDeterminedType($value);
 313          }
 314  
 315          if ($type == self::TYPE_ARRAY) {
 316              foreach ($value as &$curValue) {
 317                  if ($curValue instanceof self) {
 318                      continue;
 319                  }
 320                  $curValue = new self($curValue, self::TYPE_AUTO, self::OUTPUT_MULTIPLE_LINE, $this->getConstants());
 321              }
 322          }
 323  
 324          $output = '';
 325  
 326          switch ($type) {
 327              case self::TYPE_BOOLEAN:
 328              case self::TYPE_BOOL:
 329                  $output .= ($value ? 'true' : 'false');
 330                  break;
 331              case self::TYPE_STRING:
 332                  $output .= self::escape($value);
 333                  break;
 334              case self::TYPE_NULL:
 335                  $output .= 'null';
 336                  break;
 337              case self::TYPE_NUMBER:
 338              case self::TYPE_INTEGER:
 339              case self::TYPE_INT:
 340              case self::TYPE_FLOAT:
 341              case self::TYPE_DOUBLE:
 342              case self::TYPE_CONSTANT:
 343                  $output .= $value;
 344                  break;
 345              case self::TYPE_ARRAY:
 346                  $output .= 'array(';
 347                  if ($this->outputMode == self::OUTPUT_MULTIPLE_LINE) {
 348                      $output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1);
 349                  }
 350                  $outputParts = array();
 351                  $noKeyIndex  = 0;
 352                  foreach ($value as $n => $v) {
 353                      /* @var $v ValueGenerator */
 354                      $v->setArrayDepth($this->arrayDepth + 1);
 355                      $partV = $v->generate();
 356                      $short = false;
 357                      if (is_int($n)) {
 358                          if ($n === $noKeyIndex) {
 359                              $short = true;
 360                              $noKeyIndex++;
 361                          } else {
 362                              $noKeyIndex = max($n + 1, $noKeyIndex);
 363                          }
 364                      }
 365  
 366                      if ($short) {
 367                          $outputParts[] = $partV;
 368                      } else {
 369                          $outputParts[] = (is_int($n) ? $n : self::escape($n)) . ' => ' . $partV;
 370                      }
 371                  }
 372                  $padding = ($this->outputMode == self::OUTPUT_MULTIPLE_LINE)
 373                      ? self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1)
 374                      : ' ';
 375                  $output .= implode(',' . $padding, $outputParts);
 376                  if ($this->outputMode == self::OUTPUT_MULTIPLE_LINE) {
 377                      if (count($outputParts) > 0) {
 378                          $output .= ',';
 379                      }
 380                      $output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth);
 381                  }
 382                  $output .= ')';
 383                  break;
 384              case self::TYPE_OTHER:
 385              default:
 386                  throw new Exception\RuntimeException(
 387                      sprintf('Type "%s" is unknown or cannot be used as property default value.', get_class($value))
 388                  );
 389          }
 390  
 391          return $output;
 392      }
 393  
 394      /**
 395       * Quotes value for PHP code.
 396       *
 397       * @param  string $input Raw string.
 398       * @param  bool $quote Whether add surrounding quotes or not.
 399       * @return string PHP-ready code.
 400       */
 401      public static function escape($input, $quote = true)
 402      {
 403          $output = addcslashes($input, "\\'");
 404  
 405          // adds quoting strings
 406          if ($quote) {
 407              $output = "'" . $output . "'";
 408          }
 409  
 410          return $output;
 411      }
 412  
 413      /**
 414       * @param  string $outputMode
 415       * @return ValueGenerator
 416       */
 417      public function setOutputMode($outputMode)
 418      {
 419          $this->outputMode = (string) $outputMode;
 420          return $this;
 421      }
 422  
 423      /**
 424       * @return string
 425       */
 426      public function getOutputMode()
 427      {
 428          return $this->outputMode;
 429      }
 430  
 431      public function __toString()
 432      {
 433          return $this->generate();
 434      }
 435  }


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