Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Code / Generator / ValueGenerator.php
blob6150da8b6b8aa9a5dc893732f35d60b17ebaa00f
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Code\Generator;
12 use Zend\Stdlib\ArrayObject;
14 class ValueGenerator extends AbstractGenerator
16 /**#@+
17 * Constant values
19 const TYPE_AUTO = 'auto';
20 const TYPE_BOOLEAN = 'boolean';
21 const TYPE_BOOL = 'bool';
22 const TYPE_NUMBER = 'number';
23 const TYPE_INTEGER = 'integer';
24 const TYPE_INT = 'int';
25 const TYPE_FLOAT = 'float';
26 const TYPE_DOUBLE = 'double';
27 const TYPE_STRING = 'string';
28 const TYPE_ARRAY = 'array';
29 const TYPE_CONSTANT = 'constant';
30 const TYPE_NULL = 'null';
31 const TYPE_OBJECT = 'object';
32 const TYPE_OTHER = 'other';
33 /**#@-*/
35 const OUTPUT_MULTIPLE_LINE = 'multipleLine';
36 const OUTPUT_SINGLE_LINE = 'singleLine';
38 /**
39 * @var mixed
41 protected $value = null;
43 /**
44 * @var string
46 protected $type = self::TYPE_AUTO;
48 /**
49 * @var int
51 protected $arrayDepth = 1;
53 /**
54 * @var string
56 protected $outputMode = self::OUTPUT_MULTIPLE_LINE;
58 /**
59 * @var array
61 protected $allowedTypes = null;
62 /**
63 * Autodetectable constants
64 * @var ArrayObject
66 protected $constants = null;
68 /**
69 * @param mixed $value
70 * @param string $type
71 * @param string $outputMode
72 * @param ArrayObject $constants
74 public function __construct($value = null, $type = self::TYPE_AUTO, $outputMode = self::OUTPUT_MULTIPLE_LINE, ArrayObject $constants = null)
76 if ($value !== null) { // strict check is important here if $type = AUTO
77 $this->setValue($value);
79 if ($type !== self::TYPE_AUTO) {
80 $this->setType($type);
82 if ($outputMode !== self::OUTPUT_MULTIPLE_LINE) {
83 $this->setOutputMode($outputMode);
85 if ($constants !== null) {
86 $this->constants = $constants;
87 } else {
88 $this->constants = new ArrayObject();
93 /**
94 * Init constant list by defined and magic constants
96 public function initEnvironmentConstants()
98 $constants = array(
99 '__DIR__',
100 '__FILE__',
101 '__LINE__',
102 '__CLASS__',
103 '__TRAIT__',
104 '__METHOD__',
105 '__FUNCTION__',
106 '__NAMESPACE__',
107 '::'
109 $constants = array_merge($constants, array_keys(get_defined_constants()), $this->constants->getArrayCopy());
110 $this->constants->exchangeArray($constants);
114 * Add constant to list
116 * @param string $constant
118 * @return $this
120 public function addConstant($constant)
122 $this->constants->append($constant);
124 return $this;
128 * Delete constant from constant list
130 * @param string $constant
132 * @return bool
134 public function deleteConstant($constant)
136 if (($index = array_search($constant, $this->constants->getArrayCopy())) !== false) {
137 $this->constants->offsetUnset($index);
140 return $index !== false;
144 * Return constant list
146 * @return ArrayObject
148 public function getConstants()
150 return $this->constants;
154 * @return bool
156 public function isValidConstantType()
158 if ($this->type == self::TYPE_AUTO) {
159 $type = $this->getAutoDeterminedType($this->value);
160 } else {
161 $type = $this->type;
164 // valid types for constants
165 $scalarTypes = array(
166 self::TYPE_BOOLEAN,
167 self::TYPE_BOOL,
168 self::TYPE_NUMBER,
169 self::TYPE_INTEGER,
170 self::TYPE_INT,
171 self::TYPE_FLOAT,
172 self::TYPE_DOUBLE,
173 self::TYPE_STRING,
174 self::TYPE_CONSTANT,
175 self::TYPE_NULL
178 return in_array($type, $scalarTypes);
182 * @param mixed $value
183 * @return ValueGenerator
185 public function setValue($value)
187 $this->value = $value;
188 return $this;
192 * @return mixed
194 public function getValue()
196 return $this->value;
200 * @param string $type
201 * @return ValueGenerator
203 public function setType($type)
205 $this->type = (string) $type;
206 return $this;
210 * @return string
212 public function getType()
214 return $this->type;
218 * @param int $arrayDepth
219 * @return ValueGenerator
221 public function setArrayDepth($arrayDepth)
223 $this->arrayDepth = (int) $arrayDepth;
224 return $this;
228 * @return int
230 public function getArrayDepth()
232 return $this->arrayDepth;
236 * @param string $type
237 * @return string
239 protected function getValidatedType($type)
241 $types = array(
242 self::TYPE_AUTO,
243 self::TYPE_BOOLEAN,
244 self::TYPE_BOOL,
245 self::TYPE_NUMBER,
246 self::TYPE_INTEGER,
247 self::TYPE_INT,
248 self::TYPE_FLOAT,
249 self::TYPE_DOUBLE,
250 self::TYPE_STRING,
251 self::TYPE_ARRAY,
252 self::TYPE_CONSTANT,
253 self::TYPE_NULL,
254 self::TYPE_OBJECT,
255 self::TYPE_OTHER
258 if (in_array($type, $types)) {
259 return $type;
262 return self::TYPE_AUTO;
266 * @param mixed $value
267 * @return string
269 public function getAutoDeterminedType($value)
271 switch (gettype($value)) {
272 case 'boolean':
273 return self::TYPE_BOOLEAN;
274 case 'string':
275 foreach ($this->constants as $constant) {
276 if (strpos($value, $constant) !== false) {
277 return self::TYPE_CONSTANT;
280 return self::TYPE_STRING;
281 case 'double':
282 case 'float':
283 case 'integer':
284 return self::TYPE_NUMBER;
285 case 'array':
286 return self::TYPE_ARRAY;
287 case 'NULL':
288 return self::TYPE_NULL;
289 case 'object':
290 case 'resource':
291 case 'unknown type':
292 default:
293 return self::TYPE_OTHER;
298 * @throws Exception\RuntimeException
299 * @return string
301 public function generate()
303 $type = $this->type;
305 if ($type != self::TYPE_AUTO) {
306 $type = $this->getValidatedType($type);
309 $value = $this->value;
311 if ($type == self::TYPE_AUTO) {
312 $type = $this->getAutoDeterminedType($value);
314 if ($type == self::TYPE_ARRAY) {
315 $rii = new \RecursiveIteratorIterator(
316 $it = new \RecursiveArrayIterator($value),
317 \RecursiveIteratorIterator::SELF_FIRST
319 foreach ($rii as $curKey => $curValue) {
320 if (!$curValue instanceof ValueGenerator) {
321 $curValue = new self($curValue, self::TYPE_AUTO, self::OUTPUT_MULTIPLE_LINE, $this->getConstants());
322 $rii->getSubIterator()->offsetSet($curKey, $curValue);
324 $curValue->setArrayDepth($rii->getDepth());
326 $value = $rii->getSubIterator()->getArrayCopy();
331 $output = '';
333 switch ($type) {
334 case self::TYPE_BOOLEAN:
335 case self::TYPE_BOOL:
336 $output .= ($value ? 'true' : 'false');
337 break;
338 case self::TYPE_STRING:
339 $output .= self::escape($value);
340 break;
341 case self::TYPE_NULL:
342 $output .= 'null';
343 break;
344 case self::TYPE_NUMBER:
345 case self::TYPE_INTEGER:
346 case self::TYPE_INT:
347 case self::TYPE_FLOAT:
348 case self::TYPE_DOUBLE:
349 case self::TYPE_CONSTANT:
350 $output .= $value;
351 break;
352 case self::TYPE_ARRAY:
353 $output .= 'array(';
354 $curArrayMultiblock = false;
355 if (count($value) > 1) {
356 $curArrayMultiblock = true;
357 if ($this->outputMode == self::OUTPUT_MULTIPLE_LINE) {
358 $output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1);
361 $outputParts = array();
362 $noKeyIndex = 0;
363 foreach ($value as $n => $v) {
364 /* @var $v ValueGenerator */
365 $v->setArrayDepth($this->arrayDepth + 1);
366 $partV = $v->generate();
367 $short = false;
368 if (is_int($n)) {
369 if ($n === $noKeyIndex) {
370 $short = true;
371 $noKeyIndex++;
372 } else {
373 $noKeyIndex = max($n + 1, $noKeyIndex);
377 if ($short) {
378 $outputParts[] = $partV;
379 } else {
380 $outputParts[] = (is_int($n) ? $n : self::escape($n)) . ' => ' . $partV;
383 $padding = ($this->outputMode == self::OUTPUT_MULTIPLE_LINE)
384 ? self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1)
385 : ' ';
386 $output .= implode(',' . $padding, $outputParts);
387 if ($curArrayMultiblock == true && $this->outputMode == self::OUTPUT_MULTIPLE_LINE) {
388 $output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1);
390 $output .= ')';
391 break;
392 case self::TYPE_OTHER:
393 default:
394 throw new Exception\RuntimeException(sprintf(
395 'Type "%s" is unknown or cannot be used as property default value.',
396 get_class($value)
400 return $output;
404 * Quotes value for PHP code.
406 * @param string $input Raw string.
407 * @param bool $quote Whether add surrounding quotes or not.
408 * @return string PHP-ready code.
410 public static function escape($input, $quote = true)
412 $output = addcslashes($input, "'");
414 // adds quoting strings
415 if ($quote) {
416 $output = "'" . $output . "'";
419 return $output;
423 * @param string $outputMode
424 * @return ValueGenerator
426 public function setOutputMode($outputMode)
428 $this->outputMode = (string) $outputMode;
429 return $this;
433 * @return string
435 public function getOutputMode()
437 return $this->outputMode;
440 public function __toString()
442 return $this->generate();