CommunityID/libs/Zend/CodeGenerator/Php/Property.php

180 lines
5.2 KiB
PHP
Raw Normal View History

2019-07-17 20:16:19 +00:00
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Property.php 18951 2009-11-12 16:26:19Z alexander $
2019-07-17 20:16:19 +00:00
*/
/**
* @see Zend_CodeGenerator_Php_Member_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Member/Abstract.php';
2019-07-17 20:19:00 +00:00
/**
* @see Zend_CodeGenerator_Php_Property_DefaultValue
*/
require_once 'Zend/CodeGenerator/Php/Property/DefaultValue.php';
2019-07-17 20:16:19 +00:00
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Property extends Zend_CodeGenerator_Php_Member_Abstract
2019-07-17 20:16:19 +00:00
{
2019-07-17 20:16:19 +00:00
/**
* @var bool
*/
protected $_isConst = null;
2019-07-17 20:16:19 +00:00
/**
* @var string
*/
protected $_defaultValue = null;
/**
* fromReflection()
*
* @param Zend_Reflection_Property $reflectionProperty
* @return Zend_CodeGenerator_Php_Property
*/
2019-07-17 20:19:00 +00:00
public static function fromReflection(Zend_Reflection_Property $reflectionProperty)
{
2019-07-17 20:16:19 +00:00
$property = new self();
2019-07-17 20:19:00 +00:00
$property->setName($reflectionProperty->getName());
2019-07-17 20:19:00 +00:00
$allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties();
2019-07-17 20:19:00 +00:00
$property->setDefaultValue($allDefaultProperties[$reflectionProperty->getName()]);
2019-07-17 20:19:00 +00:00
if ($reflectionProperty->getDocComment() != '') {
$property->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionProperty->getDocComment()));
}
2019-07-17 20:19:00 +00:00
if ($reflectionProperty->isStatic()) {
$property->setStatic(true);
}
2019-07-17 20:19:00 +00:00
if ($reflectionProperty->isPrivate()) {
$property->setVisibility(self::VISIBILITY_PRIVATE);
} elseif ($reflectionProperty->isProtected()) {
$property->setVisibility(self::VISIBILITY_PROTECTED);
} else {
$property->setVisibility(self::VISIBILITY_PUBLIC);
}
2019-07-17 20:16:19 +00:00
$property->setSourceDirty(false);
2019-07-17 20:16:19 +00:00
return $property;
}
2019-07-17 20:16:19 +00:00
/**
* setConst()
*
* @param bool $const
* @return Zend_CodeGenerator_Php_Property
*/
public function setConst($const)
{
$this->_isConst = $const;
return $this;
}
2019-07-17 20:16:19 +00:00
/**
* isConst()
*
* @return bool
*/
public function isConst()
{
return ($this->_isConst) ? true : false;
}
2019-07-17 20:19:00 +00:00
2019-07-17 20:16:19 +00:00
/**
* setDefaultValue()
*
2019-07-17 20:19:00 +00:00
* @param Zend_CodeGenerator_Php_Property_DefaultValue|string|array $defaultValue
2019-07-17 20:16:19 +00:00
* @return Zend_CodeGenerator_Php_Property
*/
public function setDefaultValue($defaultValue)
{
2019-07-17 20:19:00 +00:00
// if it looks like
if (is_array($defaultValue)
2019-07-17 20:19:00 +00:00
&& array_key_exists('value', $defaultValue)
&& array_key_exists('type', $defaultValue)) {
$defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue($defaultValue);
}
2019-07-17 20:19:00 +00:00
if (!($defaultValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue)) {
$defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue(array('value' => $defaultValue));
}
2019-07-17 20:16:19 +00:00
$this->_defaultValue = $defaultValue;
return $this;
}
2019-07-17 20:19:00 +00:00
2019-07-17 20:16:19 +00:00
/**
* getDefaultValue()
*
2019-07-17 20:19:00 +00:00
* @return Zend_CodeGenerator_Php_Property_DefaultValue
2019-07-17 20:16:19 +00:00
*/
public function getDefaultValue()
{
return $this->_defaultValue;
}
2019-07-17 20:16:19 +00:00
/**
* generate()
*
* @return string
*/
public function generate()
{
$name = $this->getName();
$defaultValue = $this->getDefaultValue();
2019-07-17 20:19:00 +00:00
$output = '';
2019-07-17 20:19:00 +00:00
if (($docblock = $this->getDocblock()) !== null) {
$docblock->setIndentation(' ');
$output .= $docblock->generate();
}
2019-07-17 20:16:19 +00:00
if ($this->isConst()) {
2019-07-17 20:19:00 +00:00
if ($defaultValue != null && !$defaultValue->isValidConstantType()) {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('The property ' . $this->_name . ' is said to be '
. 'constant but does not have a valid constant value.');
}
$output .= $this->_indentation . 'const ' . $name . ' = '
2019-07-17 20:19:00 +00:00
. (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
2019-07-17 20:16:19 +00:00
} else {
2019-07-17 20:19:00 +00:00
$output .= $this->_indentation
. $this->getVisibility()
. (($this->isStatic()) ? ' static' : '')
2019-07-17 20:19:00 +00:00
. ' $' . $name . ' = '
. (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
2019-07-17 20:16:19 +00:00
}
return $output;
2019-07-17 20:16:19 +00:00
}
2019-07-17 20:16:19 +00:00
}