File indexing completed on 2024-06-16 05:29:55

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_CodeGenerator
0017  * @subpackage PHP
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_CodeGenerator_Php_Member_Abstract
0025  */
0026 // require_once 'Zend/CodeGenerator/Php/Member/Abstract.php';
0027 
0028 /**
0029  * @see Zend_CodeGenerator_Php_Property_DefaultValue
0030  */
0031 // require_once 'Zend/CodeGenerator/Php/Property/DefaultValue.php';
0032 
0033 /**
0034  * @category   Zend
0035  * @package    Zend_CodeGenerator
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_CodeGenerator_Php_Property extends Zend_CodeGenerator_Php_Member_Abstract
0040 {
0041 
0042     /**
0043      * @var bool
0044      */
0045     protected $_isConst = null;
0046 
0047     /**
0048      * @var string
0049      */
0050     protected $_defaultValue = null;
0051 
0052     /**
0053      * fromReflection()
0054      *
0055      * @param Zend_Reflection_Property $reflectionProperty
0056      * @return Zend_CodeGenerator_Php_Property
0057      */
0058     public static function fromReflection(Zend_Reflection_Property $reflectionProperty)
0059     {
0060         $property = new self();
0061 
0062         $property->setName($reflectionProperty->getName());
0063 
0064         $allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties();
0065 
0066         $property->setDefaultValue($allDefaultProperties[$reflectionProperty->getName()]);
0067 
0068         if ($reflectionProperty->getDocComment() != '') {
0069             $property->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionProperty->getDocComment()));
0070         }
0071 
0072         if ($reflectionProperty->isStatic()) {
0073             $property->setStatic(true);
0074         }
0075 
0076         if ($reflectionProperty->isPrivate()) {
0077             $property->setVisibility(self::VISIBILITY_PRIVATE);
0078         } elseif ($reflectionProperty->isProtected()) {
0079             $property->setVisibility(self::VISIBILITY_PROTECTED);
0080         } else {
0081             $property->setVisibility(self::VISIBILITY_PUBLIC);
0082         }
0083 
0084         $property->setSourceDirty(false);
0085 
0086         return $property;
0087     }
0088 
0089     /**
0090      * setConst()
0091      *
0092      * @param bool $const
0093      * @return Zend_CodeGenerator_Php_Property
0094      */
0095     public function setConst($const)
0096     {
0097         $this->_isConst = $const;
0098         return $this;
0099     }
0100 
0101     /**
0102      * isConst()
0103      *
0104      * @return bool
0105      */
0106     public function isConst()
0107     {
0108         return ($this->_isConst) ? true : false;
0109     }
0110 
0111     /**
0112      * setDefaultValue()
0113      *
0114      * @param Zend_CodeGenerator_Php_Property_DefaultValue|string|array $defaultValue
0115      * @return Zend_CodeGenerator_Php_Property
0116      */
0117     public function setDefaultValue($defaultValue)
0118     {
0119         // if it looks like
0120         if (is_array($defaultValue)
0121             && array_key_exists('value', $defaultValue)
0122             && array_key_exists('type', $defaultValue)) {
0123             $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue($defaultValue);
0124         }
0125 
0126         if (!($defaultValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue)) {
0127             $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue(array('value' => $defaultValue));
0128         }
0129 
0130         $this->_defaultValue = $defaultValue;
0131         return $this;
0132     }
0133 
0134     /**
0135      * getDefaultValue()
0136      *
0137      * @return Zend_CodeGenerator_Php_Property_DefaultValue
0138      */
0139     public function getDefaultValue()
0140     {
0141         return $this->_defaultValue;
0142     }
0143 
0144     /**
0145      * generate()
0146      *
0147      * @return string
0148      */
0149     public function generate()
0150     {
0151         $name         = $this->getName();
0152         $defaultValue = $this->getDefaultValue();
0153 
0154         $output = '';
0155 
0156         if (($docblock = $this->getDocblock()) !== null) {
0157             $docblock->setIndentation('    ');
0158             $output .= $docblock->generate();
0159         }
0160 
0161         if ($this->isConst()) {
0162             if ($defaultValue != null && !$defaultValue->isValidConstantType()) {
0163                 // require_once 'Zend/CodeGenerator/Php/Exception.php';
0164                 throw new Zend_CodeGenerator_Php_Exception('The property ' . $this->_name . ' is said to be '
0165                     . 'constant but does not have a valid constant value.');
0166             }
0167             $output .= $this->_indentation . 'const ' . $name . ' = '
0168                 . (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
0169         } else {
0170             $output .= $this->_indentation
0171                 . $this->getVisibility()
0172                 . (($this->isStatic()) ? ' static' : '')
0173                 . ' $' . $name . ' = '
0174                 . (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
0175         }
0176         return $output;
0177     }
0178 
0179 }