PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Code/Generator/ParameterGenerator.php

https://github.com/praveenuniyal/zf2
PHP | 296 lines | 163 code | 36 blank | 97 comment | 21 complexity | f0cc2255954b9d270cb5e8515c399e73 MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Code\Generator;
  10. use Zend\Code\Reflection\ParameterReflection;
  11. /**
  12. */
  13. class ParameterGenerator extends AbstractGenerator
  14. {
  15. /**
  16. * @var string
  17. */
  18. protected $name = null;
  19. /**
  20. * @var string
  21. */
  22. protected $type = null;
  23. /**
  24. * @var string|ValueGenerator
  25. */
  26. protected $defaultValue = null;
  27. /**
  28. * @var int
  29. */
  30. protected $position = null;
  31. /**
  32. * @var bool
  33. */
  34. protected $passedByReference = false;
  35. /**
  36. * @var array
  37. */
  38. protected static $simple = array('int', 'bool', 'string', 'float', 'resource', 'mixed', 'object');
  39. /**
  40. * @param ParameterReflection $reflectionParameter
  41. * @return ParameterGenerator
  42. */
  43. public static function fromReflection(ParameterReflection $reflectionParameter)
  44. {
  45. $param = new ParameterGenerator();
  46. $param->setName($reflectionParameter->getName());
  47. if ($reflectionParameter->isArray()) {
  48. $param->setType('array');
  49. } elseif (method_exists($reflectionParameter, 'isCallable') && $reflectionParameter->isCallable()) {
  50. $param->setType('callable');
  51. } else {
  52. $typeClass = $reflectionParameter->getClass();
  53. if ($typeClass) {
  54. $parameterType = $typeClass->getName();
  55. $currentNamespace = $reflectionParameter->getDeclaringClass()->getNamespaceName();
  56. if (substr($parameterType, 0, strlen($currentNamespace)) == $currentNamespace) {
  57. $parameterType = substr($parameterType, strlen($currentNamespace)+1);
  58. }
  59. $param->setType($parameterType);
  60. }
  61. }
  62. $param->setPosition($reflectionParameter->getPosition());
  63. if ($reflectionParameter->isOptional()) {
  64. $param->setDefaultValue($reflectionParameter->getDefaultValue());
  65. }
  66. $param->setPassedByReference($reflectionParameter->isPassedByReference());
  67. return $param;
  68. }
  69. /**
  70. * Generate from array
  71. *
  72. * @configkey name string [required] Class Name
  73. * @configkey type string
  74. * @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
  75. * @configkey passedbyreference bool
  76. * @configkey position int
  77. * @configkey sourcedirty bool
  78. * @configkey indentation string
  79. * @configkey sourcecontent string
  80. *
  81. * @throws Exception\InvalidArgumentException
  82. * @param array $array
  83. * @return ParameterGenerator
  84. */
  85. public static function fromArray(array $array)
  86. {
  87. if (!isset($array['name'])) {
  88. throw new Exception\InvalidArgumentException(
  89. 'Paramerer generator requires that a name is provided for this object'
  90. );
  91. }
  92. $param = new static($array['name']);
  93. foreach ($array as $name => $value) {
  94. // normalize key
  95. switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
  96. case 'type':
  97. $param->setType($value);
  98. break;
  99. case 'defaultvalue':
  100. $param->setDefaultValue($value);
  101. break;
  102. case 'passedbyreference':
  103. $param->setPassedByReference($value);
  104. break;
  105. case 'position':
  106. $param->setPosition($value);
  107. break;
  108. case 'sourcedirty':
  109. $param->setSourceDirty($value);
  110. break;
  111. case 'indentation':
  112. $param->setIndentation($value);
  113. break;
  114. case 'sourcecontent':
  115. $param->setSourceContent($value);
  116. break;
  117. }
  118. }
  119. return $param;
  120. }
  121. /**
  122. * @param string $name
  123. * @param string $type
  124. * @param mixed $defaultValue
  125. * @param int $position
  126. * @param bool $passByReference
  127. */
  128. public function __construct($name = null, $type = null, $defaultValue = null, $position = null,
  129. $passByReference = false)
  130. {
  131. if (null !== $name) {
  132. $this->setName($name);
  133. }
  134. if (null !== $type) {
  135. $this->setType($type);
  136. }
  137. if (null !== $defaultValue) {
  138. $this->setDefaultValue($defaultValue);
  139. }
  140. if (null !== $position) {
  141. $this->setPosition($position);
  142. }
  143. if (false !== $passByReference) {
  144. $this->setPassedByReference(true);
  145. }
  146. }
  147. /**
  148. * @param string $type
  149. * @return ParameterGenerator
  150. */
  151. public function setType($type)
  152. {
  153. $this->type = (string) $type;
  154. return $this;
  155. }
  156. /**
  157. * @return string
  158. */
  159. public function getType()
  160. {
  161. return $this->type;
  162. }
  163. /**
  164. * @param string $name
  165. * @return ParameterGenerator
  166. */
  167. public function setName($name)
  168. {
  169. $this->name = (string) $name;
  170. return $this;
  171. }
  172. /**
  173. * @return string
  174. */
  175. public function getName()
  176. {
  177. return $this->name;
  178. }
  179. /**
  180. * Set the default value of the parameter.
  181. *
  182. * Certain variables are difficult to express
  183. *
  184. * @param null|bool|string|int|float|array|ValueGenerator $defaultValue
  185. * @return ParameterGenerator
  186. */
  187. public function setDefaultValue($defaultValue)
  188. {
  189. if (!($defaultValue instanceof ValueGenerator)) {
  190. $defaultValue = new ValueGenerator($defaultValue);
  191. }
  192. $this->defaultValue = $defaultValue;
  193. return $this;
  194. }
  195. /**
  196. * @return string
  197. */
  198. public function getDefaultValue()
  199. {
  200. return $this->defaultValue;
  201. }
  202. /**
  203. * @param int $position
  204. * @return ParameterGenerator
  205. */
  206. public function setPosition($position)
  207. {
  208. $this->position = (int) $position;
  209. return $this;
  210. }
  211. /**
  212. * @return int
  213. */
  214. public function getPosition()
  215. {
  216. return $this->position;
  217. }
  218. /**
  219. * @return bool
  220. */
  221. public function getPassedByReference()
  222. {
  223. return $this->passedByReference;
  224. }
  225. /**
  226. * @param bool $passedByReference
  227. * @return ParameterGenerator
  228. */
  229. public function setPassedByReference($passedByReference)
  230. {
  231. $this->passedByReference = (bool) $passedByReference;
  232. return $this;
  233. }
  234. /**
  235. * @return string
  236. */
  237. public function generate()
  238. {
  239. $output = '';
  240. if ($this->type && !in_array($this->type, static::$simple)) {
  241. $output .= $this->type . ' ';
  242. }
  243. if (true === $this->passedByReference) {
  244. $output .= '&';
  245. }
  246. $output .= '$' . $this->name;
  247. if ($this->defaultValue !== null) {
  248. $output .= ' = ';
  249. if (is_string($this->defaultValue)) {
  250. $output .= ValueGenerator::escape($this->defaultValue);
  251. } elseif ($this->defaultValue instanceof ValueGenerator) {
  252. $this->defaultValue->setOutputMode(ValueGenerator::OUTPUT_SINGLE_LINE);
  253. $output .= $this->defaultValue;
  254. } else {
  255. $output .= $this->defaultValue;
  256. }
  257. }
  258. return $output;
  259. }
  260. }