EnumCase.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Identifier;
  8. use PhpParser\Node\Stmt;
  9. class EnumCase implements PhpParser\Builder {
  10. /** @var Identifier|string */
  11. protected $name;
  12. /** @var ?Node\Expr */
  13. protected ?Node\Expr $value = null;
  14. /** @var array<string, mixed> */
  15. protected array $attributes = [];
  16. /** @var list<Node\AttributeGroup> */
  17. protected array $attributeGroups = [];
  18. /**
  19. * Creates an enum case builder.
  20. *
  21. * @param string|Identifier $name Name
  22. */
  23. public function __construct($name) {
  24. $this->name = $name;
  25. }
  26. /**
  27. * Sets the value.
  28. *
  29. * @param Node\Expr|string|int $value
  30. *
  31. * @return $this
  32. */
  33. public function setValue($value) {
  34. $this->value = BuilderHelpers::normalizeValue($value);
  35. return $this;
  36. }
  37. /**
  38. * Sets doc comment for the constant.
  39. *
  40. * @param PhpParser\Comment\Doc|string $docComment Doc comment to set
  41. *
  42. * @return $this The builder instance (for fluid interface)
  43. */
  44. public function setDocComment($docComment) {
  45. $this->attributes = [
  46. 'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
  47. ];
  48. return $this;
  49. }
  50. /**
  51. * Adds an attribute group.
  52. *
  53. * @param Node\Attribute|Node\AttributeGroup $attribute
  54. *
  55. * @return $this The builder instance (for fluid interface)
  56. */
  57. public function addAttribute($attribute) {
  58. $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
  59. return $this;
  60. }
  61. /**
  62. * Returns the built enum case node.
  63. *
  64. * @return Stmt\EnumCase The built constant node
  65. */
  66. public function getNode(): PhpParser\Node {
  67. return new Stmt\EnumCase(
  68. $this->name,
  69. $this->value,
  70. $this->attributeGroups,
  71. $this->attributes
  72. );
  73. }
  74. }