Skip to content

Commit

Permalink
comparison issue with basic enums and casing
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Jun 5, 2022
1 parent e190efd commit e3812e6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
18 changes: 16 additions & 2 deletions docs/casting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}

```
10 changes: 5 additions & 5 deletions src/Helpers/EnumSubsetMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/CastsBasicEnumsLowerCaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CastsBasicEnumsLowerCaseModel extends Model
{
use CastsBasicEnumerations;

protected $keepEnumCase = false;
private $keepEnumCase = false;

protected $casts = [
'unitEnum' => SubsetUnitEnum::class,
Expand Down
3 changes: 2 additions & 1 deletion tests/Fixtures/SubsetUnitEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Concerns/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e3812e6

Please sign in to comment.