You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
3.9 KiB

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