-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from 21TORR/enumvalue
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Torr\Rad\Helper; | ||
|
||
abstract class EnumValue | ||
{ | ||
/** | ||
* Helper to resolve an enum / string value to a string. | ||
*/ | ||
public static function strict (\BackedEnum|string $value) : string | ||
{ | ||
return $value instanceof \BackedEnum | ||
? (string) $value->value | ||
: $value; | ||
} | ||
|
||
|
||
/** | ||
* Helper to resolve an enum / string / null value to a ?string. | ||
*/ | ||
public static function get (\BackedEnum|string|null $value) : ?string | ||
{ | ||
return null !== $value | ||
? self::strict($value) | ||
: $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tests\Torr\Rad\Helper; | ||
|
||
use Torr\Rad\Helper\EnumValue; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
enum TestEnum : string | ||
{ | ||
case A = "a"; | ||
} | ||
|
||
/** | ||
*/ | ||
class EnumValueTest extends TestCase | ||
{ | ||
/** | ||
* | ||
*/ | ||
public function provideTransform () : iterable | ||
{ | ||
yield [null, null]; | ||
yield ["test", "test"]; | ||
yield ["a", TestEnum::A]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideTransform | ||
*/ | ||
public function testTransform (?string $expected, mixed $value) : void | ||
{ | ||
self::assertSame($expected, EnumValue::get($value)); | ||
} | ||
} |