Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit 3bd9828

Browse files
author
Petr Knap
committed
Added mixed values support test and example
2 parents 6bdf67c + 33cf31d commit 3bd9828

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Enumerated type for PHP by [Petr Knap].
77
* [Usage of php-enum](#usage-of-php-enum)
88
* [Enum declaration](#enum-declaration)
99
* [Enum usage](#enum-usage)
10+
* [Tips & Tricks](#tips--tricks)
1011
* [How to install](#how-to-install)
1112

1213

@@ -131,6 +132,29 @@ if (date('w') == DayOfWeekEnum::FRIDAY()->getValue()) {
131132
}
132133
```
133134

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+
134158

135159
## How to install
136160

tests/Enum/EnumTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PetrKnap\Php\Enum\Enum;
66
use PetrKnap\Php\Enum\Exception\EnumException;
77
use PetrKnap\Php\Enum\Exception\EnumNotFoundException;
8+
use PetrKnap\Php\Enum\Test\EnumTest\MixedValues;
89
use PetrKnap\Php\Enum\Test\EnumTest\MyBoolean;
910

1011
class EnumTest extends \PHPUnit_Framework_TestCase
@@ -92,4 +93,23 @@ public function dataToStringWorks()
9293
array(MyBoolean::MY_FALSE(), MyBoolean::getClass() . "::MY_FALSE")
9394
);
9495
}
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+
}
95115
}

tests/Enum/EnumTest/MixedValues.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

tests/Enum/EnumTest/MyBoolean.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
*/
1111
class MyBoolean extends Enum
1212
{
13+
/**
14+
* @inheritdoc
15+
*/
1316
protected function members()
1417
{
1518
return array(

0 commit comments

Comments
 (0)