Skip to content

Commit

Permalink
added From
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Feb 25, 2022
1 parent 35dc243 commit 189a8fc
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 30 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/constructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
37 changes: 37 additions & 0 deletions docs/from.md
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.
18 changes: 18 additions & 0 deletions src/Concerns/From.php
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

namespace Henzeb\Enumhancer\Tests\Fixtures;

use Henzeb\Enumhancer\Concerns\From;
use Henzeb\Enumhancer\Concerns\Comparison;
use Henzeb\Enumhancer\Concerns\Mappers;
use Henzeb\Enumhancer\Concerns\Constructor;

/**
* @method static self CALLABLE()
*/
enum ConstructableEnum
enum ConstructableNonBackedEnum
{
use Constructor, Mappers, Comparison;
use Constructor, Mappers, Comparison, From;

case CALLABLE;
}
1 change: 0 additions & 1 deletion tests/Fixtures/EnhancedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ protected static function mapper(): ?Mapper
{
return new class extends Mapper
{

protected function mappable(): array
{
return [
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Concerns/ConstructorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Henzeb\Enumhancer\Tests\Unit\Concerns;

use Henzeb\Enumhancer\Tests\Fixtures\IntBackedStaticCallableEnum;
use Henzeb\Enumhancer\Tests\Fixtures\ConstructableEnum;
use Henzeb\Enumhancer\Tests\Fixtures\ConstructableNonBackedEnum;
use Henzeb\Enumhancer\Tests\Fixtures\StringBackedStaticCallableEnum;
use PHPUnit\Framework\TestCase;

Expand All @@ -13,15 +13,15 @@ class ConstructorTest extends TestCase
public function testShouldGetEnumUsingStaticCall(): void
{
$this->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
Expand Down
40 changes: 40 additions & 0 deletions tests/Unit/Concerns/FromTest.php
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')
);
}
}
38 changes: 19 additions & 19 deletions tests/Unit/Helpers/EnumPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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],

];
}
Expand Down Expand Up @@ -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'],
];
}
Expand All @@ -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()
Expand Down Expand Up @@ -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'));
}
}

0 comments on commit 189a8fc

Please sign in to comment.