[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
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\ParameterReflection; 13 14 class ParameterGenerator extends AbstractGenerator 15 { 16 /** 17 * @var string 18 */ 19 protected $name = null; 20 21 /** 22 * @var string 23 */ 24 protected $type = null; 25 26 /** 27 * @var string|ValueGenerator 28 */ 29 protected $defaultValue = null; 30 31 /** 32 * @var int 33 */ 34 protected $position = null; 35 36 /** 37 * @var bool 38 */ 39 protected $passedByReference = false; 40 41 /** 42 * @var array 43 */ 44 protected static $simple = array('int', 'bool', 'string', 'float', 'resource', 'mixed', 'object'); 45 46 /** 47 * @param ParameterReflection $reflectionParameter 48 * @return ParameterGenerator 49 */ 50 public static function fromReflection(ParameterReflection $reflectionParameter) 51 { 52 $param = new ParameterGenerator(); 53 $param->setName($reflectionParameter->getName()); 54 55 if ($reflectionParameter->isArray()) { 56 $param->setType('array'); 57 } elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) { 58 $param->setType('callable'); 59 } else { 60 $typeClass = $reflectionParameter->getClass(); 61 if ($typeClass) { 62 $parameterType = $typeClass->getName(); 63 $currentNamespace = $reflectionParameter->getDeclaringClass()->getNamespaceName(); 64 65 if (!empty($currentNamespace) && substr($parameterType, 0, strlen($currentNamespace)) == $currentNamespace) { 66 $parameterType = substr($parameterType, strlen($currentNamespace) + 1); 67 } else { 68 $parameterType = '\\' . trim($parameterType, '\\'); 69 } 70 71 $param->setType($parameterType); 72 } 73 } 74 75 $param->setPosition($reflectionParameter->getPosition()); 76 77 if ($reflectionParameter->isOptional()) { 78 $param->setDefaultValue($reflectionParameter->getDefaultValue()); 79 } 80 $param->setPassedByReference($reflectionParameter->isPassedByReference()); 81 82 return $param; 83 } 84 85 /** 86 * Generate from array 87 * 88 * @configkey name string [required] Class Name 89 * @configkey type string 90 * @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator 91 * @configkey passedbyreference bool 92 * @configkey position int 93 * @configkey sourcedirty bool 94 * @configkey indentation string 95 * @configkey sourcecontent string 96 * 97 * @throws Exception\InvalidArgumentException 98 * @param array $array 99 * @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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |