|
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: DefaultValue.php 21979 2010-04-24 11:07:11Z jan $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_CodeGenerator_Php_Abstract |
|
25 */ |
|
26 require_once 'Zend/CodeGenerator/Php/Abstract.php'; |
|
27 |
|
28 /** |
|
29 * @category Zend |
|
30 * @package Zend_CodeGenerator |
|
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
32 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
33 */ |
|
34 class Zend_CodeGenerator_Php_Property_DefaultValue extends Zend_CodeGenerator_Php_Abstract |
|
35 { |
|
36 /**#@+ |
|
37 * Constant values |
|
38 */ |
|
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 /**#@-*/ |
|
53 |
|
54 /** |
|
55 * @var array of reflected constants |
|
56 */ |
|
57 protected static $_constants = array(); |
|
58 |
|
59 /** |
|
60 * @var mixed |
|
61 */ |
|
62 protected $_value = null; |
|
63 |
|
64 /** |
|
65 * @var string |
|
66 */ |
|
67 protected $_type = self::TYPE_AUTO; |
|
68 |
|
69 /** |
|
70 * @var int |
|
71 */ |
|
72 protected $_arrayDepth = 1; |
|
73 |
|
74 /** |
|
75 * _init() |
|
76 * |
|
77 * This method will prepare the constant array for this class |
|
78 */ |
|
79 protected function _init() |
|
80 { |
|
81 if(count(self::$_constants) == 0) { |
|
82 $reflect = new ReflectionClass(get_class($this)); |
|
83 self::$_constants = $reflect->getConstants(); |
|
84 unset($reflect); |
|
85 } |
|
86 } |
|
87 |
|
88 /** |
|
89 * isValidConstantType() |
|
90 * |
|
91 * @return bool |
|
92 */ |
|
93 public function isValidConstantType() |
|
94 { |
|
95 if ($this->_type == self::TYPE_AUTO) { |
|
96 $type = $this->_getAutoDeterminedType($this->_value); |
|
97 } else { |
|
98 $type = $this->_type; |
|
99 } |
|
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 |
|
115 return in_array($type, $scalarTypes); |
|
116 } |
|
117 |
|
118 /** |
|
119 * setValue() |
|
120 * |
|
121 * @param mixed $value |
|
122 * @return Zend_CodeGenerator_Php_Property_DefaultValue |
|
123 */ |
|
124 public function setValue($value) |
|
125 { |
|
126 $this->_value = $value; |
|
127 return $this; |
|
128 } |
|
129 |
|
130 /** |
|
131 * getValue() |
|
132 * |
|
133 * @return mixed |
|
134 */ |
|
135 public function getValue() |
|
136 { |
|
137 return $this->_value; |
|
138 } |
|
139 |
|
140 /** |
|
141 * setType() |
|
142 * |
|
143 * @param string $type |
|
144 * @return Zend_CodeGenerator_Php_Property_DefaultValue |
|
145 */ |
|
146 public function setType($type) |
|
147 { |
|
148 $this->_type = $type; |
|
149 return $this; |
|
150 } |
|
151 |
|
152 /** |
|
153 * getType() |
|
154 * |
|
155 * @return string |
|
156 */ |
|
157 public function getType() |
|
158 { |
|
159 return $this->_type; |
|
160 } |
|
161 |
|
162 /** |
|
163 * setArrayDepth() |
|
164 * |
|
165 * @param int $arrayDepth |
|
166 * @return Zend_CodeGenerator_Php_Property_DefaultValue |
|
167 */ |
|
168 public function setArrayDepth($arrayDepth) |
|
169 { |
|
170 $this->_arrayDepth = $arrayDepth; |
|
171 return $this; |
|
172 } |
|
173 |
|
174 /** |
|
175 * getArrayDepth() |
|
176 * |
|
177 * @return int |
|
178 */ |
|
179 public function getArrayDepth() |
|
180 { |
|
181 return $this->_arrayDepth; |
|
182 } |
|
183 |
|
184 /** |
|
185 * _getValidatedType() |
|
186 * |
|
187 * @param string $type |
|
188 * @return string |
|
189 */ |
|
190 protected function _getValidatedType($type) |
|
191 { |
|
192 if (($constName = array_search($type, self::$_constants)) !== false) { |
|
193 return $type; |
|
194 } |
|
195 |
|
196 return self::TYPE_AUTO; |
|
197 } |
|
198 |
|
199 /** |
|
200 * _getAutoDeterminedType() |
|
201 * |
|
202 * @param mixed $value |
|
203 * @return string |
|
204 */ |
|
205 public function _getAutoDeterminedType($value) |
|
206 { |
|
207 switch (gettype($value)) { |
|
208 case 'boolean': |
|
209 return self::TYPE_BOOLEAN; |
|
210 case 'integer': |
|
211 return self::TYPE_INT; |
|
212 case 'string': |
|
213 return self::TYPE_STRING; |
|
214 case 'double': |
|
215 case 'float': |
|
216 case 'integer': |
|
217 return self::TYPE_NUMBER; |
|
218 case 'array': |
|
219 return self::TYPE_ARRAY; |
|
220 case 'NULL': |
|
221 return self::TYPE_NULL; |
|
222 case 'object': |
|
223 case 'resource': |
|
224 case 'unknown type': |
|
225 default: |
|
226 return self::TYPE_OTHER; |
|
227 } |
|
228 } |
|
229 |
|
230 /** |
|
231 * generate() |
|
232 * |
|
233 * @return string |
|
234 */ |
|
235 public function generate() |
|
236 { |
|
237 $type = $this->_type; |
|
238 |
|
239 if ($type != self::TYPE_AUTO) { |
|
240 $type = $this->_getValidatedType($type); |
|
241 } |
|
242 |
|
243 $value = $this->_value; |
|
244 |
|
245 if ($type == self::TYPE_AUTO) { |
|
246 $type = $this->_getAutoDeterminedType($value); |
|
247 |
|
248 if ($type == self::TYPE_ARRAY) { |
|
249 $rii = new RecursiveIteratorIterator( |
|
250 $it = new RecursiveArrayIterator($value), |
|
251 RecursiveIteratorIterator::SELF_FIRST |
|
252 ); |
|
253 foreach ($rii as $curKey => $curValue) { |
|
254 if (!$curValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue) { |
|
255 $curValue = new self(array('value' => $curValue)); |
|
256 $rii->getSubIterator()->offsetSet($curKey, $curValue); |
|
257 } |
|
258 $curValue->setArrayDepth($rii->getDepth()); |
|
259 } |
|
260 $value = $rii->getSubIterator()->getArrayCopy(); |
|
261 } |
|
262 |
|
263 } |
|
264 |
|
265 $output = ''; |
|
266 |
|
267 switch ($type) { |
|
268 case self::TYPE_BOOLEAN: |
|
269 case self::TYPE_BOOL: |
|
270 $output .= ( $value ? 'true' : 'false' ); |
|
271 break; |
|
272 case self::TYPE_STRING: |
|
273 $output .= "'" . addcslashes($value, "'") . "'"; |
|
274 break; |
|
275 case self::TYPE_NULL: |
|
276 $output .= 'null'; |
|
277 break; |
|
278 case self::TYPE_NUMBER: |
|
279 case self::TYPE_INTEGER: |
|
280 case self::TYPE_INT: |
|
281 case self::TYPE_FLOAT: |
|
282 case self::TYPE_DOUBLE: |
|
283 case self::TYPE_CONSTANT: |
|
284 $output .= $value; |
|
285 break; |
|
286 case self::TYPE_ARRAY: |
|
287 $output .= 'array('; |
|
288 $curArrayMultiblock = false; |
|
289 if (count($value) > 1) { |
|
290 $curArrayMultiblock = true; |
|
291 $output .= PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1); |
|
292 } |
|
293 $outputParts = array(); |
|
294 $noKeyIndex = 0; |
|
295 foreach ($value as $n => $v) { |
|
296 $v->setArrayDepth($this->_arrayDepth + 1); |
|
297 $partV = $v->generate(); |
|
298 $partV = substr($partV, 0, strlen($partV)-1); |
|
299 if ($n === $noKeyIndex) { |
|
300 $outputParts[] = $partV; |
|
301 $noKeyIndex++; |
|
302 } else { |
|
303 $outputParts[] = (is_int($n) ? $n : "'" . addcslashes($n, "'") . "'") . ' => ' . $partV; |
|
304 } |
|
305 |
|
306 } |
|
307 $output .= implode(',' . PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1), $outputParts); |
|
308 if ($curArrayMultiblock == true) { |
|
309 $output .= PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1); |
|
310 } |
|
311 $output .= ')'; |
|
312 break; |
|
313 case self::TYPE_OTHER: |
|
314 default: |
|
315 require_once "Zend/CodeGenerator/Php/Exception.php"; |
|
316 throw new Zend_CodeGenerator_Php_Exception( |
|
317 "Type '".get_class($value)."' is unknown or cannot be used as property default value." |
|
318 ); |
|
319 } |
|
320 |
|
321 $output .= ';'; |
|
322 |
|
323 return $output; |
|
324 } |
|
325 } |