Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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

ParameterGenerator.php

Zuletzt modifiziert: 09.10.2024, 12:57 - Dateigröße: 7.78 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-2015 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\ParameterReflection;
013   
014  class ParameterGenerator extends AbstractGenerator
015  {
016      /**
017       * @var string
018       */
019      protected $name = null;
020   
021      /**
022       * @var string
023       */
024      protected $type = null;
025   
026      /**
027       * @var string|ValueGenerator
028       */
029      protected $defaultValue = null;
030   
031      /**
032       * @var int
033       */
034      protected $position = null;
035   
036      /**
037       * @var bool
038       */
039      protected $passedByReference = false;
040   
041      /**
042       * @var array
043       */
044      protected static $simple = array('int', 'bool', 'string', 'float', 'resource', 'mixed', 'object');
045   
046      /**
047       * @param  ParameterReflection $reflectionParameter
048       * @return ParameterGenerator
049       */
050      public static function fromReflection(ParameterReflection $reflectionParameter)
051      {
052          $param = new ParameterGenerator();
053          $param->setName($reflectionParameter->getName());
054   
055          if ($reflectionParameter->isArray()) {
056              $param->setType('array');
057          } elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) {
058              $param->setType('callable');
059          } else {
060              $typeClass = $reflectionParameter->getClass();
061              if ($typeClass) {
062                  $parameterType = $typeClass->getName();
063                  $currentNamespace = $reflectionParameter->getDeclaringClass()->getNamespaceName();
064   
065                  if (!empty($currentNamespace) && substr($parameterType, 0, strlen($currentNamespace)) == $currentNamespace) {
066                      $parameterType = substr($parameterType, strlen($currentNamespace) + 1);
067                  } else {
068                      $parameterType = '\\' . trim($parameterType, '\\');
069                  }
070   
071                  $param->setType($parameterType);
072              }
073          }
074   
075          $param->setPosition($reflectionParameter->getPosition());
076   
077          if ($reflectionParameter->isOptional()) {
078              $param->setDefaultValue($reflectionParameter->getDefaultValue());
079          }
080          $param->setPassedByReference($reflectionParameter->isPassedByReference());
081   
082          return $param;
083      }
084   
085      /**
086       * Generate from array
087       *
088       * @configkey name              string                                          [required] Class Name
089       * @configkey type              string
090       * @configkey defaultvalue      null|bool|string|int|float|array|ValueGenerator
091       * @configkey passedbyreference bool
092       * @configkey position          int
093       * @configkey sourcedirty       bool
094       * @configkey indentation       string
095       * @configkey sourcecontent     string
096       *
097       * @throws Exception\InvalidArgumentException
098       * @param  array $array
099       * @return ParameterGenerator
100       */
101      public static function fromArray(array $array)
102      {
103          if (!isset($array['name'])) {
104              throw new Exception\InvalidArgumentException(
105                  'Paramerer generator requires that a name is provided for this object'
106              );
107          }
108   
109          $param = new static($array['name']);
110          foreach ($array as $name => $value) {
111              // normalize key
112              switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
113                  case 'type':
114                      $param->setType($value);
115                      break;
116                  case 'defaultvalue':
117                      $param->setDefaultValue($value);
118                      break;
119                  case 'passedbyreference':
120                      $param->setPassedByReference($value);
121                      break;
122                  case 'position':
123                      $param->setPosition($value);
124                      break;
125                  case 'sourcedirty':
126                      $param->setSourceDirty($value);
127                      break;
128                  case 'indentation':
129                      $param->setIndentation($value);
130                      break;
131                  case 'sourcecontent':
132                      $param->setSourceContent($value);
133                      break;
134              }
135          }
136   
137          return $param;
138      }
139   
140      /**
141       * @param  string $name
142       * @param  string $type
143       * @param  mixed $defaultValue
144       * @param  int $position
145       * @param  bool $passByReference
146       */
147      public function __construct(
148          $name = null,
149          $type = null,
150          $defaultValue = null,
151          $position = null,
152          $passByReference = false
153      ) {
154          if (null !== $name) {
155              $this->setName($name);
156          }
157          if (null !== $type) {
158              $this->setType($type);
159          }
160          if (null !== $defaultValue) {
161              $this->setDefaultValue($defaultValue);
162          }
163          if (null !== $position) {
164              $this->setPosition($position);
165          }
166          if (false !== $passByReference) {
167              $this->setPassedByReference(true);
168          }
169      }
170   
171      /**
172       * @param  string $type
173       * @return ParameterGenerator
174       */
175      public function setType($type)
176      {
177          $this->type = (string) $type;
178          return $this;
179      }
180   
181      /**
182       * @return string
183       */
184      public function getType()
185      {
186          return $this->type;
187      }
188   
189      /**
190       * @param  string $name
191       * @return ParameterGenerator
192       */
193      public function setName($name)
194      {
195          $this->name = (string) $name;
196          return $this;
197      }
198   
199      /**
200       * @return string
201       */
202      public function getName()
203      {
204          return $this->name;
205      }
206   
207      /**
208       * Set the default value of the parameter.
209       *
210       * Certain variables are difficult to express
211       *
212       * @param  null|bool|string|int|float|array|ValueGenerator $defaultValue
213       * @return ParameterGenerator
214       */
215      public function setDefaultValue($defaultValue)
216      {
217          if (!($defaultValue instanceof ValueGenerator)) {
218              $defaultValue = new ValueGenerator($defaultValue);
219          }
220          $this->defaultValue = $defaultValue;
221   
222          return $this;
223      }
224   
225      /**
226       * @return string
227       */
228      public function getDefaultValue()
229      {
230          return $this->defaultValue;
231      }
232   
233      /**
234       * @param  int $position
235       * @return ParameterGenerator
236       */
237      public function setPosition($position)
238      {
239          $this->position = (int) $position;
240          return $this;
241      }
242   
243      /**
244       * @return int
245       */
246      public function getPosition()
247      {
248          return $this->position;
249      }
250   
251      /**
252       * @return bool
253       */
254      public function getPassedByReference()
255      {
256          return $this->passedByReference;
257      }
258   
259      /**
260       * @param  bool $passedByReference
261       * @return ParameterGenerator
262       */
263      public function setPassedByReference($passedByReference)
264      {
265          $this->passedByReference = (bool) $passedByReference;
266          return $this;
267      }
268   
269      /**
270       * @return string
271       */
272      public function generate()
273      {
274          $output = '';
275   
276          if ($this->type && !in_array($this->type, static::$simple)) {
277              $output .= $this->type . ' ';
278          }
279   
280          if (true === $this->passedByReference) {
281              $output .= '&';
282          }
283   
284          $output .= '$' . $this->name;
285   
286          if ($this->defaultValue !== null) {
287              $output .= ' = ';
288              if (is_string($this->defaultValue)) {
289                  $output .= ValueGenerator::escape($this->defaultValue);
290              } elseif ($this->defaultValue instanceof ValueGenerator) {
291                  $this->defaultValue->setOutputMode(ValueGenerator::OUTPUT_SINGLE_LINE);
292                  $output .= $this->defaultValue;
293              } else {
294                  $output .= $this->defaultValue;
295              }
296          }
297   
298          return $output;
299      }
300  }
301