3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Code\Generator
;
12 use Zend\Code\Reflection\ParameterReflection
;
14 class ParameterGenerator
extends AbstractGenerator
19 protected $name = null;
24 protected $type = null;
27 * @var string|ValueGenerator
29 protected $defaultValue = null;
34 protected $position = null;
39 protected $passedByReference = false;
44 protected static $simple = array('int', 'bool', 'string', 'float', 'resource', 'mixed', 'object');
47 * @param ParameterReflection $reflectionParameter
48 * @return ParameterGenerator
50 public static function fromReflection(ParameterReflection
$reflectionParameter)
52 $param = new ParameterGenerator();
53 $param->setName($reflectionParameter->getName());
55 if ($reflectionParameter->isArray()) {
56 $param->setType('array');
57 } elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) {
58 $param->setType('callable');
60 $typeClass = $reflectionParameter->getClass();
62 $parameterType = $typeClass->getName();
63 $currentNamespace = $reflectionParameter->getDeclaringClass()->getNamespaceName();
65 if (!empty($currentNamespace) && substr($parameterType, 0, strlen($currentNamespace)) == $currentNamespace) {
66 $parameterType = substr($parameterType, strlen($currentNamespace) +
1);
68 $parameterType = '\\' . trim($parameterType, '\\');
71 $param->setType($parameterType);
75 $param->setPosition($reflectionParameter->getPosition());
77 if ($reflectionParameter->isOptional()) {
78 $param->setDefaultValue($reflectionParameter->getDefaultValue());
80 $param->setPassedByReference($reflectionParameter->isPassedByReference());
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
97 * @throws Exception\InvalidArgumentException
99 * @return ParameterGenerator
101 public static function fromArray(array $array)
103 if (!isset($array['name'])) {
104 throw new Exception\
InvalidArgumentException(
105 'Paramerer generator requires that a name is provided for this object'
109 $param = new static($array['name']);
110 foreach ($array as $name => $value) {
112 switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
114 $param->setType($value);
117 $param->setDefaultValue($value);
119 case 'passedbyreference':
120 $param->setPassedByReference($value);
123 $param->setPosition($value);
126 $param->setSourceDirty($value);
129 $param->setIndentation($value);
131 case 'sourcecontent':
132 $param->setSourceContent($value);
141 * @param string $name
142 * @param string $type
143 * @param mixed $defaultValue
144 * @param int $position
145 * @param bool $passByReference
147 public function __construct($name = null, $type = null, $defaultValue = null, $position = null,
148 $passByReference = false)
150 if (null !== $name) {
151 $this->setName($name);
153 if (null !== $type) {
154 $this->setType($type);
156 if (null !== $defaultValue) {
157 $this->setDefaultValue($defaultValue);
159 if (null !== $position) {
160 $this->setPosition($position);
162 if (false !== $passByReference) {
163 $this->setPassedByReference(true);
168 * @param string $type
169 * @return ParameterGenerator
171 public function setType($type)
173 $this->type
= (string) $type;
180 public function getType()
186 * @param string $name
187 * @return ParameterGenerator
189 public function setName($name)
191 $this->name
= (string) $name;
198 public function getName()
204 * Set the default value of the parameter.
206 * Certain variables are difficult to express
208 * @param null|bool|string|int|float|array|ValueGenerator $defaultValue
209 * @return ParameterGenerator
211 public function setDefaultValue($defaultValue)
213 if (!($defaultValue instanceof ValueGenerator
)) {
214 $defaultValue = new ValueGenerator($defaultValue);
216 $this->defaultValue
= $defaultValue;
224 public function getDefaultValue()
226 return $this->defaultValue
;
230 * @param int $position
231 * @return ParameterGenerator
233 public function setPosition($position)
235 $this->position
= (int) $position;
242 public function getPosition()
244 return $this->position
;
250 public function getPassedByReference()
252 return $this->passedByReference
;
256 * @param bool $passedByReference
257 * @return ParameterGenerator
259 public function setPassedByReference($passedByReference)
261 $this->passedByReference
= (bool) $passedByReference;
268 public function generate()
272 if ($this->type
&& !in_array($this->type
, static::$simple)) {
273 $output .= $this->type
. ' ';
276 if (true === $this->passedByReference
) {
280 $output .= '$' . $this->name
;
282 if ($this->defaultValue
!== null) {
284 if (is_string($this->defaultValue
)) {
285 $output .= ValueGenerator
::escape($this->defaultValue
);
286 } elseif ($this->defaultValue
instanceof ValueGenerator
) {
287 $this->defaultValue
->setOutputMode(ValueGenerator
::OUTPUT_SINGLE_LINE
);
288 $output .= $this->defaultValue
;
290 $output .= $this->defaultValue
;