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