Skip to content

Commit

Permalink
adds mapper method
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Feb 16, 2022
1 parent 4ccd504 commit af9a07e
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 25 additions & 1 deletion docs/mappers.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ enum YourEnum {
case NOT_MAPPED;

}
```

```php
use Henzeb\Enumhancer\Contracts\Mapper;

class YourMapper extends Mapper {
Expand Down Expand Up @@ -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:

Expand All @@ -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();
}
}
```
2 changes: 1 addition & 1 deletion docs/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand Down
19 changes: 16 additions & 3 deletions src/Concerns/Mappers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Reporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 3 additions & 2 deletions src/Laravel/Reporters/LaravelLogReporter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Henzeb\Enumhancer\Laravel\Reporters;

use BackedEnum;
Expand All @@ -9,11 +10,11 @@
class LaravelLogReporter implements Reporter
{

public function report(string $enum, string $key, ?BackedEnum $context): void
public function report(string $enum, ?string $key, ?BackedEnum $context): void
{
Log::warning(
class_basename($enum)
. ' does not have \'' . $key . '\'',
. ($key ? ' does not have \'' . $key . '\'' : ': A null value was passed'),
array_filter([
'class' => class_basename($enum),
'key' => $key,
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/EnhancedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Henzeb\Enumhancer\Tests\Fixtures;

use Henzeb\Enumhancer\Concerns\Enhancers;
use Henzeb\Enumhancer\Contracts\Mapper;

enum EnhancedEnum: string
{
Expand All @@ -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
];
}
};
}
}
60 changes: 60 additions & 0 deletions tests/Unit/Concerns/MappersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -99,6 +137,15 @@ public function testMakeArrayShouldWorkWitMapper()
);
}


public function testMakeArrayShouldMapWithoutMapperGiven()
{
$this->assertEquals(
[EnhancedEnum::ENUM],
EnhancedEnum::MakeArray(['anotherMappedEnum'])
);
}

public function testMakeArrayShouldThrowErrorWitMapper()
{
$this->expectError();
Expand All @@ -113,11 +160,24 @@ public function testTryMakeArrayShouldWorkWithoutMapper()
);
}

public function testTryMakeArrayShouldNotMapWhenNull()
{
$this->assertEquals([], EnhancedEnum::tryMakeArray([null], $this->getMapper()));
}

public function testTryMakeArrayShouldWorkWitMapper()
{
$this->assertEquals(
[EnhancedEnum::ENUM],
EnhancedEnum::tryMakeArray(['mappedEnum','DoesNotExist'], $this->getMapper())
);
}

public function testTryMakeArrayShouldMapWithoutMapperGiven()
{
$this->assertEquals(
[EnhancedEnum::ENUM],
EnhancedEnum::TryMakeArray(['anotherMappedEnum'])
);
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Concerns/ReportersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Helpers/EnumReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit af9a07e

Please sign in to comment.