ClassConst.php 3.7 KB

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