Skip to content

Commit

Permalink
added Value
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Feb 26, 2022
1 parent 9c8505d commit 9e04602
Show file tree
Hide file tree
Showing 17 changed files with 212 additions and 119 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ something for you.

This package currently supports the following:

- Constructor (in case you're migrating from
[Spatie's PHP Enum](https://github.com/spatie/enum))
- Comparison
- From (for non-backed enums)
- Ability to make from enum-name (Make methods)
- From (for unit enums)
- Make (Ability to make from enum-name)
- Labels
- Mappers
- Reporting (Logging)
- Properties
- Constructor (in case you're migrating from
[Spatie's PHP Enum](https://github.com/spatie/enum))
- Reporting (Logging)
- Value


## Installation
Expand Down Expand Up @@ -57,6 +58,7 @@ implemented his own version of `Makers` and `Reporters`.
- [Mappers](docs/mappers.md)
- [Properties](docs/properties.md)
- [Reporters](docs/reporters.md)
- [Value](docs/value.md)

### Laravel
When you are installing this package into a laravel project, Enumhancer will
Expand Down
25 changes: 25 additions & 0 deletions docs/value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Value

When you have a unit enum, you have no value. You have to use the `name` value. But what if you
want to have the lowercase version of that name? That's where `Value` comes in.

## Usage

```php
use Henzeb\Enumhancer\Concerns\Value;

enum yourEnum {
use Value;

case MY_ENUM;

}
```

### Examples

```php
YourEnum::MY_ENUM->value(); // will return `my_enum`;
```

Note: When used with a string or int backed enum, this method will return it's actual value.
2 changes: 1 addition & 1 deletion src/Concerns/Enhancers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

trait Enhancers
{
use Comparison, Labels, Mappers, Properties;
use Comparison, Labels, Mappers, Properties, Value;
}
11 changes: 11 additions & 0 deletions src/Concerns/Value.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Henzeb\Enumhancer\Concerns;

trait Value
{
public function value(): string|int
{
return $this->value ?? strtolower($this->name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @method static self CALLABLE()
*/
enum ConstructableNonBackedEnum
enum ConstructableUnitEnum
{
use Constructor, Mappers, Comparison, From;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* @method static self anotherMappedEnum()
*/
enum EnhancedEnum: string
enum EnhancedBackedEnum: string
{
use Enhancers, Constructor;

Expand All @@ -30,7 +30,7 @@ protected static function mapper(): ?Mapper
protected function mappable(): array
{
return [
'anotherMappedEnum' => EnhancedEnum::ENUM
'anotherMappedEnum' => EnhancedBackedEnum::ENUM
];
}
};
Expand Down
18 changes: 18 additions & 0 deletions tests/Fixtures/EnhancedUnitEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Henzeb\Enumhancer\Tests\Fixtures;

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

/**
* @method static self anotherMappedEnum()
*/
enum EnhancedUnitEnum
{
use Enhancers, Constructor;

case ENUM;
case ANOTHER_ENUM;
}
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\ConstructableNonBackedEnum;
use Henzeb\Enumhancer\Tests\Fixtures\ConstructableUnitEnum;
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(
ConstructableNonBackedEnum::CALLABLE,
ConstructableNonBackedEnum::CALLABLE()
ConstructableUnitEnum::CALLABLE,
ConstructableUnitEnum::CALLABLE()
);
}

public function testShouldFailUsingStaticCallToUnknownEnum(): void
{
$this->expectError();
ConstructableNonBackedEnum::CANNOT_CALL();
ConstructableUnitEnum::CANNOT_CALL();
}

public function testShouldGetStringBackedEnumByName(): void
Expand Down
18 changes: 9 additions & 9 deletions tests/Unit/Concerns/FromTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@
namespace Henzeb\Enumhancer\Tests\Unit\Concerns;

use PHPUnit\Framework\TestCase;
use Henzeb\Enumhancer\Tests\Fixtures\ConstructableNonBackedEnum;
use Henzeb\Enumhancer\Tests\Fixtures\ConstructableUnitEnum;


class FromTest extends TestCase
{
function testNonBackedEnumCanCallFrom(): void
{
$this->assertEquals(
ConstructableNonBackedEnum::CALLABLE,
ConstructableNonBackedEnum::from('callable')
ConstructableUnitEnum::CALLABLE,
ConstructableUnitEnum::from('callable')
);
}

function testNonBackedEnumCanCallFromAndFail(): void
function testUnitEnumCanCallFromAndFail(): void
{
$this->expectError();

ConstructableNonBackedEnum::from('doesnotexist');
ConstructableUnitEnum::from('doesnotexist');
}

function testNonBackedEnumCanCallTryFrom(): void
function testUnitEnumCanCallTryFrom(): void
{
$this->assertEquals(
ConstructableNonBackedEnum::CALLABLE,
ConstructableNonBackedEnum::tryFrom('callable')
ConstructableUnitEnum::CALLABLE,
ConstructableUnitEnum::tryFrom('callable')
);
}

function testTryFromShouldReturnNull(): void
{
$this->assertNull(
ConstructableNonBackedEnum::tryFrom('doesNotExist')
ConstructableUnitEnum::tryFrom('doesNotExist')
);
}
}
6 changes: 3 additions & 3 deletions tests/Unit/Concerns/LabelsTest.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\Concerns\Labels;
use Henzeb\Enumhancer\Tests\Fixtures\EnhancedEnum;
use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum;
use Mockery;
use PHPUnit\Framework\TestCase;

Expand All @@ -20,11 +20,11 @@ public function testShouldGetNameWhenNoLabelsSpecifiedAtAll()

public function testShouldGetLabelByName()
{
$this->assertEquals('My label', EnhancedEnum::ENUM->label());
$this->assertEquals('My label', EnhancedBackedEnum::ENUM->label());
}

public function testShouldGetNameWhenLabelDoesNotExist()
{
$this->assertEquals('ANOTHER_ENUM', EnhancedEnum::ANOTHER_ENUM->label());
$this->assertEquals('ANOTHER_ENUM', EnhancedBackedEnum::ANOTHER_ENUM->label());
}
}
Loading

0 comments on commit 9e04602

Please sign in to comment.