DefaultValue.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. * @category Zend
  28. * @package Zend_CodeGenerator
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_CodeGenerator_Php_Property_DefaultValue extends Zend_CodeGenerator_Php_Abstract
  33. {
  34. /**#@+
  35. * Constant values
  36. */
  37. const TYPE_AUTO = 'auto';
  38. const TYPE_BOOLEAN = 'boolean';
  39. const TYPE_BOOL = 'bool';
  40. const TYPE_NUMBER = 'number';
  41. const TYPE_INTEGER = 'integer';
  42. const TYPE_INT = 'int';
  43. const TYPE_FLOAT = 'float';
  44. const TYPE_DOUBLE = 'double';
  45. const TYPE_STRING = 'string';
  46. const TYPE_ARRAY = 'array';
  47. const TYPE_CONSTANT = 'constant';
  48. const TYPE_NULL = 'null';
  49. const TYPE_OTHER = 'other';
  50. /**#@-*/
  51. /**
  52. * @var array of reflected constants
  53. */
  54. protected static $_constants = array();
  55. /**
  56. * @var mixed
  57. */
  58. protected $_value = null;
  59. /**
  60. * @var string
  61. */
  62. protected $_type = self::TYPE_AUTO;
  63. /**
  64. * @var int
  65. */
  66. protected $_arrayDepth = 1;
  67. /**
  68. * _init()
  69. *
  70. * This method will prepare the constant array for this class
  71. */
  72. protected function _init()
  73. {
  74. if(count(self::$_constants) == 0) {
  75. $reflect = new ReflectionClass(get_class($this));
  76. self::$_constants = $reflect->getConstants();
  77. unset($reflect);
  78. }
  79. }
  80. /**
  81. * isValidConstantType()
  82. *
  83. * @return bool
  84. */
  85. public function isValidConstantType()
  86. {
  87. if ($this->_type == self::TYPE_AUTO) {
  88. $type = $this->_getAutoDeterminedType($this->_value);
  89. }
  90. // valid types for constants
  91. $scalarTypes = array(
  92. self::TYPE_BOOLEAN,
  93. self::TYPE_BOOL,
  94. self::TYPE_NUMBER,
  95. self::TYPE_INTEGER,
  96. self::TYPE_INT,
  97. self::TYPE_FLOAT,
  98. self::TYPE_DOUBLE,
  99. self::TYPE_STRING,
  100. self::TYPE_CONSTANT,
  101. self::TYPE_NULL
  102. );
  103. return in_array($type, $scalarTypes);
  104. }
  105. /**
  106. * setValue()
  107. *
  108. * @param mixed $value
  109. * @return Zend_CodeGenerator_Php_Property_DefaultValue
  110. */
  111. public function setValue($value)
  112. {
  113. $this->_value = $value;
  114. return $this;
  115. }
  116. /**
  117. * getValue()
  118. *
  119. * @return mixed
  120. */
  121. public function getValue()
  122. {
  123. return $this->_value;
  124. }
  125. /**
  126. * setType()
  127. *
  128. * @param string $type
  129. * @return Zend_CodeGenerator_Php_Property_DefaultValue
  130. */
  131. public function setType($type)
  132. {
  133. $this->_type = $type;
  134. return $this;
  135. }
  136. /**
  137. * getType()
  138. *
  139. * @return string
  140. */
  141. public function getType()
  142. {
  143. return $this->_type;
  144. }
  145. /**
  146. * setArrayDepth()
  147. *
  148. * @param int $arrayDepth
  149. * @return Zend_CodeGenerator_Php_Property_DefaultValue
  150. */
  151. public function setArrayDepth($arrayDepth)
  152. {
  153. $this->_arrayDepth = $arrayDepth;
  154. return $this;
  155. }
  156. /**
  157. * getArrayDepth()
  158. *
  159. * @return int
  160. */
  161. public function getArrayDepth()
  162. {
  163. return $this->_arrayDepth;
  164. }
  165. /**
  166. * _getValidatedType()
  167. *
  168. * @param string $type
  169. * @return string
  170. */
  171. protected function _getValidatedType($type)
  172. {
  173. if (($constName = array_search($type, self::$_constants)) !== false) {
  174. return $type;
  175. }
  176. return self::TYPE_AUTO;
  177. }
  178. /**
  179. * _getAutoDeterminedType()
  180. *
  181. * @param mixed $value
  182. * @return string
  183. */
  184. public function _getAutoDeterminedType($value)
  185. {
  186. switch (gettype($value)) {
  187. case 'boolean':
  188. return self::TYPE_BOOLEAN;
  189. case 'integer':
  190. return self::TYPE_INT;
  191. case 'string':
  192. return self::TYPE_STRING;
  193. case 'double':
  194. case 'float':
  195. case 'integer':
  196. return self::TYPE_NUMBER;
  197. case 'array':
  198. return self::TYPE_ARRAY;
  199. case 'NULL':
  200. return self::TYPE_NULL;
  201. case 'object':
  202. case 'resource':
  203. case 'unknown type':
  204. default:
  205. return self::TYPE_OTHER;
  206. }
  207. }
  208. /**
  209. * generate()
  210. *
  211. * @return string
  212. */
  213. public function generate()
  214. {
  215. $type = $this->_type;
  216. if ($type != self::TYPE_AUTO) {
  217. $type = $this->_getValidatedType($type);
  218. }
  219. $value = $this->_value;
  220. if ($type == self::TYPE_AUTO) {
  221. $type = $this->_getAutoDeterminedType($value);
  222. if ($type == self::TYPE_ARRAY) {
  223. $rii = new RecursiveIteratorIterator(
  224. $it = new RecursiveArrayIterator($value),
  225. RecursiveIteratorIterator::SELF_FIRST
  226. );
  227. foreach ($rii as $curKey => $curValue) {
  228. if (!$curValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue) {
  229. $curValue = new self(array('value' => $curValue));
  230. $rii->getSubIterator()->offsetSet($curKey, $curValue);
  231. }
  232. $curValue->setArrayDepth($rii->getDepth());
  233. }
  234. $value = $rii->getSubIterator()->getArrayCopy();
  235. }
  236. }
  237. $output = '';
  238. switch ($type) {
  239. case self::TYPE_BOOLEAN:
  240. case self::TYPE_BOOL:
  241. $output .= ( $value ? 'true' : 'false' );
  242. break;
  243. case self::TYPE_STRING:
  244. $output .= "'" . addcslashes($value, "'") . "'";
  245. break;
  246. case self::TYPE_NULL:
  247. $output .= 'null';
  248. break;
  249. case self::TYPE_NUMBER:
  250. case self::TYPE_INTEGER:
  251. case self::TYPE_INT:
  252. case self::TYPE_FLOAT:
  253. case self::TYPE_DOUBLE:
  254. case self::TYPE_CONSTANT:
  255. $output .= $value;
  256. break;
  257. case self::TYPE_ARRAY:
  258. $output .= 'array(';
  259. $curArrayMultiblock = false;
  260. if (count($value) > 1) {
  261. $curArrayMultiblock = true;
  262. $output .= PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1);
  263. }
  264. $outputParts = array();
  265. $noKeyIndex = 0;
  266. foreach ($value as $n => $v) {
  267. $v->setArrayDepth($this->_arrayDepth + 1);
  268. $partV = $v->generate();
  269. $partV = substr($partV, 0, strlen($partV)-1);
  270. if ($n === $noKeyIndex) {
  271. $outputParts[] = $partV;
  272. $noKeyIndex++;
  273. } else {
  274. $outputParts[] = (is_int($n) ? $n : "'" . addcslashes($n, "'") . "'") . ' => ' . $partV;
  275. }
  276. }
  277. $output .= implode(',' . PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1), $outputParts);
  278. if ($curArrayMultiblock == true) {
  279. $output .= PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1);
  280. }
  281. $output .= ')';
  282. break;
  283. case self::TYPE_OTHER:
  284. default:
  285. require_once "Zend/CodeGenerator/Php/Exception.php";
  286. throw new Zend_CodeGenerator_Php_Exception(
  287. "Type '".get_class($value)."' is unknown or cannot be used as property default value."
  288. );
  289. }
  290. $output .= ';';
  291. return $output;
  292. }
  293. }