Skip to content

Commit e3812e6

Browse files
committed
comparison issue with basic enums and casing
1 parent e190efd commit e3812e6

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

docs/casting.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,19 @@ class YourModel extends Model
2424

2525
```
2626
### Lowercase values
27-
By default, it will use of the basic enumeration as the value. If you want the
28-
lowercase variant, you can add the `$keepEnumCase` property and set it to false.
27+
By default, it will use the name of the basic enumeration as the value. If you want
28+
the lowercase variant, you can add the `$keepEnumCase` property and set it to false.
29+
30+
```php
31+
class YourModel extends Model
32+
{
33+
use CastsBasicEnumerations;
34+
35+
private $keepEnumCase = false;
36+
37+
$casts = [
38+
'column' => YourEnum::class
39+
];
40+
}
41+
42+
```

src/Helpers/EnumSubsetMethods.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ private function compare(UnitEnum $enum, UnitEnum|string|int ...$equals): bool
4646
return true;
4747
}
4848

49-
if (property_exists($enum, 'value') && $enum->value === $equal) {
50-
return true;
49+
if(is_object($equal)) {
50+
$equal = EnumValue::value($equal);
5151
}
5252

53-
if (method_exists($enum, 'value') && $enum->value() === $equal) {
54-
return true;
53+
if(is_string($equal)) {
54+
$equal = strtolower($equal);
5555
}
5656

57-
if ($equal instanceof UnitEnum && $enum->name === $equal->name) {
57+
if(EnumValue::value($enum) === $equal) {
5858
return true;
5959
}
6060
}

tests/Fixtures/CastsBasicEnumsLowerCaseModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CastsBasicEnumsLowerCaseModel extends Model
99
{
1010
use CastsBasicEnumerations;
1111

12-
protected $keepEnumCase = false;
12+
private $keepEnumCase = false;
1313

1414
protected $casts = [
1515
'unitEnum' => SubsetUnitEnum::class,

tests/Fixtures/SubsetUnitEnum.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use Henzeb\Enumhancer\Concerns\Subset;
66
use Henzeb\Enumhancer\Concerns\Extractor;
7+
use Henzeb\Enumhancer\Concerns\Comparison;
78

89
enum SubsetUnitEnum
910
{
10-
use Subset, Extractor;
11+
use Subset, Extractor, Comparison;
1112

1213
case ENUM;
1314
case ANOTHER_ENUM;

tests/Unit/Concerns/ComparisonTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Henzeb\Enumhancer\Concerns\Comparison;
66
use PHPUnit\Framework\TestCase;
77
use Henzeb\Enumhancer\Tests\Fixtures\IntBackedEnum;
8+
use Henzeb\Enumhancer\Tests\Fixtures\SubsetUnitEnum;
89
use Henzeb\Enumhancer\Tests\Fixtures\EnhancedUnitEnum;
910
use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum;
1011

@@ -84,6 +85,18 @@ public function testShouldMatchWithUnitEnumValue() {
8485
);
8586
}
8687

88+
public function testShouldMatchWithUnitEnumValue2() {
89+
$this->assertTrue(
90+
EnhancedUnitEnum::ENUM->equals('Enum')
91+
);
92+
}
93+
94+
public function testShouldMatchWithUnitEnumValueWithoutValueMethod() {
95+
$this->assertTrue(
96+
SubsetUnitEnum::ENUM->equals('enum')
97+
);
98+
}
99+
87100
public function testShouldMatchWithIntBackedEnumValue() {
88101
$this->assertTrue(
89102
IntBackedEnum::TEST->equals(0)

0 commit comments

Comments
 (0)