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.

132 lines
3.4 KiB

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