Property.php 2.7 KB

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