diff --git a/docs/casting.md b/docs/casting.md index cdbd798..2a5a130 100644 --- a/docs/casting.md +++ b/docs/casting.md @@ -24,5 +24,19 @@ class YourModel extends Model ``` ### Lowercase values -By default, it will use of the basic enumeration as the value. If you want the -lowercase variant, you can add the `$keepEnumCase` property and set it to false. +By default, it will use the name of the basic enumeration as the value. If you want +the lowercase variant, you can add the `$keepEnumCase` property and set it to false. + +```php +class YourModel extends Model +{ + use CastsBasicEnumerations; + + private $keepEnumCase = false; + + $casts = [ + 'column' => YourEnum::class + ]; +} + +``` diff --git a/src/Helpers/EnumSubsetMethods.php b/src/Helpers/EnumSubsetMethods.php index 0140a53..6ecb1cb 100644 --- a/src/Helpers/EnumSubsetMethods.php +++ b/src/Helpers/EnumSubsetMethods.php @@ -46,15 +46,15 @@ private function compare(UnitEnum $enum, UnitEnum|string|int ...$equals): bool return true; } - if (property_exists($enum, 'value') && $enum->value === $equal) { - return true; + if(is_object($equal)) { + $equal = EnumValue::value($equal); } - if (method_exists($enum, 'value') && $enum->value() === $equal) { - return true; + if(is_string($equal)) { + $equal = strtolower($equal); } - if ($equal instanceof UnitEnum && $enum->name === $equal->name) { + if(EnumValue::value($enum) === $equal) { return true; } } diff --git a/tests/Fixtures/CastsBasicEnumsLowerCaseModel.php b/tests/Fixtures/CastsBasicEnumsLowerCaseModel.php index 018709d..82eff02 100644 --- a/tests/Fixtures/CastsBasicEnumsLowerCaseModel.php +++ b/tests/Fixtures/CastsBasicEnumsLowerCaseModel.php @@ -9,7 +9,7 @@ class CastsBasicEnumsLowerCaseModel extends Model { use CastsBasicEnumerations; - protected $keepEnumCase = false; + private $keepEnumCase = false; protected $casts = [ 'unitEnum' => SubsetUnitEnum::class, diff --git a/tests/Fixtures/SubsetUnitEnum.php b/tests/Fixtures/SubsetUnitEnum.php index ff76ab2..05fe1b5 100644 --- a/tests/Fixtures/SubsetUnitEnum.php +++ b/tests/Fixtures/SubsetUnitEnum.php @@ -4,10 +4,11 @@ use Henzeb\Enumhancer\Concerns\Subset; use Henzeb\Enumhancer\Concerns\Extractor; +use Henzeb\Enumhancer\Concerns\Comparison; enum SubsetUnitEnum { - use Subset, Extractor; + use Subset, Extractor, Comparison; case ENUM; case ANOTHER_ENUM; diff --git a/tests/Unit/Concerns/ComparisonTest.php b/tests/Unit/Concerns/ComparisonTest.php index 393b380..14d649f 100644 --- a/tests/Unit/Concerns/ComparisonTest.php +++ b/tests/Unit/Concerns/ComparisonTest.php @@ -5,6 +5,7 @@ use Henzeb\Enumhancer\Concerns\Comparison; use PHPUnit\Framework\TestCase; use Henzeb\Enumhancer\Tests\Fixtures\IntBackedEnum; +use Henzeb\Enumhancer\Tests\Fixtures\SubsetUnitEnum; use Henzeb\Enumhancer\Tests\Fixtures\EnhancedUnitEnum; use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum; @@ -84,6 +85,18 @@ public function testShouldMatchWithUnitEnumValue() { ); } + public function testShouldMatchWithUnitEnumValue2() { + $this->assertTrue( + EnhancedUnitEnum::ENUM->equals('Enum') + ); + } + + public function testShouldMatchWithUnitEnumValueWithoutValueMethod() { + $this->assertTrue( + SubsetUnitEnum::ENUM->equals('enum') + ); + } + public function testShouldMatchWithIntBackedEnumValue() { $this->assertTrue( IntBackedEnum::TEST->equals(0)