PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/CodeGenerator/Php/Parameter.php

https://bitbucket.org/dbaltas/zend-framework-1.x-on-git
PHP | 250 lines | 110 code | 29 blank | 111 comment | 20 complexity | 92b66a1fabc2a50e995e7d3722490e15 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Parameter.php 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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. if($reflectionParameter->isArray()) {
  69. $param->setType('array');
  70. } else {
  71. $typeClass = $reflectionParameter->getClass();
  72. if($typeClass !== null) {
  73. $param->setType($typeClass->getName());
  74. }
  75. }
  76. $param->setPosition($reflectionParameter->getPosition());
  77. if($reflectionParameter->isOptional()) {
  78. $param->setDefaultValue($reflectionParameter->getDefaultValue());
  79. }
  80. $param->setPassedByReference($reflectionParameter->isPassedByReference());
  81. return $param;
  82. }
  83. /**
  84. * setType()
  85. *
  86. * @param string $type
  87. * @return Zend_CodeGenerator_Php_Parameter
  88. */
  89. public function setType($type)
  90. {
  91. $this->_type = $type;
  92. return $this;
  93. }
  94. /**
  95. * getType()
  96. *
  97. * @return string
  98. */
  99. public function getType()
  100. {
  101. return $this->_type;
  102. }
  103. /**
  104. * setName()
  105. *
  106. * @param string $name
  107. * @return Zend_CodeGenerator_Php_Parameter
  108. */
  109. public function setName($name)
  110. {
  111. $this->_name = $name;
  112. return $this;
  113. }
  114. /**
  115. * getName()
  116. *
  117. * @return string
  118. */
  119. public function getName()
  120. {
  121. return $this->_name;
  122. }
  123. /**
  124. * Set the default value of the parameter.
  125. *
  126. * Certain variables are difficult to expres
  127. *
  128. * @param null|bool|string|int|float|Zend_CodeGenerator_Php_Parameter_DefaultValue $defaultValue
  129. * @return Zend_CodeGenerator_Php_Parameter
  130. */
  131. public function setDefaultValue($defaultValue)
  132. {
  133. if($defaultValue === null) {
  134. $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("null");
  135. } else if(is_array($defaultValue)) {
  136. $defaultValue = str_replace(array("\r", "\n"), "", var_export($defaultValue, true));
  137. $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue($defaultValue);
  138. } else if(is_bool($defaultValue)) {
  139. if($defaultValue == true) {
  140. $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("true");
  141. } else {
  142. $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("false");
  143. }
  144. } else {
  145. $this->_defaultValue = $defaultValue;
  146. }
  147. return $this;
  148. }
  149. /**
  150. * getDefaultValue()
  151. *
  152. * @return string
  153. */
  154. public function getDefaultValue()
  155. {
  156. return $this->_defaultValue;
  157. }
  158. /**
  159. * setPosition()
  160. *
  161. * @param int $position
  162. * @return Zend_CodeGenerator_Php_Parameter
  163. */
  164. public function setPosition($position)
  165. {
  166. $this->_position = $position;
  167. return $this;
  168. }
  169. /**
  170. * getPosition()
  171. *
  172. * @return int
  173. */
  174. public function getPosition()
  175. {
  176. return $this->_position;
  177. }
  178. /**
  179. * @return bool
  180. */
  181. public function getPassedByReference()
  182. {
  183. return $this->_passedByReference;
  184. }
  185. /**
  186. * @param bool $passedByReference
  187. * @return Zend_CodeGenerator_Php_Parameter
  188. */
  189. public function setPassedByReference($passedByReference)
  190. {
  191. $this->_passedByReference = $passedByReference;
  192. return $this;
  193. }
  194. /**
  195. * generate()
  196. *
  197. * @return string
  198. */
  199. public function generate()
  200. {
  201. $output = '';
  202. if ($this->_type) {
  203. $output .= $this->_type . ' ';
  204. }
  205. if($this->_passedByReference === true) {
  206. $output .= '&';
  207. }
  208. $output .= '$' . $this->_name;
  209. if ($this->_defaultValue !== null) {
  210. $output .= ' = ';
  211. if (is_string($this->_defaultValue)) {
  212. $output .= '\'' . $this->_defaultValue . '\'';
  213. } else if($this->_defaultValue instanceof Zend_CodeGenerator_Php_Parameter_DefaultValue) {
  214. $output .= (string)$this->_defaultValue;
  215. } else {
  216. $output .= $this->_defaultValue;
  217. }
  218. }
  219. return $output;
  220. }
  221. }