ClassConst.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpParser\Builder;
  4. use PhpParser;
  5. use PhpParser\BuilderHelpers;
  6. use PhpParser\Modifiers;
  7. use PhpParser\Node;
  8. use PhpParser\Node\Const_;
  9. use PhpParser\Node\Identifier;
  10. use PhpParser\Node\Stmt;
  11. class ClassConst implements PhpParser\Builder {
  12. protected int $flags = 0;
  13. /** @var array<string, mixed> */
  14. protected array $attributes = [];
  15. /** @var list<Const_> */
  16. protected array $constants = [];
  17. /** @var list<Node\AttributeGroup> */
  18. protected array $attributeGroups = [];
  19. /** @var Identifier|Node\Name|Node\ComplexType|null */
  20. protected ?Node $type = null;
  21. /**
  22. * Creates a class constant builder
  23. *
  24. * @param string|Identifier $name Name
  25. * @param Node\Expr|bool|null|int|float|string|array $value Value
  26. */
  27. public function __construct($name, $value) {
  28. $this->constants = [new Const_($name, BuilderHelpers::normalizeValue($value))];
  29. }
  30. /**
  31. * Add another constant to const group
  32. *
  33. * @param string|Identifier $name Name
  34. * @param Node\Expr|bool|null|int|float|string|array $value Value
  35. *
  36. * @return $this The builder instance (for fluid interface)
  37. */
  38. public function addConst($name, $value) {
  39. $this->constants[] = new Const_($name, BuilderHelpers::normalizeValue($value));
  40. return $this;
  41. }
  42. /**
  43. * Makes the constant public.
  44. *
  45. * @return $this The builder instance (for fluid interface)
  46. */
  47. public function makePublic() {
  48. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
  49. return $this;
  50. }
  51. /**
  52. * Makes the constant protected.
  53. *
  54. * @return $this The builder instance (for fluid interface)
  55. */
  56. public function makeProtected() {
  57. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
  58. return $this;
  59. }
  60. /**
  61. * Makes the constant private.
  62. *
  63. * @return $this The builder instance (for fluid interface)
  64. */
  65. public function makePrivate() {
  66. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
  67. return $this;
  68. }
  69. /**
  70. * Makes the constant final.
  71. *
  72. * @return $this The builder instance (for fluid interface)
  73. */
  74. public function makeFinal() {
  75. $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::FINAL);
  76. return $this;
  77. }
  78. /**
  79. * Sets doc comment for the constant.
  80. *
  81. * @param PhpParser\Comment\Doc|string $docComment Doc comment to set
  82. *
  83. * @return $this The builder instance (for fluid interface)
  84. */
  85. public function setDocComment($docComment) {
  86. $this->attributes = [
  87. 'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
  88. ];
  89. return $this;
  90. }
  91. /**
  92. * Adds an attribute group.
  93. *
  94. * @param Node\Attribute|Node\AttributeGroup $attribute
  95. *
  96. * @return $this The builder instance (for fluid interface)
  97. */
  98. public function addAttribute($attribute) {
  99. $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
  100. return $this;
  101. }
  102. /**
  103. * Sets the constant type.
  104. *
  105. * @param string|Node\Name|Identifier|Node\ComplexType $type
  106. *
  107. * @return $this
  108. */
  109. public function setType($type) {
  110. $this->type = BuilderHelpers::normalizeType($type);
  111. return $this;
  112. }
  113. /**
  114. * Returns the built class node.
  115. *
  116. * @return Stmt\ClassConst The built constant node
  117. */
  118. public function getNode(): PhpParser\Node {
  119. return new Stmt\ClassConst(
  120. $this->constants,
  121. $this->flags,
  122. $this->attributes,
  123. $this->attributeGroups,
  124. $this->type
  125. );
  126. }
  127. }