You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The package provide abstract class `Enum` that intended to create
10
-
[enumerated objects](https://en.wikipedia.org/wiki/Enumerated_type) with support [extra data](#extradata) and
11
-
auxiliary static functions [`toValues()`](#toList), [`toObjects()`](#toObjects) and [`isValid()`](#isValid).
9
+
The package implement ideas from [RFC Enumerations](https://wiki.php.net/rfc/enumerations) and provide abstract class `Enum` that intended to create
10
+
[enumerated objects](https://en.wikipedia.org/wiki/Enumerated_type) with support [extra data](#extradata) and auxiliary static functions [`values()`](#values), [`cases()`](#cases) and [`isValid()`](#isValid).
12
11
13
12
## Requirements
14
13
@@ -36,9 +35,9 @@ use Vjik\Enum\Enum;
36
35
*/
37
36
final class Status extends Enum
38
37
{
39
-
public const NEW = 'new';
40
-
public const PROCESS = 'process';
41
-
public const DONE = 'done';
38
+
private const NEW = 'new';
39
+
private const PROCESS = 'process';
40
+
private const DONE = 'done';
42
41
}
43
42
```
44
43
@@ -50,6 +49,17 @@ final class Status extends Enum
50
49
$process = Status::from('process');
51
50
```
52
51
52
+
On create object with invalid value throws `ValueError`.
53
+
54
+
#### By static method `tryFrom()`
55
+
56
+
```php
57
+
$process = Status::tryFrom('process'); // Status object with value "process"
58
+
$process = Status::tryFrom('not-exists'); // null
59
+
```
60
+
61
+
On create object with invalid value returns `null`.
62
+
53
63
#### By static method with a name identical to the constant name
54
64
55
65
Static methods are automatically implemented to provide quick access to an enum value.
@@ -58,9 +68,17 @@ Static methods are automatically implemented to provide quick access to an enum
58
68
$process = Status::PROCESS();
59
69
```
60
70
71
+
### Getting value and name
72
+
73
+
```php
74
+
Status::DONE()->getName(); // DONE
75
+
Status::DONE()->getValue(); // done
76
+
```
77
+
61
78
### <aname="extradata"></a>Class with extra data
62
79
63
-
Set data in the protected static function `data()` and create getters using the protected method `getPropertyValue()`:
80
+
Set data in the protected static function `data()` and create getters using the protected method `getPropertyValue()`.
81
+
Also you can create getter using protected method `match()`.
64
82
65
83
```php
66
84
use Vjik\Enum\Enum;
@@ -71,8 +89,8 @@ use Vjik\Enum\Enum;
71
89
*/
72
90
final class Action extends Enum
73
91
{
74
-
public const CREATE = 1;
75
-
public const UPDATE = 2;
92
+
private const CREATE = 1;
93
+
private const UPDATE = 2;
76
94
77
95
protected static function data(): array
78
96
{
@@ -91,33 +109,50 @@ final class Action extends Enum
91
109
/** @var string */
92
110
return $this->getPropertyValue('tip');
93
111
}
112
+
113
+
public function getColor(): string
114
+
{
115
+
return $this->match([
116
+
self::CREATE => 'red',
117
+
self::UPDATE => 'blue',
118
+
]);
119
+
}
120
+
121
+
public function getCode(): int
122
+
{
123
+
return $this->match([
124
+
self::CREATE => 1,
125
+
], 99);
126
+
}
94
127
}
95
128
```
96
129
97
130
Usage:
98
131
99
132
```php
100
133
echo Action::CREATE()->getTip();
134
+
echo Action::CREATE()->getColor();
135
+
echo Action::CREATE()->getCode();
101
136
```
102
137
103
138
### Auxiliary static functions
104
139
105
-
#### <aname="toValues"></a> List of values `toValues()`
140
+
#### <aname="values"></a> List of values `values()`
106
141
107
-
Returns array of pairs constant names and values.
142
+
Returns list of values.
108
143
109
144
```php
110
-
// ['CREATE' => 1, 'UPDATE' => 2]
111
-
Action::toValues();
145
+
// [1, 2]
146
+
Action::values();
112
147
```
113
148
114
-
#### <aname="toObjects"></a> List of objects `toObjects()`
149
+
#### <aname="cases"></a> List of objects `cases()`
115
150
116
-
Returns array of pairs constant names and objects:
0 commit comments