EnumCase.php 2.0 KB

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