Skip to content

Commit

Permalink
Merge pull request #36 from 21TORR/enumvalue
Browse files Browse the repository at this point in the history
  • Loading branch information
keichinger authored Mar 13, 2024
2 parents 8690ee2 + 3130252 commit c69e9dd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

* (deprecation) Deprecate passing a `bool` to the constructor of `ApiResponse`.
* (feature) Enforce passing an explicit status code to the constructor of `ApiReponse`.
* (feature) Add `EnumValue` helper.

3.0.2
=====
Expand Down
27 changes: 27 additions & 0 deletions src/Helper/EnumValue.php
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;
}
}
34 changes: 34 additions & 0 deletions tests/Helper/EnumValueTest.php
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));
}
}

0 comments on commit c69e9dd

Please sign in to comment.