/setup/ekot/genarator/PropertyGenerator.php

https://github.com/tyoro/tiarraMetro · PHP · 217 lines · 91 code · 15 blank · 111 comment · 14 complexity · 541699a4156310a3d22a194d64dc4430 MD5 · raw file

  1. <?php
  2. /**
  3. * Ekot - the PHP Simple Migration
  4. *
  5. * PHP version 5
  6. *
  7. * @category Ekot
  8. * @package Ekot
  9. * @link http://bitbucket.org/localdisk/ekot/
  10. * @author MATSUO Masaru
  11. * @copyright 2010 MATSUO Masaru
  12. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  13. *
  14. */
  15. require_once 'AbstractGenerator.php';
  16. require_once 'CommentGenerator.php';
  17. /**
  18. * PropertyGenerator
  19. *
  20. * @package Ekot
  21. * @author MATSUO Masaru
  22. */
  23. class PropertyGenerator extends AbstractGenerator {
  24. /**
  25. * comment
  26. * @var CommentGenerator
  27. */
  28. private $_comment;
  29. /**
  30. * name
  31. * @var string
  32. */
  33. private $_name;
  34. /**
  35. * scope
  36. * @var string
  37. */
  38. private $_scope;
  39. /**
  40. * isStatic
  41. * @var bool
  42. */
  43. private $_isStatic = false;
  44. /**
  45. * isConst
  46. * @var bool
  47. */
  48. private $_isConst = false;
  49. /**
  50. * type
  51. * @var string
  52. */
  53. private $_type;
  54. /**
  55. * body
  56. * @var mixed
  57. */
  58. private $_body;
  59. /**
  60. * Constructor
  61. * @param $name string
  62. * @param $type string
  63. * @param $body string
  64. */
  65. public function __construct($name = null, $scope = null, $type = null, $body = null) {
  66. $this->_comment = new CommentGenerator();
  67. if ($name !== null) {
  68. $this->_name = $name;
  69. $this->_comment->setName($name);
  70. }
  71. if ($scope !== null) $this->_scope = $scope;
  72. if ($type !== null) {
  73. $this->_type = $type;
  74. $this->_comment->addTag(new CommentTagGenerator('var', $type));
  75. }
  76. if ($body !== null) $this->_body = $body;
  77. }
  78. /**
  79. * generate
  80. * @return string
  81. */
  82. public function generate() {
  83. if ($this->_name === null) throw new Exception('name is null');
  84. if ($this->_scope === null && $this->_isConst === false) {
  85. throw new Exception('scope is null');
  86. }
  87. $out .= $this->_comment->generate();
  88. $out .= self::INDENT;
  89. if ($this->_isConst === true) {
  90. $out .= 'const ' . $this->_name;
  91. } else {
  92. if ($this->_isStatic === true) {
  93. $out .= $this->_scope . ' static ' . ' $' . $this->_name;
  94. } else {
  95. $out .= $this->_scope . ' $' . $this->_name;
  96. }
  97. }
  98. if ($this->_body !== null) $out .= ' = ' . $this->_body;
  99. $out .= ';' . self::LINE_FEED;
  100. return $out;
  101. }
  102. /**
  103. * getName
  104. * @return string
  105. */
  106. public function getName() {
  107. return $this->_name;
  108. }
  109. /**
  110. * setName
  111. * @param string $name
  112. * @return PropertyGenerator
  113. */
  114. public function setName($name) {
  115. $this->_name = $name;
  116. $this->_comment->setName($name);
  117. return $this;
  118. }
  119. /**
  120. * getScope
  121. * @return string
  122. */
  123. public function getScope() {
  124. return $this->_scope;
  125. }
  126. /**
  127. * setScope
  128. * @param string $scope
  129. * @return PropertyGenerator
  130. */
  131. public function setScope($scope) {
  132. $this->_scope = $scope;
  133. return $this;
  134. }
  135. /**
  136. * isStatic
  137. * @return bool
  138. */
  139. public function isStatic() {
  140. return $this->_isStatic;
  141. }
  142. /**
  143. * setStatic
  144. * @param bool $static
  145. * @return PropertyGenerator
  146. */
  147. public function setStatic($static) {
  148. if (!is_bool($static)) throw new Exception('arg type is not boolean');
  149. $this->_isStatic = $static;
  150. return $this;
  151. }
  152. /**
  153. * isConst
  154. * @return bool
  155. */
  156. public function isConst() {
  157. return $this->_isConst;
  158. }
  159. /**
  160. * isConst
  161. * @param bool $const
  162. * @return PropertyGenerator
  163. */
  164. public function setConst($const) {
  165. if (!is_bool($const)) throw new Exception('arg type is not boolean');
  166. $this->_isConst = $const;
  167. return $this;
  168. }
  169. /**
  170. * getType
  171. * @return string
  172. */
  173. public function getType() {
  174. return $this->_type;
  175. }
  176. /**
  177. * setType
  178. * @param string $type
  179. * @return PropertyGenerator
  180. */
  181. public function setType($type) {
  182. $this->_type = $type;
  183. $this->_comment->addTag(new CommentGenerator('var', $type));
  184. return $this;
  185. }
  186. /**
  187. * getBody
  188. * @return mixed
  189. */
  190. public function getBody() {
  191. return $this->_body;
  192. }
  193. /**
  194. * setBody
  195. * @param mixed $body
  196. * @return PropertyGenerator
  197. */
  198. public function setBody($body) {
  199. $this->_body = $body;
  200. return $this;
  201. }
  202. }