[ Index ]

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


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