Property.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace PhpParser\Builder;
  3. use PhpParser;
  4. use PhpParser\Node\Stmt;
  5. class Property extends PhpParser\BuilderAbstract
  6. {
  7. protected $name;
  8. protected $flags = 0;
  9. protected $default = null;
  10. protected $attributes = array();
  11. /**
  12. * Creates a property builder.
  13. *
  14. * @param string $name Name of the property
  15. */
  16. public function __construct($name) {
  17. $this->name = $name;
  18. }
  19. /**
  20. * Makes the property public.
  21. *
  22. * @return $this The builder instance (for fluid interface)
  23. */
  24. public function makePublic() {
  25. $this->setModifier(Stmt\Class_::MODIFIER_PUBLIC);
  26. return $this;
  27. }
  28. /**
  29. * Makes the property protected.
  30. *
  31. * @return $this The builder instance (for fluid interface)
  32. */
  33. public function makeProtected() {
  34. $this->setModifier(Stmt\Class_::MODIFIER_PROTECTED);
  35. return $this;
  36. }
  37. /**
  38. * Makes the property private.
  39. *
  40. * @return $this The builder instance (for fluid interface)
  41. */
  42. public function makePrivate() {
  43. $this->setModifier(Stmt\Class_::MODIFIER_PRIVATE);
  44. return $this;
  45. }
  46. /**
  47. * Makes the property static.
  48. *
  49. * @return $this The builder instance (for fluid interface)
  50. */
  51. public function makeStatic() {
  52. $this->setModifier(Stmt\Class_::MODIFIER_STATIC);
  53. return $this;
  54. }
  55. /**
  56. * Sets default value for the property.
  57. *
  58. * @param mixed $value Default value to use
  59. *
  60. * @return $this The builder instance (for fluid interface)
  61. */
  62. public function setDefault($value) {
  63. $this->default = $this->normalizeValue($value);
  64. return $this;
  65. }
  66. /**
  67. * Sets doc comment for the property.
  68. *
  69. * @param PhpParser\Comment\Doc|string $docComment Doc comment to set
  70. *
  71. * @return $this The builder instance (for fluid interface)
  72. */
  73. public function setDocComment($docComment) {
  74. $this->attributes = array(
  75. 'comments' => array($this->normalizeDocComment($docComment))
  76. );
  77. return $this;
  78. }
  79. /**
  80. * Returns the built class node.
  81. *
  82. * @return Stmt\Property The built property node
  83. */
  84. public function getNode() {
  85. return new Stmt\Property(
  86. $this->flags !== 0 ? $this->flags : Stmt\Class_::MODIFIER_PUBLIC,
  87. array(
  88. new Stmt\PropertyProperty($this->name, $this->default)
  89. ),
  90. $this->attributes
  91. );
  92. }
  93. }