|
9 | 9 |
|
10 | 10 | namespace Nette\PhpGenerator\Traits; |
11 | 11 |
|
| 12 | +use Nette; |
| 13 | +use Nette\PhpGenerator\Modifier; |
12 | 14 | use Nette\PhpGenerator\PropertyHook; |
13 | 15 |
|
14 | 16 |
|
|
17 | 19 | */ |
18 | 20 | trait PropertyLike |
19 | 21 | { |
20 | | - use VisibilityAware; |
21 | | - |
| 22 | + /** @var array{'set' => ?string, 'get' => ?string} */ |
| 23 | + private array $visibility = ['set' => null, 'get' => null]; |
22 | 24 | private bool $readOnly = false; |
23 | 25 |
|
24 | 26 | /** @var array<string, ?PropertyHook> */ |
25 | 27 | private array $hooks = ['set' => null, 'get' => null]; |
26 | 28 |
|
27 | 29 |
|
| 30 | + /** |
| 31 | + * @param ?string $get public|protected|private |
| 32 | + * @param ?string $set public|protected|private |
| 33 | + */ |
| 34 | + public function setVisibility(?string $get, ?string $set = null): static |
| 35 | + { |
| 36 | + if (!in_array($get, [Modifier::Public, Modifier::Protected, Modifier::Private, null], true) |
| 37 | + || !in_array($set, [Modifier::Public, Modifier::Protected, Modifier::Private, null], true)) { |
| 38 | + throw new Nette\InvalidArgumentException('Argument must be public|protected|private.'); |
| 39 | + } |
| 40 | + |
| 41 | + $this->visibility = ['set' => $set, 'get' => $get]; |
| 42 | + return $this; |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + /** @param 'set'|'get' $mode */ |
| 47 | + public function getVisibility(string $mode = 'get'): ?string |
| 48 | + { |
| 49 | + return $this->visibility[$this->checkMode($mode)]; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /** @param 'set'|'get' $mode */ |
| 54 | + public function setPublic(string $mode = 'get'): static |
| 55 | + { |
| 56 | + $this->visibility[$this->checkMode($mode)] = Modifier::Public; |
| 57 | + return $this; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + /** @param 'set'|'get' $mode */ |
| 62 | + public function isPublic(string $mode = 'get'): bool |
| 63 | + { |
| 64 | + return in_array($this->visibility[$this->checkMode($mode)], [Modifier::Public, null], true); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /** @param 'set'|'get' $mode */ |
| 69 | + public function setProtected(string $mode = 'get'): static |
| 70 | + { |
| 71 | + $this->visibility[$this->checkMode($mode)] = Modifier::Protected; |
| 72 | + return $this; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + /** @param 'set'|'get' $mode */ |
| 77 | + public function isProtected(string $mode = 'get'): bool |
| 78 | + { |
| 79 | + return $this->visibility[$this->checkMode($mode)] === Modifier::Protected; |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + /** @param 'set'|'get' $mode */ |
| 84 | + public function setPrivate(string $mode = 'get'): static |
| 85 | + { |
| 86 | + $this->visibility[$this->checkMode($mode)] = Modifier::Private; |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + /** @param 'set'|'get' $mode */ |
| 92 | + public function isPrivate(string $mode = 'get'): bool |
| 93 | + { |
| 94 | + return $this->visibility[$this->checkMode($mode)] === Modifier::Private; |
| 95 | + } |
| 96 | + |
| 97 | + |
28 | 98 | public function setReadOnly(bool $state = true): static |
29 | 99 | { |
30 | 100 | $this->readOnly = $state; |
@@ -79,4 +149,12 @@ public function hasHook(string|\PropertyHookType $type): bool |
79 | 149 | { |
80 | 150 | return isset($this->hooks[$type]); |
81 | 151 | } |
| 152 | + |
| 153 | + |
| 154 | + private function checkMode(string $mode): string |
| 155 | + { |
| 156 | + return $mode === Modifier::Set || $mode === Modifier::Get |
| 157 | + ? $mode |
| 158 | + : throw new Nette\InvalidArgumentException('Argument must be set|get.'); |
| 159 | + } |
82 | 160 | } |
0 commit comments