Enum_.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser;
  4. use PhpParser\BuilderHelpers;
  5. use PhpParser\Node;
  6. use PhpParser\Node\Identifier;
  7. use PhpParser\Node\Name;
  8. use PhpParser\Node\Stmt;
  9. class Enum_ extends Declaration {
  10. protected string $name;
  11. protected ?Identifier $scalarType = null;
  12. /** @var list<Name> */
  13. protected array $implements = [];
  14. /** @var list<Stmt\TraitUse> */
  15. protected array $uses = [];
  16. /** @var list<Stmt\EnumCase> */
  17. protected array $enumCases = [];
  18. /** @var list<Stmt\ClassConst> */
  19. protected array $constants = [];
  20. /** @var list<Stmt\ClassMethod> */
  21. protected array $methods = [];
  22. /** @var list<Node\AttributeGroup> */
  23. protected array $attributeGroups = [];
  24. /**
  25. * Creates an enum builder.
  26. *
  27. * @param string $name Name of the enum
  28. */
  29. public function __construct(string $name) {
  30. $this->name = $name;
  31. }
  32. /**
  33. * Sets the scalar type.
  34. *
  35. * @param string|Identifier $scalarType
  36. *
  37. * @return $this
  38. */
  39. public function setScalarType($scalarType) {
  40. $this->scalarType = BuilderHelpers::normalizeType($scalarType);
  41. return $this;
  42. }
  43. /**
  44. * Implements one or more interfaces.
  45. *
  46. * @param Name|string ...$interfaces Names of interfaces to implement
  47. *
  48. * @return $this The builder instance (for fluid interface)
  49. */
  50. public function implement(...$interfaces) {
  51. foreach ($interfaces as $interface) {
  52. $this->implements[] = BuilderHelpers::normalizeName($interface);
  53. }
  54. return $this;
  55. }
  56. /**
  57. * Adds a statement.
  58. *
  59. * @param Stmt|PhpParser\Builder $stmt The statement to add
  60. *
  61. * @return $this The builder instance (for fluid interface)
  62. */
  63. public function addStmt($stmt) {
  64. $stmt = BuilderHelpers::normalizeNode($stmt);
  65. if ($stmt instanceof Stmt\EnumCase) {
  66. $this->enumCases[] = $stmt;
  67. } elseif ($stmt instanceof Stmt\ClassMethod) {
  68. $this->methods[] = $stmt;
  69. } elseif ($stmt instanceof Stmt\TraitUse) {
  70. $this->uses[] = $stmt;
  71. } elseif ($stmt instanceof Stmt\ClassConst) {
  72. $this->constants[] = $stmt;
  73. } else {
  74. throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
  75. }
  76. return $this;
  77. }
  78. /**
  79. * Adds an attribute group.
  80. *
  81. * @param Node\Attribute|Node\AttributeGroup $attribute
  82. *
  83. * @return $this The builder instance (for fluid interface)
  84. */
  85. public function addAttribute($attribute) {
  86. $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
  87. return $this;
  88. }
  89. /**
  90. * Returns the built class node.
  91. *
  92. * @return Stmt\Enum_ The built enum node
  93. */
  94. public function getNode(): PhpParser\Node {
  95. return new Stmt\Enum_($this->name, [
  96. 'scalarType' => $this->scalarType,
  97. 'implements' => $this->implements,
  98. 'stmts' => array_merge($this->uses, $this->enumCases, $this->constants, $this->methods),
  99. 'attrGroups' => $this->attributeGroups,
  100. ], $this->attributes);
  101. }
  102. }