1<?php
2
3declare(strict_types=1);
4
5namespace PhpParser\Builder;
6
7use PhpParser;
8use PhpParser\BuilderHelpers;
9use PhpParser\Node;
10use PhpParser\Node\Identifier;
11use PhpParser\Node\Stmt;
12
13class EnumCase implements PhpParser\Builder {
14    /** @var Identifier|string */
15    protected $name;
16    /** @var ?Node\Expr */
17    protected ?Node\Expr $value = null;
18    /** @var array<string, mixed> */
19    protected array $attributes = [];
20
21    /** @var list<Node\AttributeGroup> */
22    protected array $attributeGroups = [];
23
24    /**
25     * Creates an enum case builder.
26     *
27     * @param string|Identifier $name Name
28     */
29    public function __construct($name) {
30        $this->name = $name;
31    }
32
33    /**
34     * Sets the value.
35     *
36     * @param Node\Expr|string|int $value
37     *
38     * @return $this
39     */
40    public function setValue($value) {
41        $this->value = BuilderHelpers::normalizeValue($value);
42
43        return $this;
44    }
45
46    /**
47     * Sets doc comment for the constant.
48     *
49     * @param PhpParser\Comment\Doc|string $docComment Doc comment to set
50     *
51     * @return $this The builder instance (for fluid interface)
52     */
53    public function setDocComment($docComment) {
54        $this->attributes = [
55            'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
56        ];
57
58        return $this;
59    }
60
61    /**
62     * Adds an attribute group.
63     *
64     * @param Node\Attribute|Node\AttributeGroup $attribute
65     *
66     * @return $this The builder instance (for fluid interface)
67     */
68    public function addAttribute($attribute) {
69        $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
70
71        return $this;
72    }
73
74    /**
75     * Returns the built enum case node.
76     *
77     * @return Stmt\EnumCase The built constant node
78     */
79    public function getNode(): PhpParser\Node {
80        return new Stmt\EnumCase(
81            $this->name,
82            $this->value,
83            $this->attributeGroups,
84            $this->attributes
85        );
86    }
87}
88