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: Property.php 16971 2009-07-22 18:05:45Z mikaelkael $
24 * @see Zend_CodeGenerator_Php_Member_Abstract
26 require_once 'Zend/CodeGenerator/Php/Member/Abstract.php';
29 * @see Zend_CodeGenerator_Php_Property_DefaultValue
31 require_once 'Zend/CodeGenerator/Php/Property/DefaultValue.php';
35 * @package Zend_CodeGenerator
36 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license http://framework.zend.com/license/new-bsd New BSD License
39 class Zend_CodeGenerator_Php_Property
extends Zend_CodeGenerator_Php_Member_Abstract
45 protected $_isConst = null;
50 protected $_defaultValue = null;
55 * @param Zend_Reflection_Property $reflectionProperty
56 * @return Zend_CodeGenerator_Php_Property
58 public static function fromReflection(Zend_Reflection_Property
$reflectionProperty)
60 $property = new self();
62 $property->setName($reflectionProperty->getName());
64 $allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties();
66 $property->setDefaultValue($allDefaultProperties[$reflectionProperty->getName()]);
68 if ($reflectionProperty->getDocComment() != '') {
69 $property->setDocblock(Zend_CodeGenerator_Php_Docblock
::fromReflection($reflectionProperty->getDocComment()));
72 if ($reflectionProperty->isStatic()) {
73 $property->setStatic(true);
76 if ($reflectionProperty->isPrivate()) {
77 $property->setVisibility(self
::VISIBILITY_PRIVATE
);
78 } elseif ($reflectionProperty->isProtected()) {
79 $property->setVisibility(self
::VISIBILITY_PROTECTED
);
81 $property->setVisibility(self
::VISIBILITY_PUBLIC
);
84 $property->setSourceDirty(false);
93 * @return Zend_CodeGenerator_Php_Property
95 public function setConst($const)
97 $this->_isConst
= $const;
106 public function isConst()
108 return ($this->_isConst
) ?
true : false;
114 * @param Zend_CodeGenerator_Php_Property_DefaultValue|string|array $defaultValue
115 * @return Zend_CodeGenerator_Php_Property
117 public function setDefaultValue($defaultValue)
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);
126 if (!($defaultValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue
)) {
127 $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue(array('value' => $defaultValue));
130 $this->_defaultValue
= $defaultValue;
137 * @return Zend_CodeGenerator_Php_Property_DefaultValue
139 public function getDefaultValue()
141 return $this->_defaultValue
;
149 public function generate()
151 $name = $this->getName();
152 $defaultValue = $this->getDefaultValue();
156 if (($docblock = $this->getDocblock()) !== null) {
157 $docblock->setIndentation(' ');
158 $output .= $docblock->generate();
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.');
167 $output .= $this->_indentation
. 'const ' . $name . ' = '
168 . (($defaultValue !== null) ?
$defaultValue->generate() : 'null;');
170 $output .= $this->_indentation
171 . $this->getVisibility()
172 . (($this->isStatic()) ?
' static' : '')
173 . ' $' . $name . ' = '
174 . (($defaultValue !== null) ?
$defaultValue->generate() : 'null;');