2
0

Parameter.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_CodeGenerator
  17. * @subpackage PHP
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_CodeGenerator_Php_Abstract
  24. */
  25. require_once 'Zend/CodeGenerator/Php/Abstract.php';
  26. /**
  27. * @see Zend_CodeGenerator_Php_ParameterDefaultValue
  28. */
  29. require_once 'Zend/CodeGenerator/Php/Parameter/DefaultValue.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_CodeGenerator
  33. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
  37. {
  38. /**
  39. * @var string
  40. */
  41. protected $_type = null;
  42. /**
  43. * @var string
  44. */
  45. protected $_name = null;
  46. /**
  47. * @var string
  48. */
  49. protected $_defaultValue = null;
  50. /**
  51. * @var int
  52. */
  53. protected $_position = null;
  54. /**
  55. * @var bool
  56. */
  57. protected $_passedByReference = false;
  58. /**
  59. * fromReflection()
  60. *
  61. * @param Zend_Reflection_Parameter $reflectionParameter
  62. * @return Zend_CodeGenerator_Php_Parameter
  63. */
  64. public static function fromReflection(Zend_Reflection_Parameter $reflectionParameter)
  65. {
  66. $param = new Zend_CodeGenerator_Php_Parameter();
  67. $param->setName($reflectionParameter->getName());
  68. try {
  69. $param->setType($reflectionParameter->getType());
  70. } catch(Zend_Reflection_Exception $e) {
  71. if($reflectionParameter->isArray()) {
  72. $param->setType('array');
  73. } else {
  74. $typeClass = $reflectionParameter->getClass();
  75. if($typeClass !== null) {
  76. $param->setType($typeClass->getName());
  77. }
  78. }
  79. }
  80. $param->setPosition($reflectionParameter->getPosition());
  81. if($reflectionParameter->isOptional()) {
  82. $param->setDefaultValue($reflectionParameter->getDefaultValue());
  83. }
  84. $param->setPassedByReference($reflectionParameter->isPassedByReference());
  85. return $param;
  86. }
  87. /**
  88. * setType()
  89. *
  90. * @param string $type
  91. * @return Zend_CodeGenerator_Php_Parameter
  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 Zend_CodeGenerator_Php_Parameter
  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 expres
  131. *
  132. * @param null|bool|string|int|float|Zend_CodeGenerator_Php_Parameter_DefaultValue $defaultValue
  133. * @return Zend_CodeGenerator_Php_Parameter
  134. */
  135. public function setDefaultValue($defaultValue)
  136. {
  137. if($defaultValue === null) {
  138. $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("null");
  139. } else if(is_array($defaultValue)) {
  140. $defaultValue = str_replace(array("\r", "\n"), "", var_export($defaultValue, true));
  141. $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue($defaultValue);
  142. } else if(is_bool($defaultValue)) {
  143. if($defaultValue == true) {
  144. $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("true");
  145. } else {
  146. $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("false");
  147. }
  148. } else {
  149. $this->_defaultValue = $defaultValue;
  150. }
  151. return $this;
  152. }
  153. /**
  154. * getDefaultValue()
  155. *
  156. * @return string
  157. */
  158. public function getDefaultValue()
  159. {
  160. return $this->_defaultValue;
  161. }
  162. /**
  163. * setPosition()
  164. *
  165. * @param int $position
  166. * @return Zend_CodeGenerator_Php_Parameter
  167. */
  168. public function setPosition($position)
  169. {
  170. $this->_position = $position;
  171. return $this;
  172. }
  173. /**
  174. * getPosition()
  175. *
  176. * @return int
  177. */
  178. public function getPosition()
  179. {
  180. return $this->_position;
  181. }
  182. /**
  183. * @return bool
  184. */
  185. public function getPassedByReference()
  186. {
  187. return $this->_passedByReference;
  188. }
  189. /**
  190. * @param bool $passedByReference
  191. * @return Zend_CodeGenerator_Php_Parameter
  192. */
  193. public function setPassedByReference($passedByReference)
  194. {
  195. $this->_passedByReference = $passedByReference;
  196. return $this;
  197. }
  198. /**
  199. * generate()
  200. *
  201. * @return string
  202. */
  203. public function generate()
  204. {
  205. $output = '';
  206. if ($this->_type) {
  207. $output .= $this->_type . ' ';
  208. }
  209. if($this->_passedByReference === true) {
  210. $output .= '&';
  211. }
  212. $output .= '$' . $this->_name;
  213. if ($this->_defaultValue !== null) {
  214. $output .= ' = ';
  215. if (is_string($this->_defaultValue)) {
  216. $output .= '\'' . $this->_defaultValue . '\'';
  217. } else if($this->_defaultValue instanceof Zend_CodeGenerator_Php_ParameterDefaultValue) {
  218. $output .= (string)$this->_defaultValue;
  219. } else {
  220. $output .= $this->_defaultValue;
  221. }
  222. }
  223. return $output;
  224. }
  225. }