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