|
13 | 13 | use Nette\PhpGenerator\PropertyHook;
|
14 | 14 | use Nette\PhpGenerator\PropertyHookType;
|
15 | 15 | use Nette\PhpGenerator\Visibility;
|
16 |
| -use function array_filter, in_array; |
| 16 | +use function array_filter, in_array, is_string; |
17 | 17 |
|
18 | 18 |
|
19 | 19 | /**
|
20 | 20 | * @internal
|
21 | 21 | */
|
22 | 22 | trait PropertyLike
|
23 | 23 | {
|
24 |
| - /** @var array{set: ?string, get: ?string} */ |
25 |
| - private array $visibility = [PropertyAccessMode::Set => null, PropertyAccessMode::Get => null]; |
| 24 | + /** @var array{set: ?Visibility, get: ?Visibility} */ |
| 25 | + private array $visibility = ['set' => null, 'get' => null]; |
26 | 26 | private bool $final = false;
|
27 | 27 | private bool $readOnly = false;
|
28 | 28 |
|
29 | 29 | /** @var array<string, ?PropertyHook> */
|
30 |
| - private array $hooks = [PropertyHookType::Set => null, PropertyHookType::Get => null]; |
| 30 | + private array $hooks = ['set' => null, 'get' => null]; |
31 | 31 |
|
32 | 32 |
|
33 |
| - /** |
34 |
| - * @param 'public'|'protected'|'private'|null $get |
35 |
| - * @param 'public'|'protected'|'private'|null $set |
36 |
| - */ |
37 |
| - public function setVisibility(?string $get, ?string $set = null): static |
| 33 | + public function setVisibility(Visibility|string|null $get, Visibility|string|null $set = null): static |
38 | 34 | {
|
39 | 35 | $this->visibility = [
|
40 |
| - PropertyAccessMode::Set => $set === null ? $set : Visibility::from($set), |
41 |
| - PropertyAccessMode::Get => $get === null ? $get : Visibility::from($get), |
| 36 | + 'set' => $set instanceof Visibility || $set === null ? $set : Visibility::from($set), |
| 37 | + 'get' => $get instanceof Visibility || $get === null ? $get : Visibility::from($get), |
42 | 38 | ];
|
43 | 39 | return $this;
|
44 | 40 | }
|
45 | 41 |
|
46 | 42 |
|
47 |
| - /** @param 'set'|'get' $mode */ |
48 |
| - public function getVisibility(string $mode = PropertyAccessMode::Get): ?string |
| 43 | + public function getVisibility(PropertyAccessMode|string $mode = PropertyAccessMode::Get): ?string |
49 | 44 | {
|
50 |
| - return $this->visibility[PropertyAccessMode::from($mode)]; |
| 45 | + $mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode; |
| 46 | + return $this->visibility[$mode->value]?->value; |
51 | 47 | }
|
52 | 48 |
|
53 | 49 |
|
54 |
| - /** @param 'set'|'get' $mode */ |
55 |
| - public function setPublic(string $mode = PropertyAccessMode::Get): static |
| 50 | + public function setPublic(PropertyAccessMode|string $mode = PropertyAccessMode::Get): static |
56 | 51 | {
|
57 |
| - $this->visibility[PropertyAccessMode::from($mode)] = Visibility::Public; |
| 52 | + $mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode; |
| 53 | + $this->visibility[$mode->value] = Visibility::Public; |
58 | 54 | return $this;
|
59 | 55 | }
|
60 | 56 |
|
61 | 57 |
|
62 |
| - /** @param 'set'|'get' $mode */ |
63 |
| - public function isPublic(string $mode = PropertyAccessMode::Get): bool |
| 58 | + public function isPublic(PropertyAccessMode|string $mode = PropertyAccessMode::Get): bool |
64 | 59 | {
|
65 |
| - return in_array($this->visibility[PropertyAccessMode::from($mode)], [Visibility::Public, null], true); |
| 60 | + $mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode; |
| 61 | + return in_array($this->visibility[$mode->value], [Visibility::Public, null], true); |
66 | 62 | }
|
67 | 63 |
|
68 | 64 |
|
69 |
| - /** @param 'set'|'get' $mode */ |
70 |
| - public function setProtected(string $mode = PropertyAccessMode::Get): static |
| 65 | + public function setProtected(PropertyAccessMode|string $mode = PropertyAccessMode::Get): static |
71 | 66 | {
|
72 |
| - $this->visibility[PropertyAccessMode::from($mode)] = Visibility::Protected; |
| 67 | + $mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode; |
| 68 | + $this->visibility[$mode->value] = Visibility::Protected; |
73 | 69 | return $this;
|
74 | 70 | }
|
75 | 71 |
|
76 | 72 |
|
77 |
| - /** @param 'set'|'get' $mode */ |
78 |
| - public function isProtected(string $mode = PropertyAccessMode::Get): bool |
| 73 | + public function isProtected(PropertyAccessMode|string $mode = PropertyAccessMode::Get): bool |
79 | 74 | {
|
80 |
| - return $this->visibility[PropertyAccessMode::from($mode)] === Visibility::Protected; |
| 75 | + $mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode; |
| 76 | + return $this->visibility[$mode->value] === Visibility::Protected; |
81 | 77 | }
|
82 | 78 |
|
83 | 79 |
|
84 |
| - /** @param 'set'|'get' $mode */ |
85 |
| - public function setPrivate(string $mode = PropertyAccessMode::Get): static |
| 80 | + public function setPrivate(PropertyAccessMode|string $mode = PropertyAccessMode::Get): static |
86 | 81 | {
|
87 |
| - $this->visibility[PropertyAccessMode::from($mode)] = Visibility::Private; |
| 82 | + $mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode; |
| 83 | + $this->visibility[$mode->value] = Visibility::Private; |
88 | 84 | return $this;
|
89 | 85 | }
|
90 | 86 |
|
91 | 87 |
|
92 |
| - /** @param 'set'|'get' $mode */ |
93 |
| - public function isPrivate(string $mode = PropertyAccessMode::Get): bool |
| 88 | + public function isPrivate(PropertyAccessMode|string $mode = PropertyAccessMode::Get): bool |
94 | 89 | {
|
95 |
| - return $this->visibility[PropertyAccessMode::from($mode)] === Visibility::Private; |
| 90 | + $mode = is_string($mode) ? PropertyAccessMode::from($mode) : $mode; |
| 91 | + return $this->visibility[$mode->value] === Visibility::Private; |
96 | 92 | }
|
97 | 93 |
|
98 | 94 |
|
@@ -141,24 +137,24 @@ public function getHooks(): array
|
141 | 137 | }
|
142 | 138 |
|
143 | 139 |
|
144 |
| - /** @param 'set'|'get' $type */ |
145 |
| - public function addHook(string $type, string $shortBody = ''): PropertyHook |
| 140 | + public function addHook(PropertyHookType|string $type, string $shortBody = ''): PropertyHook |
146 | 141 | {
|
147 |
| - return $this->hooks[PropertyHookType::from($type)] = (new PropertyHook) |
| 142 | + $type = is_string($type) ? PropertyHookType::from($type) : $type; |
| 143 | + return $this->hooks[$type->value] = (new PropertyHook) |
148 | 144 | ->setBody($shortBody, short: true);
|
149 | 145 | }
|
150 | 146 |
|
151 | 147 |
|
152 |
| - /** @param 'set'|'get' $type */ |
153 |
| - public function getHook(string $type): ?PropertyHook |
| 148 | + public function getHook(PropertyHookType|string $type): ?PropertyHook |
154 | 149 | {
|
155 |
| - return $this->hooks[PropertyHookType::from($type)] ?? null; |
| 150 | + $type = is_string($type) ? PropertyHookType::from($type) : $type; |
| 151 | + return $this->hooks[$type->value] ?? null; |
156 | 152 | }
|
157 | 153 |
|
158 | 154 |
|
159 |
| - /** @param 'set'|'get' $type */ |
160 |
| - public function hasHook(string $type): bool |
| 155 | + public function hasHook(PropertyHookType|string $type): bool |
161 | 156 | {
|
162 |
| - return isset($this->hooks[PropertyHookType::from($type)]); |
| 157 | + $type = is_string($type) ? PropertyHookType::from($type) : $type; |
| 158 | + return isset($this->hooks[$type->value]); |
163 | 159 | }
|
164 | 160 | }
|
0 commit comments