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