PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/CodeGenerator/Php/Property.php

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