diff --git a/README.md b/README.md index 9f335a7..7dcd181 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: diff --git a/docs/comparison.md b/docs/comparison.md index 3b4fc3d..2d373c2 100644 --- a/docs/comparison.md +++ b/docs/comparison.md @@ -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 @@ -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 @@ -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 ``` diff --git a/docs/extractor.md b/docs/extractor.md index 715c5f9..d1b3f0f 100644 --- a/docs/extractor.md +++ b/docs/extractor.md @@ -16,6 +16,13 @@ enum YourEnum: string { case ENUM = 'enum'; case ENUM2 = 'another enum'; } + +enum YourOtherEnum: int { + use Extractor; + + case ENUM = 0; + case ENUM2 = 1; +} ``` ### Examples @@ -23,7 +30,13 @@ enum YourEnum: string { 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. diff --git a/docs/labels.md b/docs/labels.md index e81ae64..40b09d9 100644 --- a/docs/labels.md +++ b/docs/labels.md @@ -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 @@ -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'; ``` diff --git a/docs/makers.md b/docs/makers.md index 04ec069..deabc9d 100644 --- a/docs/makers.md +++ b/docs/makers.md @@ -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; diff --git a/docs/properties.md b/docs/properties.md index 19f3c53..bdcc47e 100644 --- a/docs/properties.md +++ b/docs/properties.md @@ -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. ``` diff --git a/docs/reporters.md b/docs/reporters.md index efa1edd..dbf32fa 100644 --- a/docs/reporters.md +++ b/docs/reporters.md @@ -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 diff --git a/docs/value.md b/docs/value.md index 9bd53b8..1ca887b 100644 --- a/docs/value.md +++ b/docs/value.md @@ -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 diff --git a/src/Concerns/Labels.php b/src/Concerns/Labels.php index 7615e00..e4dd6bb 100644 --- a/src/Concerns/Labels.php +++ b/src/Concerns/Labels.php @@ -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; } } diff --git a/tests/Unit/Concerns/LabelsTest.php b/tests/Unit/Concerns/LabelsTest.php index 3bac26b..625e72c 100644 --- a/tests/Unit/Concerns/LabelsTest.php +++ b/tests/Unit/Concerns/LabelsTest.php @@ -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()); } }