1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Part of SplTypes package. |
5
|
|
|
* |
6
|
|
|
* (c) Adrien Loyant <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ducks\Component\SplTypes; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait used for magic accessor on SplEnum class |
16
|
|
|
* |
17
|
|
|
* @template T |
18
|
|
|
* |
19
|
|
|
* @phpstan-require-extends SplEnum |
20
|
|
|
* |
21
|
|
|
* @psalm-api |
22
|
|
|
*/ |
23
|
|
|
trait SplEnumAccessorsTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Return the case-sensitive name of the case class itself. |
27
|
|
|
* |
28
|
|
|
* @param string $name |
29
|
|
|
* |
30
|
|
|
* @return mixed |
31
|
|
|
*/ |
32
|
|
|
final public function __get(string $name) |
33
|
|
|
{ |
34
|
|
|
switch ($name) { |
35
|
|
|
case 'name': |
36
|
|
|
$result = \array_search($this->__default, $this->getConstList()); |
|
|
|
|
37
|
|
|
break; |
38
|
|
|
|
39
|
|
|
case 'value': |
40
|
|
|
$result = $this->__default; |
41
|
|
|
break; |
42
|
|
|
|
43
|
|
|
default: |
44
|
|
|
\trigger_error('Undefined property: ' . static::class . '::$' . $name, E_USER_WARNING); |
45
|
|
|
break; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $result ?? null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Writing data to inaccessible (protected or private) or non-existing properties. |
53
|
|
|
* |
54
|
|
|
* @param string $name |
55
|
|
|
* @param mixed $value |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
* |
59
|
|
|
* @throws \Error |
60
|
|
|
* |
61
|
|
|
* @phpstan-ignore-next-line |
62
|
|
|
* |
63
|
|
|
* @psalm-suppress MissingParamType |
64
|
|
|
* @psalm-suppress UnusedParam |
65
|
|
|
*/ |
66
|
|
|
final public function __set(string $name, $value): void |
67
|
|
|
{ |
68
|
|
|
// Fast return |
69
|
|
|
if ('name' === $name || 'value' === $name) { |
70
|
|
|
throw new \Error('Cannot modify readonly property ' . static::class . '::$' . $name); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
throw new \Error('Cannot create dynamic property ' . static::class . '::$' . $name); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Triggered by calling isset() or empty() on inaccessible or non-existing properties. |
78
|
|
|
* |
79
|
|
|
* @param string $name |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
final public function __isset(string $name): bool |
84
|
|
|
{ |
85
|
|
|
switch ($name) { |
86
|
|
|
case 'name': |
87
|
|
|
case 'value': |
88
|
|
|
$result = true; |
89
|
|
|
break; |
90
|
|
|
|
91
|
|
|
default: |
92
|
|
|
$result = isset($this->$name); |
93
|
|
|
break; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $result; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Invoked when unset() is used on inaccessible or non-existing properties. |
101
|
|
|
* |
102
|
|
|
* @param string $name |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
final public function __unset(string $name): void |
107
|
|
|
{ |
108
|
|
|
// Fast return |
109
|
|
|
if (!\in_array($name, ['name', 'value'])) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
throw new \Error('Cannot unset readonly property ' . static::class . '::$' . $name); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|