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