/library/Zend/CodeGenerator/Php/Property/DefaultValue.php

https://github.com/eschabell/openshift-zendframework · PHP · 325 lines · 189 code · 30 blank · 106 comment · 17 complexity · c20b272e87949c0d94559b255e1bdf18 MD5 · raw file

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