No Description

Enum_.php 2.9KB

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