*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / CodeGenerator / Php / Property.php
blob80f5e5597012c364d32f3e4c859312446d405b63
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: Property.php 16971 2009-07-22 18:05:45Z mikaelkael $
23 /**
24 * @see Zend_CodeGenerator_Php_Member_Abstract
26 require_once 'Zend/CodeGenerator/Php/Member/Abstract.php';
28 /**
29 * @see Zend_CodeGenerator_Php_Property_DefaultValue
31 require_once 'Zend/CodeGenerator/Php/Property/DefaultValue.php';
33 /**
34 * @category Zend
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
42 /**
43 * @var bool
45 protected $_isConst = null;
47 /**
48 * @var string
50 protected $_defaultValue = null;
52 /**
53 * fromReflection()
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);
80 } else {
81 $property->setVisibility(self::VISIBILITY_PUBLIC);
84 $property->setSourceDirty(false);
86 return $property;
89 /**
90 * setConst()
92 * @param bool $const
93 * @return Zend_CodeGenerator_Php_Property
95 public function setConst($const)
97 $this->_isConst = $const;
98 return $this;
102 * isConst()
104 * @return bool
106 public function isConst()
108 return ($this->_isConst) ? true : false;
112 * setDefaultValue()
114 * @param Zend_CodeGenerator_Php_Property_DefaultValue|string|array $defaultValue
115 * @return Zend_CodeGenerator_Php_Property
117 public function setDefaultValue($defaultValue)
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);
126 if (!($defaultValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue)) {
127 $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue(array('value' => $defaultValue));
130 $this->_defaultValue = $defaultValue;
131 return $this;
135 * getDefaultValue()
137 * @return Zend_CodeGenerator_Php_Property_DefaultValue
139 public function getDefaultValue()
141 return $this->_defaultValue;
145 * generate()
147 * @return string
149 public function generate()
151 $name = $this->getName();
152 $defaultValue = $this->getDefaultValue();
154 $output = '';
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;');
169 } else {
170 $output .= $this->_indentation
171 . $this->getVisibility()
172 . (($this->isStatic()) ? ' static' : '')
173 . ' $' . $name . ' = '
174 . (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
176 return $output;