PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/CodeGenerator/Php/PhpValue.php

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