Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Code / Generator / ParameterGenerator.php
blobef4eb5a3ad96fd2fbbafde3e5082f9eb21ea7517
1 <?php
2 /**
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
8 */
10 namespace Zend\Code\Generator;
12 use Zend\Code\Reflection\ParameterReflection;
14 class ParameterGenerator extends AbstractGenerator
16 /**
17 * @var string
19 protected $name = null;
21 /**
22 * @var string
24 protected $type = null;
26 /**
27 * @var string|ValueGenerator
29 protected $defaultValue = null;
31 /**
32 * @var int
34 protected $position = null;
36 /**
37 * @var bool
39 protected $passedByReference = false;
41 /**
42 * @var array
44 protected static $simple = array('int', 'bool', 'string', 'float', 'resource', 'mixed', 'object');
46 /**
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');
59 } else {
60 $typeClass = $reflectionParameter->getClass();
61 if ($typeClass) {
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);
67 } else {
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());
82 return $param;
85 /**
86 * Generate from array
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
98 * @param array $array
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) {
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;
137 return $param;
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;
174 return $this;
178 * @return string
180 public function getType()
182 return $this->type;
186 * @param string $name
187 * @return ParameterGenerator
189 public function setName($name)
191 $this->name = (string) $name;
192 return $this;
196 * @return string
198 public function getName()
200 return $this->name;
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;
218 return $this;
222 * @return string
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;
236 return $this;
240 * @return int
242 public function getPosition()
244 return $this->position;
248 * @return bool
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;
262 return $this;
266 * @return string
268 public function generate()
270 $output = '';
272 if ($this->type && !in_array($this->type, static::$simple)) {
273 $output .= $this->type . ' ';
276 if (true === $this->passedByReference) {
277 $output .= '&';
280 $output .= '$' . $this->name;
282 if ($this->defaultValue !== null) {
283 $output .= ' = ';
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;
289 } else {
290 $output .= $this->defaultValue;
294 return $output;