-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
131 additions
and
30 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
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,37 @@ | ||
# From | ||
|
||
The methods `from` and `tryFrom` do not exist on non-backed enums. When you have | ||
a non-backed enum where the string value is exactly the same as it's key, but | ||
you need `from` or `tryFrom` because of some library (like Laravel's validation), | ||
this might be useful. | ||
|
||
## Usage | ||
|
||
```php | ||
use Henzeb\Enumhancer\Concerns\From; | ||
|
||
enum yourEnum { | ||
use From; | ||
|
||
case MY_ENUM; | ||
|
||
} | ||
``` | ||
|
||
### Examples | ||
|
||
```php | ||
YourEnum::from('my_enum'); // will return YourEnum::CALLABLE; | ||
YourEnum::from('MY_ENUM'); // will return YourEnum::CALLABLE; | ||
YourEnum::from('CALLABLE'); // will throw error | ||
|
||
YourEnum::tryFrom('my_enum'); // will return YourEnum::CALLABLE; | ||
YourEnum::tryFrom('MY_ENUM'); // will return YourEnum::CALLABLE; | ||
YourEnum::tryFrom('DOESNOTEXIST'); // will return null | ||
``` | ||
Note: Under the hood it uses the Makers functionality, so you can use lower- | ||
and uppercase names, and you can also use mappers that you have defined using | ||
the `mappers` method. | ||
|
||
Note: This trait is not enabled by default when | ||
using the `Enumhancer` trait. |
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,18 @@ | ||
<?php | ||
|
||
namespace Henzeb\Enumhancer\Concerns; | ||
|
||
use Henzeb\Enumhancer\Helpers\EnumMakers; | ||
|
||
trait From | ||
{ | ||
public static function from(string $key): static | ||
{ | ||
return EnumMakers::make(static::class, $key); | ||
} | ||
|
||
public static function tryFrom(string $key): ?static | ||
{ | ||
return EnumMakers::tryMake(static::class, $key); | ||
} | ||
} |
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
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,40 @@ | ||
<?php | ||
|
||
namespace Henzeb\Enumhancer\Tests\Unit\Concerns; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Henzeb\Enumhancer\Tests\Fixtures\ConstructableNonBackedEnum; | ||
|
||
|
||
class FromTest extends TestCase | ||
{ | ||
function testNonBackedEnumCanCallFrom(): void | ||
{ | ||
$this->assertEquals( | ||
ConstructableNonBackedEnum::CALLABLE, | ||
ConstructableNonBackedEnum::from('callable') | ||
); | ||
} | ||
|
||
function testNonBackedEnumCanCallFromAndFail(): void | ||
{ | ||
$this->expectError(); | ||
|
||
ConstructableNonBackedEnum::from('doesnotexist'); | ||
} | ||
|
||
function testNonBackedEnumCanCallTryFrom(): void | ||
{ | ||
$this->assertEquals( | ||
ConstructableNonBackedEnum::CALLABLE, | ||
ConstructableNonBackedEnum::tryFrom('callable') | ||
); | ||
} | ||
|
||
function testTryFromShouldReturnNull(): void | ||
{ | ||
$this->assertNull( | ||
ConstructableNonBackedEnum::tryFrom('doesNotExist') | ||
); | ||
} | ||
} |
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