PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-code/src/Generator/ValueGenerator.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 435 lines | 279 code | 44 blank | 112 comment | 32 complexity | 0d67175ed415ee8b3a80aad6bc3a32d4 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Code\Generator;
  10. use Zend\Stdlib\ArrayObject;
  11. class ValueGenerator extends AbstractGenerator
  12. {
  13. /**#@+
  14. * Constant values
  15. */
  16. const TYPE_AUTO = 'auto';
  17. const TYPE_BOOLEAN = 'boolean';
  18. const TYPE_BOOL = 'bool';
  19. const TYPE_NUMBER = 'number';
  20. const TYPE_INTEGER = 'integer';
  21. const TYPE_INT = 'int';
  22. const TYPE_FLOAT = 'float';
  23. const TYPE_DOUBLE = 'double';
  24. const TYPE_STRING = 'string';
  25. const TYPE_ARRAY = 'array';
  26. const TYPE_CONSTANT = 'constant';
  27. const TYPE_NULL = 'null';
  28. const TYPE_OBJECT = 'object';
  29. const TYPE_OTHER = 'other';
  30. /**#@-*/
  31. const OUTPUT_MULTIPLE_LINE = 'multipleLine';
  32. const OUTPUT_SINGLE_LINE = 'singleLine';
  33. /**
  34. * @var mixed
  35. */
  36. protected $value = null;
  37. /**
  38. * @var string
  39. */
  40. protected $type = self::TYPE_AUTO;
  41. /**
  42. * @var int
  43. */
  44. protected $arrayDepth = 0;
  45. /**
  46. * @var string
  47. */
  48. protected $outputMode = self::OUTPUT_MULTIPLE_LINE;
  49. /**
  50. * @var array
  51. */
  52. protected $allowedTypes = null;
  53. /**
  54. * Autodetectable constants
  55. * @var ArrayObject
  56. */
  57. protected $constants = null;
  58. /**
  59. * @param mixed $value
  60. * @param string $type
  61. * @param string $outputMode
  62. * @param ArrayObject $constants
  63. */
  64. public function __construct($value = null, $type = self::TYPE_AUTO, $outputMode = self::OUTPUT_MULTIPLE_LINE, ArrayObject $constants = null)
  65. {
  66. // strict check is important here if $type = AUTO
  67. if ($value !== null) {
  68. $this->setValue($value);
  69. }
  70. if ($type !== self::TYPE_AUTO) {
  71. $this->setType($type);
  72. }
  73. if ($outputMode !== self::OUTPUT_MULTIPLE_LINE) {
  74. $this->setOutputMode($outputMode);
  75. }
  76. if ($constants !== null) {
  77. $this->constants = $constants;
  78. } else {
  79. $this->constants = new ArrayObject();
  80. }
  81. }
  82. /**
  83. * Init constant list by defined and magic constants
  84. */
  85. public function initEnvironmentConstants()
  86. {
  87. $constants = array(
  88. '__DIR__',
  89. '__FILE__',
  90. '__LINE__',
  91. '__CLASS__',
  92. '__TRAIT__',
  93. '__METHOD__',
  94. '__FUNCTION__',
  95. '__NAMESPACE__',
  96. '::'
  97. );
  98. $constants = array_merge($constants, array_keys(get_defined_constants()), $this->constants->getArrayCopy());
  99. $this->constants->exchangeArray($constants);
  100. }
  101. /**
  102. * Add constant to list
  103. *
  104. * @param string $constant
  105. *
  106. * @return $this
  107. */
  108. public function addConstant($constant)
  109. {
  110. $this->constants->append($constant);
  111. return $this;
  112. }
  113. /**
  114. * Delete constant from constant list
  115. *
  116. * @param string $constant
  117. *
  118. * @return bool
  119. */
  120. public function deleteConstant($constant)
  121. {
  122. if (($index = array_search($constant, $this->constants->getArrayCopy())) !== false) {
  123. $this->constants->offsetUnset($index);
  124. }
  125. return $index !== false;
  126. }
  127. /**
  128. * Return constant list
  129. *
  130. * @return ArrayObject
  131. */
  132. public function getConstants()
  133. {
  134. return $this->constants;
  135. }
  136. /**
  137. * @return bool
  138. */
  139. public function isValidConstantType()
  140. {
  141. if ($this->type == self::TYPE_AUTO) {
  142. $type = $this->getAutoDeterminedType($this->value);
  143. } else {
  144. $type = $this->type;
  145. }
  146. // valid types for constants
  147. $scalarTypes = array(
  148. self::TYPE_BOOLEAN,
  149. self::TYPE_BOOL,
  150. self::TYPE_NUMBER,
  151. self::TYPE_INTEGER,
  152. self::TYPE_INT,
  153. self::TYPE_FLOAT,
  154. self::TYPE_DOUBLE,
  155. self::TYPE_STRING,
  156. self::TYPE_CONSTANT,
  157. self::TYPE_NULL
  158. );
  159. return in_array($type, $scalarTypes);
  160. }
  161. /**
  162. * @param mixed $value
  163. * @return ValueGenerator
  164. */
  165. public function setValue($value)
  166. {
  167. $this->value = $value;
  168. return $this;
  169. }
  170. /**
  171. * @return mixed
  172. */
  173. public function getValue()
  174. {
  175. return $this->value;
  176. }
  177. /**
  178. * @param string $type
  179. * @return ValueGenerator
  180. */
  181. public function setType($type)
  182. {
  183. $this->type = (string) $type;
  184. return $this;
  185. }
  186. /**
  187. * @return string
  188. */
  189. public function getType()
  190. {
  191. return $this->type;
  192. }
  193. /**
  194. * @param int $arrayDepth
  195. * @return ValueGenerator
  196. */
  197. public function setArrayDepth($arrayDepth)
  198. {
  199. $this->arrayDepth = (int) $arrayDepth;
  200. return $this;
  201. }
  202. /**
  203. * @return int
  204. */
  205. public function getArrayDepth()
  206. {
  207. return $this->arrayDepth;
  208. }
  209. /**
  210. * @param string $type
  211. * @return string
  212. */
  213. protected function getValidatedType($type)
  214. {
  215. $types = array(
  216. self::TYPE_AUTO,
  217. self::TYPE_BOOLEAN,
  218. self::TYPE_BOOL,
  219. self::TYPE_NUMBER,
  220. self::TYPE_INTEGER,
  221. self::TYPE_INT,
  222. self::TYPE_FLOAT,
  223. self::TYPE_DOUBLE,
  224. self::TYPE_STRING,
  225. self::TYPE_ARRAY,
  226. self::TYPE_CONSTANT,
  227. self::TYPE_NULL,
  228. self::TYPE_OBJECT,
  229. self::TYPE_OTHER
  230. );
  231. if (in_array($type, $types)) {
  232. return $type;
  233. }
  234. return self::TYPE_AUTO;
  235. }
  236. /**
  237. * @param mixed $value
  238. * @return string
  239. */
  240. public function getAutoDeterminedType($value)
  241. {
  242. switch (gettype($value)) {
  243. case 'boolean':
  244. return self::TYPE_BOOLEAN;
  245. case 'string':
  246. foreach ($this->constants as $constant) {
  247. if (strpos($value, $constant) !== false) {
  248. return self::TYPE_CONSTANT;
  249. }
  250. }
  251. return self::TYPE_STRING;
  252. case 'double':
  253. case 'float':
  254. case 'integer':
  255. return self::TYPE_NUMBER;
  256. case 'array':
  257. return self::TYPE_ARRAY;
  258. case 'NULL':
  259. return self::TYPE_NULL;
  260. case 'object':
  261. case 'resource':
  262. case 'unknown type':
  263. default:
  264. return self::TYPE_OTHER;
  265. }
  266. }
  267. /**
  268. * @throws Exception\RuntimeException
  269. * @return string
  270. */
  271. public function generate()
  272. {
  273. $type = $this->type;
  274. if ($type != self::TYPE_AUTO) {
  275. $type = $this->getValidatedType($type);
  276. }
  277. $value = $this->value;
  278. if ($type == self::TYPE_AUTO) {
  279. $type = $this->getAutoDeterminedType($value);
  280. }
  281. if ($type == self::TYPE_ARRAY) {
  282. foreach ($value as &$curValue) {
  283. if ($curValue instanceof self) {
  284. continue;
  285. }
  286. $curValue = new self($curValue, self::TYPE_AUTO, self::OUTPUT_MULTIPLE_LINE, $this->getConstants());
  287. }
  288. }
  289. $output = '';
  290. switch ($type) {
  291. case self::TYPE_BOOLEAN:
  292. case self::TYPE_BOOL:
  293. $output .= ($value ? 'true' : 'false');
  294. break;
  295. case self::TYPE_STRING:
  296. $output .= self::escape($value);
  297. break;
  298. case self::TYPE_NULL:
  299. $output .= 'null';
  300. break;
  301. case self::TYPE_NUMBER:
  302. case self::TYPE_INTEGER:
  303. case self::TYPE_INT:
  304. case self::TYPE_FLOAT:
  305. case self::TYPE_DOUBLE:
  306. case self::TYPE_CONSTANT:
  307. $output .= $value;
  308. break;
  309. case self::TYPE_ARRAY:
  310. $output .= 'array(';
  311. if ($this->outputMode == self::OUTPUT_MULTIPLE_LINE) {
  312. $output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1);
  313. }
  314. $outputParts = array();
  315. $noKeyIndex = 0;
  316. foreach ($value as $n => $v) {
  317. /* @var $v ValueGenerator */
  318. $v->setArrayDepth($this->arrayDepth + 1);
  319. $partV = $v->generate();
  320. $short = false;
  321. if (is_int($n)) {
  322. if ($n === $noKeyIndex) {
  323. $short = true;
  324. $noKeyIndex++;
  325. } else {
  326. $noKeyIndex = max($n + 1, $noKeyIndex);
  327. }
  328. }
  329. if ($short) {
  330. $outputParts[] = $partV;
  331. } else {
  332. $outputParts[] = (is_int($n) ? $n : self::escape($n)) . ' => ' . $partV;
  333. }
  334. }
  335. $padding = ($this->outputMode == self::OUTPUT_MULTIPLE_LINE)
  336. ? self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1)
  337. : ' ';
  338. $output .= implode(',' . $padding, $outputParts);
  339. if ($this->outputMode == self::OUTPUT_MULTIPLE_LINE) {
  340. if (count($outputParts) > 0) {
  341. $output .= ',';
  342. }
  343. $output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth);
  344. }
  345. $output .= ')';
  346. break;
  347. case self::TYPE_OTHER:
  348. default:
  349. throw new Exception\RuntimeException(
  350. sprintf('Type "%s" is unknown or cannot be used as property default value.', get_class($value))
  351. );
  352. }
  353. return $output;
  354. }
  355. /**
  356. * Quotes value for PHP code.
  357. *
  358. * @param string $input Raw string.
  359. * @param bool $quote Whether add surrounding quotes or not.
  360. * @return string PHP-ready code.
  361. */
  362. public static function escape($input, $quote = true)
  363. {
  364. $output = addcslashes($input, "\\'");
  365. // adds quoting strings
  366. if ($quote) {
  367. $output = "'" . $output . "'";
  368. }
  369. return $output;
  370. }
  371. /**
  372. * @param string $outputMode
  373. * @return ValueGenerator
  374. */
  375. public function setOutputMode($outputMode)
  376. {
  377. $this->outputMode = (string) $outputMode;
  378. return $this;
  379. }
  380. /**
  381. * @return string
  382. */
  383. public function getOutputMode()
  384. {
  385. return $this->outputMode;
  386. }
  387. public function __toString()
  388. {
  389. return $this->generate();
  390. }
  391. }