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