Skip to content

Commit 5b05842

Browse files
committed
- Add isIntBacked and isStringBacked method
1 parent 8ab74a9 commit 5b05842

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,16 @@ StringBackedEnum::tryFromName('MISSING'); // null
201201
### Inspection
202202
This helper permits check the type of enum (`isPure()`,`isBacked()`) and if enum contains a case name or value (`has()`, `doesntHave()`, `hasName()`, `doesntHaveName()`, `hasValue()`, `doesntHaveValue()`).
203203

204-
#### `isPure()` and `isBacked()`
204+
#### `isPure()`, `isBacked()`, `isIntBacked()`and `isStringBacked()`
205205
With these methods you can check the type of the enum instance.
206206

207207
```php
208208
PureEnum::PENDING->isPure() // true
209209
PureEnum::PENDING->isBacked() // false
210210
IntBackedEnum::PENDING->isPure() // false
211+
IntBackedEnum::PENDING->isIntBacked() // true
211212
StringBackedEnum::PENDING->isBacked() // true
213+
StringBackedEnum::PENDING->isStringBacked() // true
212214
```
213215

214216
#### `has()` and `doesntHave()`

src/Traits/EnumInspection.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Datomatic\EnumHelper\Traits;
66

77
use BackedEnum;
8+
use ReflectionEnum;
89

910
trait EnumInspection
1011
{
@@ -26,6 +27,22 @@ public static function isBacked(): bool
2627
return is_subclass_of(self::class, BackedEnum::class);
2728
}
2829

30+
/**
31+
* Check if enum is IntBackedEnum.
32+
*/
33+
public static function isIntBacked(): bool
34+
{
35+
return (new ReflectionEnum(self::class))->getBackingType()->getName() === 'int';
36+
}
37+
38+
/**
39+
* Check if enum is IntBackedEnum.
40+
*/
41+
public static function isStringBacked(): bool
42+
{
43+
return (new ReflectionEnum(self::class))->getBackingType()->getName() === 'string';
44+
}
45+
2946
/**
3047
* Check if enum has a case expressed in int, string or enum instance
3148
*

0 commit comments

Comments
 (0)