|
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: Property.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_CodeGenerator_Php_Member_Abstract |
|
25 */ |
|
26 require_once 'Zend/CodeGenerator/Php/Member/Abstract.php'; |
|
27 |
|
28 /** |
|
29 * @see Zend_CodeGenerator_Php_Property_DefaultValue |
|
30 */ |
|
31 require_once 'Zend/CodeGenerator/Php/Property/DefaultValue.php'; |
|
32 |
|
33 /** |
|
34 * @category Zend |
|
35 * @package Zend_CodeGenerator |
|
36 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
37 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
38 */ |
|
39 class Zend_CodeGenerator_Php_Property extends Zend_CodeGenerator_Php_Member_Abstract |
|
40 { |
|
41 |
|
42 /** |
|
43 * @var bool |
|
44 */ |
|
45 protected $_isConst = null; |
|
46 |
|
47 /** |
|
48 * @var string |
|
49 */ |
|
50 protected $_defaultValue = null; |
|
51 |
|
52 /** |
|
53 * fromReflection() |
|
54 * |
|
55 * @param Zend_Reflection_Property $reflectionProperty |
|
56 * @return Zend_CodeGenerator_Php_Property |
|
57 */ |
|
58 public static function fromReflection(Zend_Reflection_Property $reflectionProperty) |
|
59 { |
|
60 $property = new self(); |
|
61 |
|
62 $property->setName($reflectionProperty->getName()); |
|
63 |
|
64 $allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties(); |
|
65 |
|
66 $property->setDefaultValue($allDefaultProperties[$reflectionProperty->getName()]); |
|
67 |
|
68 if ($reflectionProperty->getDocComment() != '') { |
|
69 $property->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionProperty->getDocComment())); |
|
70 } |
|
71 |
|
72 if ($reflectionProperty->isStatic()) { |
|
73 $property->setStatic(true); |
|
74 } |
|
75 |
|
76 if ($reflectionProperty->isPrivate()) { |
|
77 $property->setVisibility(self::VISIBILITY_PRIVATE); |
|
78 } elseif ($reflectionProperty->isProtected()) { |
|
79 $property->setVisibility(self::VISIBILITY_PROTECTED); |
|
80 } else { |
|
81 $property->setVisibility(self::VISIBILITY_PUBLIC); |
|
82 } |
|
83 |
|
84 $property->setSourceDirty(false); |
|
85 |
|
86 return $property; |
|
87 } |
|
88 |
|
89 /** |
|
90 * setConst() |
|
91 * |
|
92 * @param bool $const |
|
93 * @return Zend_CodeGenerator_Php_Property |
|
94 */ |
|
95 public function setConst($const) |
|
96 { |
|
97 $this->_isConst = $const; |
|
98 return $this; |
|
99 } |
|
100 |
|
101 /** |
|
102 * isConst() |
|
103 * |
|
104 * @return bool |
|
105 */ |
|
106 public function isConst() |
|
107 { |
|
108 return ($this->_isConst) ? true : false; |
|
109 } |
|
110 |
|
111 /** |
|
112 * setDefaultValue() |
|
113 * |
|
114 * @param Zend_CodeGenerator_Php_Property_DefaultValue|string|array $defaultValue |
|
115 * @return Zend_CodeGenerator_Php_Property |
|
116 */ |
|
117 public function setDefaultValue($defaultValue) |
|
118 { |
|
119 // if it looks like |
|
120 if (is_array($defaultValue) |
|
121 && array_key_exists('value', $defaultValue) |
|
122 && array_key_exists('type', $defaultValue)) { |
|
123 $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue($defaultValue); |
|
124 } |
|
125 |
|
126 if (!($defaultValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue)) { |
|
127 $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue(array('value' => $defaultValue)); |
|
128 } |
|
129 |
|
130 $this->_defaultValue = $defaultValue; |
|
131 return $this; |
|
132 } |
|
133 |
|
134 /** |
|
135 * getDefaultValue() |
|
136 * |
|
137 * @return Zend_CodeGenerator_Php_Property_DefaultValue |
|
138 */ |
|
139 public function getDefaultValue() |
|
140 { |
|
141 return $this->_defaultValue; |
|
142 } |
|
143 |
|
144 /** |
|
145 * generate() |
|
146 * |
|
147 * @return string |
|
148 */ |
|
149 public function generate() |
|
150 { |
|
151 $name = $this->getName(); |
|
152 $defaultValue = $this->getDefaultValue(); |
|
153 |
|
154 $output = ''; |
|
155 |
|
156 if (($docblock = $this->getDocblock()) !== null) { |
|
157 $docblock->setIndentation(' '); |
|
158 $output .= $docblock->generate(); |
|
159 } |
|
160 |
|
161 if ($this->isConst()) { |
|
162 if ($defaultValue != null && !$defaultValue->isValidConstantType()) { |
|
163 require_once 'Zend/CodeGenerator/Php/Exception.php'; |
|
164 throw new Zend_CodeGenerator_Php_Exception('The property ' . $this->_name . ' is said to be ' |
|
165 . 'constant but does not have a valid constant value.'); |
|
166 } |
|
167 $output .= $this->_indentation . 'const ' . $name . ' = ' |
|
168 . (($defaultValue !== null) ? $defaultValue->generate() : 'null;'); |
|
169 } else { |
|
170 $output .= $this->_indentation |
|
171 . $this->getVisibility() |
|
172 . (($this->isStatic()) ? ' static' : '') |
|
173 . ' $' . $name . ' = ' |
|
174 . (($defaultValue !== null) ? $defaultValue->generate() : 'null;'); |
|
175 } |
|
176 return $output; |
|
177 } |
|
178 |
|
179 } |