1<?php
2
3declare(strict_types=1);
4
5namespace PhpParser\Builder;
6
7use PhpParser;
8use PhpParser\BuilderHelpers;
9use PhpParser\Modifiers;
10use PhpParser\Node;
11use PhpParser\Node\Const_;
12use PhpParser\Node\Identifier;
13use PhpParser\Node\Stmt;
14
15class ClassConst implements PhpParser\Builder {
16    protected int $flags = 0;
17    /** @var array<string, mixed> */
18    protected array $attributes = [];
19    /** @var list<Const_> */
20    protected array $constants = [];
21
22    /** @var list<Node\AttributeGroup> */
23    protected array $attributeGroups = [];
24    /** @var Identifier|Node\Name|Node\ComplexType|null */
25    protected ?Node $type = null;
26
27    /**
28     * Creates a class constant builder
29     *
30     * @param string|Identifier $name Name
31     * @param Node\Expr|bool|null|int|float|string|array $value Value
32     */
33    public function __construct($name, $value) {
34        $this->constants = [new Const_($name, BuilderHelpers::normalizeValue($value))];
35    }
36
37    /**
38     * Add another constant to const group
39     *
40     * @param string|Identifier $name Name
41     * @param Node\Expr|bool|null|int|float|string|array $value Value
42     *
43     * @return $this The builder instance (for fluid interface)
44     */
45    public function addConst($name, $value) {
46        $this->constants[] = new Const_($name, BuilderHelpers::normalizeValue($value));
47
48        return $this;
49    }
50
51    /**
52     * Makes the constant public.
53     *
54     * @return $this The builder instance (for fluid interface)
55     */
56    public function makePublic() {
57        $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
58
59        return $this;
60    }
61
62    /**
63     * Makes the constant protected.
64     *
65     * @return $this The builder instance (for fluid interface)
66     */
67    public function makeProtected() {
68        $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
69
70        return $this;
71    }
72
73    /**
74     * Makes the constant private.
75     *
76     * @return $this The builder instance (for fluid interface)
77     */
78    public function makePrivate() {
79        $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
80
81        return $this;
82    }
83
84    /**
85     * Makes the constant final.
86     *
87     * @return $this The builder instance (for fluid interface)
88     */
89    public function makeFinal() {
90        $this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::FINAL);
91
92        return $this;
93    }
94
95    /**
96     * Sets doc comment for the constant.
97     *
98     * @param PhpParser\Comment\Doc|string $docComment Doc comment to set
99     *
100     * @return $this The builder instance (for fluid interface)
101     */
102    public function setDocComment($docComment) {
103        $this->attributes = [
104            'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
105        ];
106
107        return $this;
108    }
109
110    /**
111     * Adds an attribute group.
112     *
113     * @param Node\Attribute|Node\AttributeGroup $attribute
114     *
115     * @return $this The builder instance (for fluid interface)
116     */
117    public function addAttribute($attribute) {
118        $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
119
120        return $this;
121    }
122
123    /**
124     * Sets the constant type.
125     *
126     * @param string|Node\Name|Identifier|Node\ComplexType $type
127     *
128     * @return $this
129     */
130    public function setType($type) {
131        $this->type = BuilderHelpers::normalizeType($type);
132
133        return $this;
134    }
135
136    /**
137     * Returns the built class node.
138     *
139     * @return Stmt\ClassConst The built constant node
140     */
141    public function getNode(): PhpParser\Node {
142        return new Stmt\ClassConst(
143            $this->constants,
144            $this->flags,
145            $this->attributes,
146            $this->attributeGroups,
147            $this->type
148        );
149    }
150}
151