EnumCase.php 1.9 KB

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