*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / CodeGenerator / Php / Property / DefaultValue.php
blobac4752aa2e036ee05788787415159ddd83c1f356
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_CodeGenerator
17 * @subpackage PHP
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 $
23 /**
24 * @see Zend_CodeGenerator_Php_Abstract
26 require_once 'Zend/CodeGenerator/Php/Abstract.php';
28 /**
29 * @category Zend
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
36 /**#@+
37 * Constant values
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';
52 /**#@-*/
54 /**
55 * @var array of reflected constants
57 protected static $_constants = array();
59 /**
60 * @var mixed
62 protected $_value = null;
64 /**
65 * @var string
67 protected $_type = self::TYPE_AUTO;
69 /**
70 * @var int
72 protected $_arrayDepth = 1;
74 /**
75 * _init()
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();
83 unset($reflect);
86 /**
87 * isValidConstantType()
89 * @return bool
91 public function isValidConstantType()
93 if ($this->_type == self::TYPE_AUTO) {
94 $type = $this->_getAutoDeterminedType($this->_value);
97 // valid types for constants
98 $scalarTypes = array(
99 self::TYPE_BOOLEAN,
100 self::TYPE_BOOL,
101 self::TYPE_NUMBER,
102 self::TYPE_INTEGER,
103 self::TYPE_INT,
104 self::TYPE_FLOAT,
105 self::TYPE_DOUBLE,
106 self::TYPE_STRING,
107 self::TYPE_CONSTANT,
108 self::TYPE_NULL
111 return in_array($type, $scalarTypes);
115 * setValue()
117 * @param mixed $value
118 * @return Zend_CodeGenerator_Php_Property_DefaultValue
120 public function setValue($value)
122 $this->_value = $value;
123 return $this;
127 * getValue()
129 * @return mixed
131 public function getValue()
133 return $this->_value;
137 * setType()
139 * @param string $type
140 * @return Zend_CodeGenerator_Php_Property_DefaultValue
142 public function setType($type)
144 $this->_type = $type;
145 return $this;
149 * getType()
151 * @return string
153 public function getType()
155 return $this->_type;
159 * setArrayDepth()
161 * @param int $arrayDepth
162 * @return Zend_CodeGenerator_Php_Property_DefaultValue
164 public function setArrayDepth($arrayDepth)
166 $this->_arrayDepth = $arrayDepth;
167 return $this;
171 * getArrayDepth()
173 * @return int
175 public function getArrayDepth()
177 return $this->_arrayDepth;
181 * _getValidatedType()
183 * @param string $type
184 * @return string
186 protected function _getValidatedType($type)
188 if (($constName = array_search($type, self::$_constants)) !== false) {
189 return $type;
192 return self::TYPE_AUTO;
196 * _getAutoDeterminedType()
198 * @param mixed $value
199 * @return string
201 public function _getAutoDeterminedType($value)
203 switch (gettype($value)) {
204 case 'boolean':
205 return self::TYPE_BOOLEAN;
206 case 'integer':
207 return self::TYPE_INT;
208 case 'string':
209 return self::TYPE_STRING;
210 case 'double':
211 case 'float':
212 case 'integer':
213 return self::TYPE_NUMBER;
214 case 'array':
215 return self::TYPE_ARRAY;
216 case 'NULL':
217 return self::TYPE_NULL;
218 case 'object':
219 case 'resource':
220 case 'unknown type':
221 default:
222 return self::TYPE_OTHER;
225 return self::TYPE_OTHER;
229 * generate()
231 * @return string
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();
263 $output = '';
265 switch ($type) {
266 case self::TYPE_STRING:
267 $output .= "'" . $value . "'";
268 break;
269 case self::TYPE_NUMBER:
270 case self::TYPE_INTEGER:
271 case self::TYPE_INT:
272 case self::TYPE_FLOAT:
273 case self::TYPE_DOUBLE:
274 case self::TYPE_NULL:
275 case self::TYPE_CONSTANT:
276 $output .= $value;
277 break;
278 case self::TYPE_ARRAY:
279 $output .= '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();
286 $noKeyIndex = 0;
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;
293 $noKeyIndex++;
294 } else {
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);
303 $output .= ')';
304 break;
305 case self::TYPE_OTHER:
306 default:
307 throw new Exception('I dont know this type');
310 $output .= ';';
312 return $output;