[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/zendframework/zend-code/src/Generator/ -> PropertyGenerator.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-2016 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\Code\Reflection\PropertyReflection;
  13  
  14  use function sprintf;
  15  use function str_replace;
  16  use function strtolower;
  17  
  18  class PropertyGenerator extends AbstractMemberGenerator
  19  {
  20      const FLAG_CONSTANT = 0x08;
  21  
  22      /**
  23       * @var bool
  24       */
  25      protected $isConst;
  26  
  27      /**
  28       * @var PropertyValueGenerator
  29       */
  30      protected $defaultValue;
  31  
  32      /**
  33       * @var bool
  34       */
  35      private $omitDefaultValue = false;
  36  
  37      /**
  38       * @param  PropertyReflection $reflectionProperty
  39       * @return PropertyGenerator
  40       */
  41      public static function fromReflection(PropertyReflection $reflectionProperty)
  42      {
  43          $property = new static();
  44  
  45          $property->setName($reflectionProperty->getName());
  46  
  47          $allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties();
  48  
  49          $defaultValue = $allDefaultProperties[$reflectionProperty->getName()];
  50          $property->setDefaultValue($defaultValue);
  51          if ($defaultValue === null) {
  52              $property->omitDefaultValue = true;
  53          }
  54  
  55          if ($reflectionProperty->getDocComment() != '') {
  56              $property->setDocBlock(DocBlockGenerator::fromReflection($reflectionProperty->getDocBlock()));
  57          }
  58  
  59          if ($reflectionProperty->isStatic()) {
  60              $property->setStatic(true);
  61          }
  62  
  63          if ($reflectionProperty->isPrivate()) {
  64              $property->setVisibility(self::VISIBILITY_PRIVATE);
  65          } elseif ($reflectionProperty->isProtected()) {
  66              $property->setVisibility(self::VISIBILITY_PROTECTED);
  67          } else {
  68              $property->setVisibility(self::VISIBILITY_PUBLIC);
  69          }
  70  
  71          $property->setSourceDirty(false);
  72  
  73          return $property;
  74      }
  75  
  76      /**
  77       * Generate from array
  78       *
  79       * @configkey name               string                                          [required] Class Name
  80       * @configkey const              bool
  81       * @configkey defaultvalue       null|bool|string|int|float|array|ValueGenerator
  82       * @configkey flags              int
  83       * @configkey abstract           bool
  84       * @configkey final              bool
  85       * @configkey static             bool
  86       * @configkey visibility         string
  87       * @configkey omitdefaultvalue   bool
  88       *
  89       * @throws Exception\InvalidArgumentException
  90       * @param  array $array
  91       * @return PropertyGenerator
  92       */
  93      public static function fromArray(array $array)
  94      {
  95          if (! isset($array['name'])) {
  96              throw new Exception\InvalidArgumentException(
  97                  'Property generator requires that a name is provided for this object'
  98              );
  99          }
 100  
 101          $property = new static($array['name']);
 102          foreach ($array as $name => $value) {
 103              // normalize key
 104              switch (strtolower(str_replace(['.', '-', '_'], '', $name))) {
 105                  case 'const':
 106                      $property->setConst($value);
 107                      break;
 108                  case 'defaultvalue':
 109                      $property->setDefaultValue($value);
 110                      break;
 111                  case 'docblock':
 112                      $docBlock = $value instanceof DocBlockGenerator ? $value : DocBlockGenerator::fromArray($value);
 113                      $property->setDocBlock($docBlock);
 114                      break;
 115                  case 'flags':
 116                      $property->setFlags($value);
 117                      break;
 118                  case 'abstract':
 119                      $property->setAbstract($value);
 120                      break;
 121                  case 'final':
 122                      $property->setFinal($value);
 123                      break;
 124                  case 'static':
 125                      $property->setStatic($value);
 126                      break;
 127                  case 'visibility':
 128                      $property->setVisibility($value);
 129                      break;
 130                  case 'omitdefaultvalue':
 131                      $property->omitDefaultValue($value);
 132                      break;
 133              }
 134          }
 135  
 136          return $property;
 137      }
 138  
 139      /**
 140       * @param string $name
 141       * @param PropertyValueGenerator|string|array $defaultValue
 142       * @param int $flags
 143       */
 144      public function __construct($name = null, $defaultValue = null, $flags = self::FLAG_PUBLIC)
 145      {
 146          if (null !== $name) {
 147              $this->setName($name);
 148          }
 149          if (null !== $defaultValue) {
 150              $this->setDefaultValue($defaultValue);
 151          }
 152          if ($flags !== self::FLAG_PUBLIC) {
 153              $this->setFlags($flags);
 154          }
 155      }
 156  
 157      /**
 158       * @param  bool $const
 159       * @return PropertyGenerator
 160       */
 161      public function setConst($const)
 162      {
 163          if ($const) {
 164              $this->setFlags(self::FLAG_CONSTANT);
 165          } else {
 166              $this->removeFlag(self::FLAG_CONSTANT);
 167          }
 168  
 169          return $this;
 170      }
 171  
 172      /**
 173       * @return bool
 174       */
 175      public function isConst()
 176      {
 177          return (bool) ($this->flags & self::FLAG_CONSTANT);
 178      }
 179  
 180      /**
 181       * @param PropertyValueGenerator|mixed $defaultValue
 182       * @param string                       $defaultValueType
 183       * @param string                       $defaultValueOutputMode
 184       *
 185       * @return PropertyGenerator
 186       */
 187      public function setDefaultValue(
 188          $defaultValue,
 189          $defaultValueType = PropertyValueGenerator::TYPE_AUTO,
 190          $defaultValueOutputMode = PropertyValueGenerator::OUTPUT_MULTIPLE_LINE
 191      ) {
 192          if (! $defaultValue instanceof PropertyValueGenerator) {
 193              $defaultValue = new PropertyValueGenerator($defaultValue, $defaultValueType, $defaultValueOutputMode);
 194          }
 195  
 196          $this->defaultValue = $defaultValue;
 197  
 198          return $this;
 199      }
 200  
 201      /**
 202       * @return PropertyValueGenerator
 203       */
 204      public function getDefaultValue()
 205      {
 206          return $this->defaultValue;
 207      }
 208  
 209      /**
 210       * @throws Exception\RuntimeException
 211       * @return string
 212       */
 213      public function generate()
 214      {
 215          $name         = $this->getName();
 216          $defaultValue = $this->getDefaultValue();
 217  
 218          $output = '';
 219  
 220          if (($docBlock = $this->getDocBlock()) !== null) {
 221              $docBlock->setIndentation('    ');
 222              $output .= $docBlock->generate();
 223          }
 224  
 225          if ($this->isConst()) {
 226              if ($defaultValue !== null && ! $defaultValue->isValidConstantType()) {
 227                  throw new Exception\RuntimeException(sprintf(
 228                      'The property %s is said to be '
 229                      . 'constant but does not have a valid constant value.',
 230                      $this->name
 231                  ));
 232              }
 233              $output .= $this->indentation . $this->getVisibility() . ' const ' . $name . ' = '
 234                  . ($defaultValue !== null ? $defaultValue->generate() : 'null;');
 235  
 236              return $output;
 237          }
 238  
 239          $output .= $this->indentation . $this->getVisibility() . ($this->isStatic() ? ' static' : '') . ' $' . $name;
 240  
 241          if ($this->omitDefaultValue) {
 242              return $output . ';';
 243          }
 244  
 245          return $output . ' = ' . ($defaultValue !== null ? $defaultValue->generate() : 'null;');
 246      }
 247  
 248      /**
 249       * @param bool $omit
 250       * @return PropertyGenerator
 251       */
 252      public function omitDefaultValue(bool $omit = true)
 253      {
 254          $this->omitDefaultValue = $omit;
 255  
 256          return $this;
 257      }
 258  }


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