diff --git a/CHANGELOG.md b/CHANGELOG.md index bd67582..72fd591 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,15 @@ All notable changes to `Enumhancer` will be documented in this file -## 1.0.2 - 2022 02-16 +## 1.1.0 - 2022-02-25 + +- added From. Useful for situations where you need them with non-backed enums + +## 1.0.2 - 2022-02-16 - Bugfix: Constructor did not use internal mapper -## 1.0.1 - 2022 02-16 +## 1.0.1 - 2022-02-16 - You can now define a mapper in a method - When you use an empty string or null in mappable, it will return null now diff --git a/README.md b/README.md index c8ef36e..2fa1486 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ something for you. This package currently supports the following: - Comparison +- From (for non-backed enums) - Ability to make from enum-name (Make methods) - Labels - Mappers @@ -29,7 +30,7 @@ composer require henzeb/enumhancer ## Usage You can simply add the `Enhancers` trait to your `enum` in -order to use all functionality of this package. All functionality +order to use almost all functionality of this package. All functionality should work with `unit` enums as well as `backed` enums'. ```php @@ -50,6 +51,7 @@ implemented his own version of `Makers` and `Reporters`. ### Functionality - [Constructor](docs/constructor.md) - [Comparison](docs/comparison.md) +- [From](docs/from.md) - [Labels](docs/labels.md) - [Makers](docs/makers.md) - [Mappers](docs/mappers.md) diff --git a/docs/constructor.md b/docs/constructor.md index c3e8337..8b524db 100644 --- a/docs/constructor.md +++ b/docs/constructor.md @@ -30,5 +30,5 @@ YourEnum::CALLABLE(); // will return YourEnum::CALLABLE; Note: Under the hood it is using `__callStatic`, so it may give some unpredicted behavior when calling a method that doesn't exist. -Note: This is the only one that is not enabled by default when +Note: This trait is not enabled by default when using the `Enumhancer` trait. diff --git a/docs/from.md b/docs/from.md new file mode 100644 index 0000000..7c2aaaf --- /dev/null +++ b/docs/from.md @@ -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::MY_ENUM; +YourEnum::from('MY_ENUM'); // will return YourEnum::MY_ENUM; +YourEnum::from('CALLABLE'); // will throw error + +YourEnum::tryFrom('my_enum'); // will return YourEnum::MY_ENUM; +YourEnum::tryFrom('MY_ENUM'); // will return YourEnum::MY_ENUM; +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. diff --git a/src/Concerns/From.php b/src/Concerns/From.php new file mode 100644 index 0000000..b9fac37 --- /dev/null +++ b/src/Concerns/From.php @@ -0,0 +1,18 @@ +assertEquals( - ConstructableEnum::CALLABLE, - ConstructableEnum::CALLABLE() + ConstructableNonBackedEnum::CALLABLE, + ConstructableNonBackedEnum::CALLABLE() ); } public function testShouldFailUsingStaticCallToUnknownEnum(): void { $this->expectError(); - ConstructableEnum::CANNOT_CALL(); + ConstructableNonBackedEnum::CANNOT_CALL(); } public function testShouldGetStringBackedEnumByName(): void diff --git a/tests/Unit/Concerns/FromTest.php b/tests/Unit/Concerns/FromTest.php new file mode 100644 index 0000000..9859292 --- /dev/null +++ b/tests/Unit/Concerns/FromTest.php @@ -0,0 +1,40 @@ +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') + ); + } +} diff --git a/tests/Unit/Helpers/EnumPropertiesTest.php b/tests/Unit/Helpers/EnumPropertiesTest.php index 7f2618f..2bf97e3 100644 --- a/tests/Unit/Helpers/EnumPropertiesTest.php +++ b/tests/Unit/Helpers/EnumPropertiesTest.php @@ -4,7 +4,7 @@ use Henzeb\Enumhancer\Helpers\EnumProperties; -use Henzeb\Enumhancer\Tests\Fixtures\ConstructableEnum; +use Henzeb\Enumhancer\Tests\Fixtures\ConstructableNonBackedEnum; use Henzeb\Enumhancer\Tests\Fixtures\EnhancedEnum; use Henzeb\Enumhancer\Tests\Fixtures\StringBackedMakersEnum; use PHPUnit\Framework\TestCase; @@ -17,20 +17,20 @@ protected function setUp(): void { EnumProperties::clearGlobal(); EnumProperties::clear(StringBackedMakersEnum::class); - EnumProperties::clear(ConstructableEnum::class); + EnumProperties::clear(ConstructableNonBackedEnum::class); EnumProperties::clear(EnhancedEnum::class); } public function providesTestcasesForStoreProperty(): array { return [ - 'boolean' => ['property', true, true, ConstructableEnum::class], - 'object' => ['anObject', new stdClass(), new stdClass(), ConstructableEnum::class], - 'string' => ['aString', 'A String', 'A String', ConstructableEnum::class], - 'enum' => ['anEnum', ConstructableEnum::CALLABLE, ConstructableEnum::CALLABLE, ConstructableEnum::class], - 'callable' => ['property', fn() => 'true', fn() => 'true', ConstructableEnum::class], + 'boolean' => ['property', true, true, ConstructableNonBackedEnum::class], + 'object' => ['anObject', new stdClass(), new stdClass(), ConstructableNonBackedEnum::class], + 'string' => ['aString', 'A String', 'A String', ConstructableNonBackedEnum::class], + 'enum' => ['anEnum', ConstructableNonBackedEnum::CALLABLE, ConstructableNonBackedEnum::CALLABLE, ConstructableNonBackedEnum::class], + 'callable' => ['property', fn() => 'true', fn() => 'true', ConstructableNonBackedEnum::class], - 'another-enum-that-tries-to-get' => ['anotherProperty', true, null, ConstructableEnum::class, StringBackedMakersEnum::class], + 'another-enum-that-tries-to-get' => ['anotherProperty', true, null, ConstructableNonBackedEnum::class, StringBackedMakersEnum::class], ]; } @@ -62,7 +62,7 @@ public function providesTestcasesForStorePropertyGlobally(): array 'boolean' => ['property', true, true], 'object' => ['anObject', new stdClass(), new stdClass()], 'string' => ['aString', 'A String', 'A String'], - 'enum' => ['anEnum', ConstructableEnum::CALLABLE, ConstructableEnum::CALLABLE], + 'enum' => ['anEnum', ConstructableNonBackedEnum::CALLABLE, ConstructableNonBackedEnum::CALLABLE], 'callable' => ['property', fn() => 'true', fn() => 'true'], ]; } @@ -84,24 +84,24 @@ public function testStoreProperty(string $key, mixed $value, mixed $expectedValu public function testClearsProperties() { - EnumProperties::store(ConstructableEnum::class, 'property', 'a value'); + EnumProperties::store(ConstructableNonBackedEnum::class, 'property', 'a value'); EnumProperties::store(StringBackedMakersEnum::class, 'property', 'a value'); - EnumProperties::clear(ConstructableEnum::class); + EnumProperties::clear(ConstructableNonBackedEnum::class); - $this->assertNull(EnumProperties::get(ConstructableEnum::class, 'property')); + $this->assertNull(EnumProperties::get(ConstructableNonBackedEnum::class, 'property')); $this->assertEquals('a value', EnumProperties::get(StringBackedMakersEnum::class, 'property')); } public function testClearsSingleProperty() { - EnumProperties::store(ConstructableEnum::class, 'property', 'a value'); - EnumProperties::store(ConstructableEnum::class, 'property2', 'a value'); + EnumProperties::store(ConstructableNonBackedEnum::class, 'property', 'a value'); + EnumProperties::store(ConstructableNonBackedEnum::class, 'property2', 'a value'); - EnumProperties::clear(ConstructableEnum::class, 'property'); + EnumProperties::clear(ConstructableNonBackedEnum::class, 'property'); - $this->assertNull(EnumProperties::get(ConstructableEnum::class, 'property')); - $this->assertEquals('a value', EnumProperties::get(ConstructableEnum::class, 'property2')); + $this->assertNull(EnumProperties::get(ConstructableNonBackedEnum::class, 'property')); + $this->assertEquals('a value', EnumProperties::get(ConstructableNonBackedEnum::class, 'property2')); } public function testClearsGlobal() @@ -135,8 +135,8 @@ public function testStoreGlobally(string $key, mixed $value, mixed $expectedValu public function testIfLocalPropertyOverridesGlobalProperty() { EnumProperties::global('property', 'global value'); - EnumProperties::store(ConstructableEnum::class, 'property', 'local value'); + EnumProperties::store(ConstructableNonBackedEnum::class, 'property', 'local value'); - $this->assertEquals('local value', EnumProperties::get(ConstructableEnum::class, 'property')); + $this->assertEquals('local value', EnumProperties::get(ConstructableNonBackedEnum::class, 'property')); } }