This repository was archived by the owner on Dec 26, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +72
-0
lines changed Expand file tree Collapse file tree 4 files changed +72
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ Enumerated type for PHP by [Petr Knap].
7
7
* [ Usage of php-enum] ( #usage-of-php-enum )
8
8
* [ Enum declaration] ( #enum-declaration )
9
9
* [ Enum usage] ( #enum-usage )
10
+ * [ Tips & Tricks] ( #tips--tricks )
10
11
* [ How to install] ( #how-to-install )
11
12
12
13
@@ -131,6 +132,29 @@ if (date('w') == DayOfWeekEnum::FRIDAY()->getValue()) {
131
132
}
132
133
```
133
134
135
+ ### Tips & Tricks
136
+
137
+ Enum is capable to carry any data type as values, including another enum instance.
138
+
139
+ ``` php
140
+ class MixedValues extends \PetrKnap\Php\Enum\Enum
141
+ {
142
+ protected function members()
143
+ {
144
+ return array(
145
+ "null" => null,
146
+ "boolean" => true,
147
+ "integer" => 1,
148
+ "float" => 1.0,
149
+ "string" => "s",
150
+ "array" => array(),
151
+ "object" => new \stdClass(),
152
+ "callable" => function() {}
153
+ );
154
+ }
155
+ }
156
+ ```
157
+
134
158
135
159
## How to install
136
160
Original file line number Diff line number Diff line change 5
5
use PetrKnap \Php \Enum \Enum ;
6
6
use PetrKnap \Php \Enum \Exception \EnumException ;
7
7
use PetrKnap \Php \Enum \Exception \EnumNotFoundException ;
8
+ use PetrKnap \Php \Enum \Test \EnumTest \MixedValues ;
8
9
use PetrKnap \Php \Enum \Test \EnumTest \MyBoolean ;
9
10
10
11
class EnumTest extends \PHPUnit_Framework_TestCase
@@ -92,4 +93,23 @@ public function dataToStringWorks()
92
93
array (MyBoolean::MY_FALSE (), MyBoolean::getClass () . "::MY_FALSE " )
93
94
);
94
95
}
96
+
97
+ /**
98
+ * @dataProvider dataMixedValuesAreSupported
99
+ * @param MixedValues $enum
100
+ * @param string $expectedDataType
101
+ */
102
+ public function testMixedValuesAreSupported (MixedValues $ enum , $ expectedDataType )
103
+ {
104
+ $ this ->assertInternalType ($ expectedDataType , $ enum ->getValue ());
105
+ }
106
+
107
+ public function dataMixedValuesAreSupported ()
108
+ {
109
+ $ data = array ();
110
+ foreach (MixedValues::getMembers () as $ name => $ ignored ) {
111
+ $ data [] = array (MixedValues::__callStatic ($ name , array ()), $ name );
112
+ }
113
+ return $ data ;
114
+ }
95
115
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace PetrKnap \Php \Enum \Test \EnumTest ;
4
+
5
+ use PetrKnap \Php \Enum \Enum ;
6
+
7
+ class MixedValues extends Enum
8
+ {
9
+ /**
10
+ * @inheritdoc
11
+ */
12
+ protected function members ()
13
+ {
14
+ return array (
15
+ "null " => null ,
16
+ "boolean " => true ,
17
+ "integer " => 1 ,
18
+ "float " => 1.0 ,
19
+ "string " => "s " ,
20
+ "array " => array (),
21
+ "object " => new \stdClass (),
22
+ "callable " => function () {}
23
+ );
24
+ }
25
+ }
Original file line number Diff line number Diff line change 10
10
*/
11
11
class MyBoolean extends Enum
12
12
{
13
+ /**
14
+ * @inheritdoc
15
+ */
13
16
protected function members ()
14
17
{
15
18
return array (
You can’t perform that action at this time.
0 commit comments