Skip to content

Commit

Permalink
doc updates and backwards compatibility fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Jun 2, 2022
1 parent 3678b71 commit 47496cc
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 16 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This package is your Swiss Army knife when it comes to PHP 8.1's enums.
In this package you will find a lot of tools for the most common use cases
and more will be added in the future.

If you have an idea or you miss something that needs to be added,
If you have an idea, or you miss something that needs to be added,
just let me know.

This package currently supports the following:
Expand All @@ -25,6 +25,10 @@ This package currently supports the following:
- Subset
- Value (for unit enums)

While functionality that also exists in Spatie's PHP Enum is made backwards
compatible to allow for an easy migration to PHP native enums, currently this is
not the case for the PHPUnit assertions or Faker Provider.

## Installation

You can install the package via composer:
Expand Down
35 changes: 31 additions & 4 deletions docs/comparison.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Comparison
This will allow you to easily compare enums and strings with each other. This
also is backwards-compatible with [Spatie's PHP Enum](https://github.com/spatie/enum)
This will allow you to easily compare enums, integers and strings with each other.
This also is backwards-compatible with
[Spatie's PHP Enum](https://github.com/spatie/enum)

## usage

Expand All @@ -13,11 +14,25 @@ enum YourEnum: string {
CASE ENUM = 'your_value';
CASE ENUM2 = 'your_other_value';
}

enum YourOtherEnum: int {
use Comparison;

CASE ENUM = 0;
CASE ENUM2 = 1;
}

enum YourThirdEnum {
use Comparison;

CASE ENUM;
CASE ENUM2;
}
```

### Examples
The method equals accepts multiple strings or enums. If one of
them matches, true will be returned.
The method equals accepts multiple strings, integers or enums of the same type.
If one of them matches, true will be returned.

```php
YourEnum::ENUM->equals(YourEnum::ENUM); // returns true
Expand All @@ -31,5 +46,17 @@ YourEnum::ENUM->equals('your_other_value'); //returns false
YourEnum::ENUM->equals(YourEnum::ENUM, 'your_other_value'); // returns true
YourEnum::ENUM->equals('ENUM', YourEnum::ENUM2); // returns true
YourEnum::ENUM->equals('your_value', 'your_other_value'); //returns true

YourOtherEnum::ENUM->equals(YourOtherEnum::ENUM); // returns true
YourOtherEnum::ENUM->equals(0); // returns true
YourOtherEnum::ENUM->equals(1); //returns false
YourOtherEnum::ENUM->equals(0, 1); //returns true

YourThirdEnum::ENUM->equals(YourThirdEnum::ENUM); // returns true
YourThirdEnum::ENUM->equals('ENUM'); // returns true
YourThirdEnum::ENUM->equals('ENUM2'); //returns false
YourThirdEnum::ENUM->equals('enum'); // returns true
YourThirdEnum::ENUM->equals('enum2'); //returns false
YourThirdEnum::ENUM->equals('enum', 'enum2'); //returns true
```

17 changes: 15 additions & 2 deletions docs/extractor.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,27 @@ enum YourEnum: string {
case ENUM = 'enum';
case ENUM2 = 'another enum';
}

enum YourOtherEnum: int {
use Extractor;

case ENUM = 0;
case ENUM2 = 1;
}
```

### Examples
```php
YourEnum::extract('you can find another enum here'); // returns [YourEnum::ENUM2]
YourEnum::extract('A lot of text with (enum)'); // returns [YourEnum::ENUM]
YourEnum::extract('another enum (enum)'); // returns [YourEnum::ENUM2, YourEnum::ENUM]
YourEnum::extract('extact case sensitive another ENUM') // returns [YourEnum::ENUM2];
YourEnum::extract('extract case sensitive another ENUM') // returns [YourEnum::ENUM2];
YourEnum::extract('contains multiple enums') // returns [];

YourOtherEnum::extract('I found 1 truth'); // returns [YourOtherEnum::ENUM2]
YourOtherEnum::extract('I found 1 truth and 0 lies'); // returns [YourOtherEnum::ENUM2, YourOtherEnum::ENUM]
YourOtherEnum::extract('I found 100 lies'); // returns []
```

Note: You can use `Mappers`
Note: You can use `Mappers` in combination with `Extractor`. This might be helpful
when the source has multiple or different notations of your enum cases.
7 changes: 4 additions & 3 deletions docs/labels.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Labels

Just like [Spatie's PHP Enum](https://github.com/spatie/enum), you can add labels to your enums. This is backwards
compatible with their package.
Just like [Spatie's PHP Enum](https://github.com/spatie/enum), you can add labels to
your enums. This is largely backwards compatible with their package, except that it
also works for UnitEnum's in which case it returns the name if not specified.

## Usage

Expand All @@ -26,5 +27,5 @@ enum YourEnum {
### Examples
```php
YourEnum::ENUM->label(); // will return 'Your label'
YourEnum::NO_LABEL->label(); // will return null;
YourEnum::NO_LABEL->label(); // will return 'NO_LABEL';
```
4 changes: 4 additions & 0 deletions docs/makers.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ you cannot use them to create `enum objects` with the name of the `enum`.

This allows you to do so.

Note: the name `make` is chosen because of Spatie, who started using them
and never moved away from them. So this is also backwards compatible with
[Spatie's PHP Enum](https://github.com/spatie/enum)

## Usage
```php
use Henzeb\Enumhancer\Concerns\Makers;
Expand Down
2 changes: 1 addition & 1 deletion docs/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ YourEnum::property('your_property'); // will return callable
YourEnum::property('your_property', new stdClass());
YourEnum::property('your_property'); // will return stdClass() instance

YourEnum::unset('your_property'); // will remove you_property
YourEnum::unset('your_property'); // will remove your_property
YourEnum::unsetAll(); // will clear all properties.
```

Expand Down
2 changes: 1 addition & 1 deletion docs/reporters.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Reporters

Sometimes you don't want your code to fail, yet you want to know when an
enum is tried to be instantiated, but failed. This allow you to do so.
enum is tried to be instantiated, but failed. This allows you to do so.

## usage
```php
Expand Down
2 changes: 1 addition & 1 deletion docs/value.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Value

When you have a unit enum, you have no value. You have to use the `name` value. But what if you
When you have a UnitEnum 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
Expand Down
5 changes: 4 additions & 1 deletion src/Concerns/Labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public function labels(): array

final public function label(): ?string
{
return $this->labels()[$this->name] ?? $this->name;
return $this->labels()[$this->name]
?? (method_exists($this, 'value') ? $this->value() : null)
?? $this->value
?? $this->name;
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Concerns/LabelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function testShouldGetLabelByName()
$this->assertEquals('My label', EnhancedBackedEnum::ENUM->label());
}

public function testShouldGetNameWhenLabelDoesNotExist()
public function testShouldGetValueWhenLabelDoesNotExist()
{
$this->assertEquals('ANOTHER_ENUM', EnhancedBackedEnum::ANOTHER_ENUM->label());
$this->assertEquals('another enum', EnhancedBackedEnum::ANOTHER_ENUM->label());
}
}

0 comments on commit 47496cc

Please sign in to comment.