Property.php 3.2 KB

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