diff --git a/CHANGELOG.md b/CHANGELOG.md index cf6bec6..1c3b098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to `Enumhancer` will be documented in this file +## 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. + ## 1.0.0 - 2022-02-15 - initial release diff --git a/docs/mappers.md b/docs/mappers.md index ea8f927..059f352 100644 --- a/docs/mappers.md +++ b/docs/mappers.md @@ -23,6 +23,9 @@ enum YourEnum { case NOT_MAPPED; } +``` + +```php use Henzeb\Enumhancer\Contracts\Mapper; class YourMapper extends Mapper { @@ -66,6 +69,7 @@ YourEnum::makeOrReport(['unknown', 'NOT_MAPPED'], new YourMapper()); // will ret Note: See for the `makeOrReport` method: [Reporters](reporters.md) ### Shared Mapper + You can also use one `Mapper` for multiple enums. Just use the FQCN of the enum as a key in your array, like below: @@ -87,4 +91,24 @@ class YourMapper extends Mapper { ``` And then use the commands as shown in the `example` section. -Note: You can also mix them up, if some enums may share a value or a name. +## The mapper method + +In case you don't want to add the mapper all the time, you can also specify a `mapper` method that returns your mapper. + +```php +use Henzeb\Enumhancer\Concerns\Mappers; +use Henzeb\Enumhancer\Contracts\Mapper; + +enum YourEnum { + use Mappers; + + case ENUM; + case NO_LABEL; + case NOT_MAPPED; + + protected static mapper(): ?Mapper + { + return new YourMapper(); + } +} +``` diff --git a/docs/reporters.md b/docs/reporters.md index e1c828d..efa1edd 100644 --- a/docs/reporters.md +++ b/docs/reporters.md @@ -19,7 +19,7 @@ use Henzeb\Enumhancer\Contracts\Reporter; class YourReporter implements Reporter { - public function report(string $enum, string $key, ?BackedEnum $context) : void + public function report(string $enum, ?string $key, ?BackedEnum $context) : void { // TODO: Implement report() method. } diff --git a/src/Concerns/Mappers.php b/src/Concerns/Mappers.php index f8598a0..264e787 100644 --- a/src/Concerns/Mappers.php +++ b/src/Concerns/Mappers.php @@ -10,14 +10,27 @@ trait Mappers { - final protected static function reporter():?Reporter + protected static function reporter():?Reporter { return EnumReporter::get(); } - private static function map(string|int|null $value, Mapper|string $mapper = null): string + protected static function mapper(): ?Mapper { - return ($mapper) ? + return null; + } + + private static function map(string|int|null $value, Mapper|string $mapper = null): ?string + { + if(null === $value) { + return null; + } + + $value = ($mapper) ? + $mapper->map($value, static::class) ?? $value + : $value; + + return ($mapper = self::mapper()) ? $mapper->map($value, static::class) ?? $value : $value; } diff --git a/src/Contracts/Reporter.php b/src/Contracts/Reporter.php index b78be7b..531460c 100644 --- a/src/Contracts/Reporter.php +++ b/src/Contracts/Reporter.php @@ -5,5 +5,5 @@ interface Reporter { - public function report(string $enum, string $key, ?BackedEnum $context): void; + public function report(string $enum, ?string $key, ?BackedEnum $context): void; } diff --git a/src/Laravel/Reporters/LaravelLogReporter.php b/src/Laravel/Reporters/LaravelLogReporter.php index f536f15..037ddc9 100644 --- a/src/Laravel/Reporters/LaravelLogReporter.php +++ b/src/Laravel/Reporters/LaravelLogReporter.php @@ -1,4 +1,5 @@ class_basename($enum), 'key' => $key, diff --git a/tests/Fixtures/EnhancedEnum.php b/tests/Fixtures/EnhancedEnum.php index 8c62b14..97bf16a 100644 --- a/tests/Fixtures/EnhancedEnum.php +++ b/tests/Fixtures/EnhancedEnum.php @@ -3,6 +3,7 @@ namespace Henzeb\Enumhancer\Tests\Fixtures; use Henzeb\Enumhancer\Concerns\Enhancers; +use Henzeb\Enumhancer\Contracts\Mapper; enum EnhancedEnum: string { @@ -17,4 +18,18 @@ protected function labels(): array 'ENUM'=>'My label' ]; } + + protected static function mapper(): ?Mapper + { + return new class extends Mapper + { + + protected function mappable(): array + { + return [ + 'anotherMappedEnum' => EnhancedEnum::ENUM + ]; + } + }; + } } diff --git a/tests/Unit/Concerns/MappersTest.php b/tests/Unit/Concerns/MappersTest.php index cd87576..28d6e91 100644 --- a/tests/Unit/Concerns/MappersTest.php +++ b/tests/Unit/Concerns/MappersTest.php @@ -45,6 +45,20 @@ public function testMakeShouldMap() ); } + public function testMakeShouldNotMapWhenNull() + { + $this->expectError(); + EnhancedEnum::make(null, $this->getMapper()); + } + + public function testMakeShouldMapWithoutMapperGiven() + { + $this->assertEquals( + EnhancedEnum::ENUM, + EnhancedEnum::make('anotherMappedEnum') + ); + } + public function testMakeShouldErrorWithMap() { $this->expectError(); @@ -64,6 +78,14 @@ public function testTryMakeShouldReturnNullWithoutMapper() $this->assertNull(EnhancedEnum::tryMake('mappedEnum')); } + public function testTryMakeShouldNotMapWhenNull() + { + + $this->assertNull( + EnhancedEnum::tryMake(null, $this->getMapper()) + ); + } + public function testTryMakeShouldMap() { $this->assertEquals( @@ -72,11 +94,27 @@ public function testTryMakeShouldMap() ); } + public function testTryMakeShouldMapWithoutMapperGiven() + { + $this->assertEquals( + EnhancedEnum::ENUM, + EnhancedEnum::tryMake('anotherMappedEnum') + ); + } + + public function testTryMakeShouldReturnNullWithMap() { $this->assertNull(EnhancedEnum::tryMake('not existing', $this->getMapper())); } + + public function testMakeArrayShouldNotMapWhenNull() + { + $this->expectError(); + EnhancedEnum::makeArray([null], $this->getMapper()); + } + public function testMakeArrayShouldWorkWithoutMapper() { $this->assertEquals( @@ -99,6 +137,15 @@ public function testMakeArrayShouldWorkWitMapper() ); } + + public function testMakeArrayShouldMapWithoutMapperGiven() + { + $this->assertEquals( + [EnhancedEnum::ENUM], + EnhancedEnum::MakeArray(['anotherMappedEnum']) + ); + } + public function testMakeArrayShouldThrowErrorWitMapper() { $this->expectError(); @@ -113,6 +160,11 @@ public function testTryMakeArrayShouldWorkWithoutMapper() ); } + public function testTryMakeArrayShouldNotMapWhenNull() + { + $this->assertEquals([], EnhancedEnum::tryMakeArray([null], $this->getMapper())); + } + public function testTryMakeArrayShouldWorkWitMapper() { $this->assertEquals( @@ -120,4 +172,12 @@ public function testTryMakeArrayShouldWorkWitMapper() EnhancedEnum::tryMakeArray(['mappedEnum','DoesNotExist'], $this->getMapper()) ); } + + public function testTryMakeArrayShouldMapWithoutMapperGiven() + { + $this->assertEquals( + [EnhancedEnum::ENUM], + EnhancedEnum::TryMakeArray(['anotherMappedEnum']) + ); + } } diff --git a/tests/Unit/Concerns/ReportersTest.php b/tests/Unit/Concerns/ReportersTest.php index 278b7fe..b8d56d4 100644 --- a/tests/Unit/Concerns/ReportersTest.php +++ b/tests/Unit/Concerns/ReportersTest.php @@ -157,7 +157,7 @@ public function testReportWithContext(string $enum) { $reporter = new class implements Reporter { - public function report(string $enum, string $key, ?BackedEnum $context): void + public function report(string $enum, ?string $key, ?BackedEnum $context): void { enum_exists($context::class); } @@ -179,7 +179,7 @@ public function testMakeOrReportArrayWithContext(string $enum) { $reporter = new class implements Reporter { - public function report(string $enum, string $key, ?BackedEnum $context): void + public function report(string $enum, ?string $key, ?BackedEnum $context): void { enum_exists($context::class); } diff --git a/tests/Unit/Helpers/EnumReporterTest.php b/tests/Unit/Helpers/EnumReporterTest.php index 906f485..48e4b35 100644 --- a/tests/Unit/Helpers/EnumReporterTest.php +++ b/tests/Unit/Helpers/EnumReporterTest.php @@ -22,7 +22,7 @@ public function testSetReporter() { EnumReporter::set(null); $reporter = new class implements Reporter { - public function report(string $enum, string $key, ?BackedEnum $context): void {} + public function report(string $enum, ?string $key, ?BackedEnum $context): void {} }; EnumReporter::set($reporter);