DefaultValue.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. $reflect = new ReflectionClass(get_class($this));
  75. self::$_constants = $reflect->getConstants();
  76. unset($reflect);
  77. }
  78. /**
  79. * isValidConstantType()
  80. *
  81. * @return bool
  82. */
  83. public function isValidConstantType()
  84. {
  85. if ($this->_type == self::TYPE_AUTO) {
  86. $type = $this->_getAutoDeterminedType($this->_value);
  87. }
  88. // valid types for constants
  89. $scalarTypes = array(
  90. self::TYPE_BOOLEAN,
  91. self::TYPE_BOOL,
  92. self::TYPE_NUMBER,
  93. self::TYPE_INTEGER,
  94. self::TYPE_INT,
  95. self::TYPE_FLOAT,
  96. self::TYPE_DOUBLE,
  97. self::TYPE_STRING,
  98. self::TYPE_CONSTANT,
  99. self::TYPE_NULL
  100. );
  101. return in_array($type, $scalarTypes);
  102. }
  103. /**
  104. * setValue()
  105. *
  106. * @param mixed $value
  107. * @return Zend_CodeGenerator_Php_Property_DefaultValue
  108. */
  109. public function setValue($value)
  110. {
  111. $this->_value = $value;
  112. return $this;
  113. }
  114. /**
  115. * getValue()
  116. *
  117. * @return mixed
  118. */
  119. public function getValue()
  120. {
  121. return $this->_value;
  122. }
  123. /**
  124. * setType()
  125. *
  126. * @param string $type
  127. * @return Zend_CodeGenerator_Php_Property_DefaultValue
  128. */
  129. public function setType($type)
  130. {
  131. $this->_type = $type;
  132. return $this;
  133. }
  134. /**
  135. * getType()
  136. *
  137. * @return string
  138. */
  139. public function getType()
  140. {
  141. return $this->_type;
  142. }
  143. /**
  144. * setArrayDepth()
  145. *
  146. * @param int $arrayDepth
  147. * @return Zend_CodeGenerator_Php_Property_DefaultValue
  148. */
  149. public function setArrayDepth($arrayDepth)
  150. {
  151. $this->_arrayDepth = $arrayDepth;
  152. return $this;
  153. }
  154. /**
  155. * getArrayDepth()
  156. *
  157. * @return int
  158. */
  159. public function getArrayDepth()
  160. {
  161. return $this->_arrayDepth;
  162. }
  163. /**
  164. * _getValidatedType()
  165. *
  166. * @param string $type
  167. * @return string
  168. */
  169. protected function _getValidatedType($type)
  170. {
  171. if (($constName = array_search($type, self::$_constants)) !== false) {
  172. return $type;
  173. }
  174. return self::TYPE_AUTO;
  175. }
  176. /**
  177. * _getAutoDeterminedType()
  178. *
  179. * @param mixed $value
  180. * @return string
  181. */
  182. public function _getAutoDeterminedType($value)
  183. {
  184. switch (gettype($value)) {
  185. case 'boolean':
  186. return self::TYPE_BOOLEAN;
  187. case 'integer':
  188. return self::TYPE_INT;
  189. case 'string':
  190. return self::TYPE_STRING;
  191. case 'double':
  192. case 'float':
  193. case 'integer':
  194. return self::TYPE_NUMBER;
  195. case 'array':
  196. return self::TYPE_ARRAY;
  197. case 'NULL':
  198. return self::TYPE_NULL;
  199. case 'object':
  200. case 'resource':
  201. case 'unknown type':
  202. default:
  203. return self::TYPE_OTHER;
  204. }
  205. return self::TYPE_OTHER;
  206. }
  207. /**
  208. * generate()
  209. *
  210. * @return string
  211. */
  212. public function generate()
  213. {
  214. $type = $this->_type;
  215. if ($type != self::TYPE_AUTO) {
  216. $type = $this->_getValidatedType($type);
  217. }
  218. $value = $this->_value;
  219. if ($type == self::TYPE_AUTO) {
  220. $type = $this->_getAutoDeterminedType($value);
  221. if ($type == self::TYPE_ARRAY) {
  222. $rii = new RecursiveIteratorIterator(
  223. $it = new RecursiveArrayIterator($value),
  224. RecursiveIteratorIterator::SELF_FIRST
  225. );
  226. foreach ($rii as $curKey => $curValue) {
  227. if (!$curValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue) {
  228. $curValue = new self(array('value' => $curValue));
  229. $rii->getSubIterator()->offsetSet($curKey, $curValue);
  230. }
  231. $curValue->setArrayDepth($rii->getDepth());
  232. }
  233. $value = $rii->getSubIterator()->getArrayCopy();
  234. }
  235. }
  236. $output = '';
  237. switch ($type) {
  238. case self::TYPE_STRING:
  239. $output .= "'" . $value . "'";
  240. break;
  241. case self::TYPE_NUMBER:
  242. case self::TYPE_INTEGER:
  243. case self::TYPE_INT:
  244. case self::TYPE_FLOAT:
  245. case self::TYPE_DOUBLE:
  246. case self::TYPE_NULL:
  247. case self::TYPE_CONSTANT:
  248. $output .= $value;
  249. break;
  250. case self::TYPE_ARRAY:
  251. $output .= 'array(';
  252. $curArrayMultiblock = false;
  253. if (count($value) > 1) {
  254. $curArrayMultiblock = true;
  255. $output .= PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1);
  256. }
  257. $outputParts = array();
  258. $noKeyIndex = 0;
  259. foreach ($value as $n => $v) {
  260. $v->setArrayDepth($this->_arrayDepth + 1);
  261. $partV = $v->generate();
  262. $partV = substr($partV, 0, strlen($partV)-1);
  263. if ($n === $noKeyIndex) {
  264. $outputParts[] = $partV;
  265. $noKeyIndex++;
  266. } else {
  267. $outputParts[] = (is_int($n) ? $n : "'" . $n . "'") . ' => ' . $partV;
  268. }
  269. }
  270. $output .= implode(',' . PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1), $outputParts);
  271. if ($curArrayMultiblock == true) {
  272. $output .= PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1);
  273. }
  274. $output .= ')';
  275. break;
  276. case self::TYPE_OTHER:
  277. default:
  278. throw new Exception('I dont know this type');
  279. }
  280. $output .= ';';
  281. return $output;
  282. }
  283. }