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.
16 * @package Zend_CodeGenerator
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: DefaultValue.php 16971 2009-07-22 18:05:45Z mikaelkael $
24 * @see Zend_CodeGenerator_Php_Abstract
26 require_once 'Zend/CodeGenerator/Php/Abstract.php';
30 * @package Zend_CodeGenerator
31 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
34 class Zend_CodeGenerator_Php_Property_DefaultValue
extends Zend_CodeGenerator_Php_Abstract
39 const TYPE_AUTO
= 'auto';
40 const TYPE_BOOLEAN
= 'boolean';
41 const TYPE_BOOL
= 'bool';
42 const TYPE_NUMBER
= 'number';
43 const TYPE_INTEGER
= 'integer';
44 const TYPE_INT
= 'int';
45 const TYPE_FLOAT
= 'float';
46 const TYPE_DOUBLE
= 'double';
47 const TYPE_STRING
= 'string';
48 const TYPE_ARRAY
= 'array';
49 const TYPE_CONSTANT
= 'constant';
50 const TYPE_NULL
= 'null';
51 const TYPE_OTHER
= 'other';
55 * @var array of reflected constants
57 protected static $_constants = array();
62 protected $_value = null;
67 protected $_type = self
::TYPE_AUTO
;
72 protected $_arrayDepth = 1;
77 * This method will prepare the constant array for this class
79 protected function _init()
81 $reflect = new ReflectionClass(get_class($this));
82 self
::$_constants = $reflect->getConstants();
87 * isValidConstantType()
91 public function isValidConstantType()
93 if ($this->_type
== self
::TYPE_AUTO
) {
94 $type = $this->_getAutoDeterminedType($this->_value
);
97 // valid types for constants
111 return in_array($type, $scalarTypes);
117 * @param mixed $value
118 * @return Zend_CodeGenerator_Php_Property_DefaultValue
120 public function setValue($value)
122 $this->_value
= $value;
131 public function getValue()
133 return $this->_value
;
139 * @param string $type
140 * @return Zend_CodeGenerator_Php_Property_DefaultValue
142 public function setType($type)
144 $this->_type
= $type;
153 public function getType()
161 * @param int $arrayDepth
162 * @return Zend_CodeGenerator_Php_Property_DefaultValue
164 public function setArrayDepth($arrayDepth)
166 $this->_arrayDepth
= $arrayDepth;
175 public function getArrayDepth()
177 return $this->_arrayDepth
;
181 * _getValidatedType()
183 * @param string $type
186 protected function _getValidatedType($type)
188 if (($constName = array_search($type, self
::$_constants)) !== false) {
192 return self
::TYPE_AUTO
;
196 * _getAutoDeterminedType()
198 * @param mixed $value
201 public function _getAutoDeterminedType($value)
203 switch (gettype($value)) {
205 return self
::TYPE_BOOLEAN
;
207 return self
::TYPE_INT
;
209 return self
::TYPE_STRING
;
213 return self
::TYPE_NUMBER
;
215 return self
::TYPE_ARRAY
;
217 return self
::TYPE_NULL
;
222 return self
::TYPE_OTHER
;
225 return self
::TYPE_OTHER
;
233 public function generate()
235 $type = $this->_type
;
237 if ($type != self
::TYPE_AUTO
) {
238 $type = $this->_getValidatedType($type);
241 $value = $this->_value
;
243 if ($type == self
::TYPE_AUTO
) {
244 $type = $this->_getAutoDeterminedType($value);
246 if ($type == self
::TYPE_ARRAY
) {
247 $rii = new RecursiveIteratorIterator(
248 $it = new RecursiveArrayIterator($value),
249 RecursiveIteratorIterator
::SELF_FIRST
251 foreach ($rii as $curKey => $curValue) {
252 if (!$curValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue
) {
253 $curValue = new self(array('value' => $curValue));
254 $rii->getSubIterator()->offsetSet($curKey, $curValue);
256 $curValue->setArrayDepth($rii->getDepth());
258 $value = $rii->getSubIterator()->getArrayCopy();
266 case self
::TYPE_STRING
:
267 $output .= "'" . $value . "'";
269 case self
::TYPE_NUMBER
:
270 case self
::TYPE_INTEGER
:
272 case self
::TYPE_FLOAT
:
273 case self
::TYPE_DOUBLE
:
274 case self
::TYPE_NULL
:
275 case self
::TYPE_CONSTANT
:
278 case self
::TYPE_ARRAY
:
280 $curArrayMultiblock = false;
281 if (count($value) > 1) {
282 $curArrayMultiblock = true;
283 $output .= PHP_EOL
. str_repeat($this->_indentation
, $this->_arrayDepth+
1);
285 $outputParts = array();
287 foreach ($value as $n => $v) {
288 $v->setArrayDepth($this->_arrayDepth +
1);
289 $partV = $v->generate();
290 $partV = substr($partV, 0, strlen($partV)-1);
291 if ($n === $noKeyIndex) {
292 $outputParts[] = $partV;
295 $outputParts[] = (is_int($n) ?
$n : "'" . $n . "'") . ' => ' . $partV;
299 $output .= implode(',' . PHP_EOL
. str_repeat($this->_indentation
, $this->_arrayDepth+
1), $outputParts);
300 if ($curArrayMultiblock == true) {
301 $output .= PHP_EOL
. str_repeat($this->_indentation
, $this->_arrayDepth+
1);
305 case self
::TYPE_OTHER
:
307 throw new Exception('I dont know this type');