PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/pcelta/zf2
PHP | 248 lines | 121 code | 32 blank | 95 comment | 16 complexity | c191ece4a18156f4d35d9b8ae7ef3c19 MD5 | raw file
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Code
  9. */
  10. namespace Zend\Code\Generator;
  11. use Zend\Code\Reflection\ParameterReflection;
  12. /**
  13. *
  14. * @category Zend
  15. * @package Zend_Code_Generator
  16. */
  17. class ParameterGenerator extends AbstractGenerator
  18. {
  19. /**
  20. * @var string
  21. */
  22. protected $name = null;
  23. /**
  24. * @var string
  25. */
  26. protected $type = null;
  27. /**
  28. * @var string|ValueGenerator
  29. */
  30. protected $defaultValue = null;
  31. /**
  32. * @var int
  33. */
  34. protected $position = null;
  35. /**
  36. * @var bool
  37. */
  38. protected $passedByReference = false;
  39. /**
  40. * @var array
  41. */
  42. protected static $simple = array('int', 'bool', 'string', 'float', 'resource', 'mixed', 'object');
  43. /**
  44. * fromReflection()
  45. *
  46. * @param ParameterReflection $reflectionParameter
  47. * @return ParameterGenerator
  48. */
  49. public static function fromReflection(ParameterReflection $reflectionParameter)
  50. {
  51. $param = new ParameterGenerator();
  52. $param->setName($reflectionParameter->getName());
  53. if ($reflectionParameter->isArray()) {
  54. $param->setType('array');
  55. } else {
  56. $typeClass = $reflectionParameter->getClass();
  57. if ($typeClass !== null) {
  58. $param->setType($typeClass->getName());
  59. }
  60. }
  61. $param->setPosition($reflectionParameter->getPosition());
  62. if ($reflectionParameter->isOptional()) {
  63. $param->setDefaultValue($reflectionParameter->getDefaultValue());
  64. }
  65. $param->setPassedByReference($reflectionParameter->isPassedByReference());
  66. return $param;
  67. }
  68. public function __construct($name = null, $type = null, $defaultValue = null, $position = null,
  69. $passByReference = false)
  70. {
  71. if ($name !== null) {
  72. $this->setName($name);
  73. }
  74. if ($type !== null) {
  75. $this->setType($type);
  76. }
  77. if ($defaultValue !== null) {
  78. $this->setDefaultValue($defaultValue);
  79. }
  80. if ($position !== null) {
  81. $this->setPosition($position);
  82. }
  83. if ($passByReference !== false) {
  84. $this->setPassedByReference(true);
  85. }
  86. }
  87. /**
  88. * setType()
  89. *
  90. * @param string $type
  91. * @return ParameterGenerator
  92. */
  93. public function setType($type)
  94. {
  95. $this->type = $type;
  96. return $this;
  97. }
  98. /**
  99. * getType()
  100. *
  101. * @return string
  102. */
  103. public function getType()
  104. {
  105. return $this->type;
  106. }
  107. /**
  108. * setName()
  109. *
  110. * @param string $name
  111. * @return ParameterGenerator
  112. */
  113. public function setName($name)
  114. {
  115. $this->name = $name;
  116. return $this;
  117. }
  118. /**
  119. * getName()
  120. *
  121. * @return string
  122. */
  123. public function getName()
  124. {
  125. return $this->name;
  126. }
  127. /**
  128. * Set the default value of the parameter.
  129. *
  130. * Certain variables are difficult to express
  131. *
  132. * @param null|bool|string|int|float|array|ValueGenerator $defaultValue
  133. * @return ParameterGenerator
  134. */
  135. public function setDefaultValue($defaultValue)
  136. {
  137. if (!($defaultValue instanceof ValueGenerator)) {
  138. $defaultValue = new ValueGenerator($defaultValue);
  139. }
  140. $this->defaultValue = $defaultValue;
  141. return $this;
  142. }
  143. /**
  144. * getDefaultValue()
  145. *
  146. * @return string
  147. */
  148. public function getDefaultValue()
  149. {
  150. return $this->defaultValue;
  151. }
  152. /**
  153. * setPosition()
  154. *
  155. * @param int $position
  156. * @return ParameterGenerator
  157. */
  158. public function setPosition($position)
  159. {
  160. $this->position = $position;
  161. return $this;
  162. }
  163. /**
  164. * getPosition()
  165. *
  166. * @return int
  167. */
  168. public function getPosition()
  169. {
  170. return $this->position;
  171. }
  172. /**
  173. * @return bool
  174. */
  175. public function getPassedByReference()
  176. {
  177. return $this->passedByReference;
  178. }
  179. /**
  180. * @param bool $passedByReference
  181. * @return ParameterGenerator
  182. */
  183. public function setPassedByReference($passedByReference)
  184. {
  185. $this->passedByReference = $passedByReference;
  186. return $this;
  187. }
  188. /**
  189. * generate()
  190. *
  191. * @return string
  192. */
  193. public function generate()
  194. {
  195. $output = '';
  196. if ($this->type && !in_array($this->type, static::$simple)) {
  197. $output .= $this->type . ' ';
  198. }
  199. if ($this->passedByReference === true) {
  200. $output .= '&';
  201. }
  202. $output .= '$' . $this->name;
  203. if ($this->defaultValue !== null) {
  204. $output .= ' = ';
  205. if (is_string($this->defaultValue)) {
  206. $output .= ValueGenerator::escape($this->defaultValue);
  207. } elseif ($this->defaultValue instanceof ValueGenerator) {
  208. $this->defaultValue->setOutputMode(ValueGenerator::OUTPUT_SINGLE_LINE);
  209. $output .= $this->defaultValue;
  210. } else {
  211. $output .= $this->defaultValue;
  212. }
  213. }
  214. return $output;
  215. }
  216. }