1 | <?php declare(strict_types=1); |
||
17 | * Constants part |
||
18 | * |
||
19 | * For all models that can contain constants |
||
20 | * |
||
21 | * @author Thomas Gossmann |
||
22 | 52 | */ |
|
23 | 52 | trait ConstantsPart { |
|
24 | 52 | ||
25 | /** @var Map */ |
||
26 | private Map $constants; |
||
|
|||
27 | |||
28 | private function initConstants() { |
||
29 | $this->constants = new Map(); |
||
30 | } |
||
31 | |||
32 | 1 | /** |
|
33 | 1 | * Sets a collection of constants |
|
34 | 1 | * |
|
35 | 1 | * @param array|PhpConstant[] $constants |
|
36 | 1 | * |
|
37 | * @return $this |
||
38 | 1 | */ |
|
39 | 1 | public function setConstants(array $constants): self { |
|
40 | 1 | $normalizedConstants = []; |
|
41 | foreach ($constants as $name => $value) { |
||
42 | if ($value instanceof PhpConstant) { |
||
43 | 1 | $name = $value->getName(); |
|
44 | } else { |
||
45 | $constValue = $value; |
||
46 | 1 | $value = new PhpConstant($name); |
|
47 | $value->setValue($constValue); |
||
48 | 1 | } |
|
49 | |||
50 | $normalizedConstants[$name] = $value; |
||
51 | } |
||
52 | |||
53 | $this->constants->setAll($normalizedConstants); |
||
54 | |||
55 | return $this; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | 8 | * Adds a constant |
|
60 | 8 | * |
|
61 | 7 | * @param string|PhpConstant $nameOrConstant constant name or instance |
|
62 | 7 | * @param string|int|float|bool|null $value |
|
63 | * @param bool $isExpression whether the value is an expression or not |
||
64 | 2 | * |
|
65 | 2 | * @return $this |
|
66 | */ |
||
67 | public function setConstant(PhpConstant|string $nameOrConstant, string|int|float|bool|null $value = '', bool $isExpression = false): self { |
||
68 | 8 | if ($nameOrConstant instanceof PhpConstant) { |
|
69 | $name = $nameOrConstant->getName(); |
||
70 | 8 | $constant = $nameOrConstant; |
|
71 | } else { |
||
72 | $name = $nameOrConstant; |
||
73 | $constant = new PhpConstant($nameOrConstant, $value, $isExpression); |
||
74 | } |
||
75 | |||
76 | $this->constants->set($name, $constant); |
||
77 | |||
78 | return $this; |
||
79 | } |
||
80 | 2 | ||
81 | 2 | /** |
|
82 | 1 | * Removes a constant |
|
83 | * |
||
84 | * @param string|PhpConstant $nameOrConstant constant name |
||
85 | 2 | * |
|
86 | 1 | * @throws \InvalidArgumentException If the constant cannot be found |
|
87 | * |
||
88 | * @return $this |
||
89 | 1 | */ |
|
90 | public function removeConstant(PhpConstant|string $nameOrConstant): self { |
||
91 | 1 | if (!$this->constants->has((string) $nameOrConstant)) { |
|
92 | throw new \InvalidArgumentException(sprintf('The constant "%s" does not exist.', $nameOrConstant)); |
||
93 | } |
||
94 | |||
95 | $this->constants->remove((string) $nameOrConstant); |
||
96 | |||
97 | return $this; |
||
98 | } |
||
99 | |||
100 | 2 | /** |
|
101 | 2 | * Checks whether a constant exists |
|
102 | 1 | * |
|
103 | * @param string|PhpConstant $nameOrConstant |
||
104 | * |
||
105 | 2 | * @return bool |
|
106 | */ |
||
107 | public function hasConstant(PhpConstant|string $nameOrConstant): bool { |
||
108 | return $this->constants->has((string) $nameOrConstant); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Returns a constant. |
||
113 | * |
||
114 | * @param string $name |
||
115 | 6 | * |
|
116 | 6 | * @throws \InvalidArgumentException If the constant cannot be found |
|
117 | 1 | * |
|
118 | * @return PhpConstant |
||
119 | */ |
||
120 | 6 | public function getConstant(string $name): PhpConstant { |
|
121 | 2 | if (!$this->constants->has($name)) { |
|
122 | throw new \InvalidArgumentException(sprintf('The constant "%s" does not exist.', $name)); |
||
123 | } |
||
124 | 5 | ||
125 | return $this->constants->get($name); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Returns all constants |
||
130 | * |
||
131 | * @return Map |
||
132 | 16 | */ |
|
133 | 16 | public function getConstants(): Map { |
|
134 | return $this->constants; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Returns all constant names |
||
139 | * |
||
140 | * @return Set |
||
141 | 1 | */ |
|
142 | 1 | public function getConstantNames(): Set { |
|
143 | return $this->constants->keys(); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Clears all constants |
||
148 | * |
||
149 | * @return $this |
||
150 | 1 | */ |
|
151 | 1 | public function clearConstants(): self { |
|
152 | $this->constants->clear(); |
||
153 | 1 | ||
154 | return $this; |
||
155 | } |
||
156 | } |
||
157 |