Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

PropertyGenerator.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 7.49 KiB


001  <?php
002  /**
003   * Zend Framework (http://framework.zend.com/)
004   *
005   * @link      http://github.com/zendframework/zf2 for the canonical source repository
006   * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
007   * @license   http://framework.zend.com/license/new-bsd New BSD License
008   */
009   
010  namespace Zend\Code\Generator;
011   
012  use Zend\Code\Reflection\PropertyReflection;
013   
014  use function sprintf;
015  use function str_replace;
016  use function strtolower;
017   
018  class PropertyGenerator extends AbstractMemberGenerator
019  {
020      const FLAG_CONSTANT = 0x08;
021   
022      /**
023       * @var bool
024       */
025      protected $isConst;
026   
027      /**
028       * @var PropertyValueGenerator
029       */
030      protected $defaultValue;
031   
032      /**
033       * @var bool
034       */
035      private $omitDefaultValue = false;
036   
037      /**
038       * @param  PropertyReflection $reflectionProperty
039       * @return PropertyGenerator
040       */
041      public static function fromReflection(PropertyReflection $reflectionProperty)
042      {
043          $property = new static();
044   
045          $property->setName($reflectionProperty->getName());
046   
047          $allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties();
048   
049          $defaultValue = $allDefaultProperties[$reflectionProperty->getName()];
050          $property->setDefaultValue($defaultValue);
051          if ($defaultValue === null) {
052              $property->omitDefaultValue = true;
053          }
054   
055          if ($reflectionProperty->getDocComment() != '') {
056              $property->setDocBlock(DocBlockGenerator::fromReflection($reflectionProperty->getDocBlock()));
057          }
058   
059          if ($reflectionProperty->isStatic()) {
060              $property->setStatic(true);
061          }
062   
063          if ($reflectionProperty->isPrivate()) {
064              $property->setVisibility(self::VISIBILITY_PRIVATE);
065          } elseif ($reflectionProperty->isProtected()) {
066              $property->setVisibility(self::VISIBILITY_PROTECTED);
067          } else {
068              $property->setVisibility(self::VISIBILITY_PUBLIC);
069          }
070   
071          $property->setSourceDirty(false);
072   
073          return $property;
074      }
075   
076      /**
077       * Generate from array
078       *
079       * @configkey name               string                                          [required] Class Name
080       * @configkey const              bool
081       * @configkey defaultvalue       null|bool|string|int|float|array|ValueGenerator
082       * @configkey flags              int
083       * @configkey abstract           bool
084       * @configkey final              bool
085       * @configkey static             bool
086       * @configkey visibility         string
087       * @configkey omitdefaultvalue   bool
088       *
089       * @throws Exception\InvalidArgumentException
090       * @param  array $array
091       * @return PropertyGenerator
092       */
093      public static function fromArray(array $array)
094      {
095          if (! isset($array['name'])) {
096              throw new Exception\InvalidArgumentException(
097                  'Property generator requires that a name is provided for this object'
098              );
099          }
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  }
259