Skip to content

Commit

Permalink
Add additional comparison methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
hailwood committed Mar 1, 2023
1 parent bbd5d25 commit e0817fb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 45 deletions.
40 changes: 11 additions & 29 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,35 @@

All notable changes to `laravel-backed-enums` will be documented in this file.

## Add support for direct value comparisons - 2023-02-27
## v1.2.3 - 2023-03-02

Right now if you want to use `->isA` or `->isAny` or the other comparison methods you must pass in an enum instance, I.e.

```php
$user->role->isA(MyEnum::from('admin')); // true
$user->role->isA('admin'); // false

$user->role->isA(MyEnum::from('not-a-value')); // exception
$user->role->isA('not-a-value'); // false

$user->role->isAny([MyEnum::from('admin')]); // true
$user->role->isAny(['admin']); // false

$user->role->isAny([MyEnum::from('not-a-value')]); // exception
$user->role->isAny(['not-a-value']); // false
### What's Changed

```
This release makes it so each pair of methods will act the same whether given a string value or an enum instance.
- Add additional comparison methods

```php
$user->role->isA(MyEnum::from('admin')); // true
$user->role->isA('admin'); // true
**Full Changelog**: https://github.com/webfox/laravel-backed-enums/compare/v1.1.0...v1.2.3

$user->role->isA(MyEnum::from('not-a-value')); // exception
$user->role->isA('not-a-value'); // exception
## v1.2.2 - 2023-02-28

$user->role->isAny([MyEnum::from('admin')]); // true
$user->role->isAny(['admin']); // true
### What's Changed

$user->role->isAny([MyEnum::from('not-a-value')]); // exception
$user->role->isAny(['not-a-value']); // exception
- Add support for direct value comparisons

```
This also applies for isAn, isNotA, isNotAn, isNotAny
**Full Changelog**: https://github.com/webfox/laravel-backed-enums/compare/v1.2.1...v1.2.2

## v1.2.1 - 2023-02-22

### What's Changed

- Add support for laravel 10

**Full Changelog**: https://github.com/webfox/laravel-backed-enums/compare/v1.1.0...v1.2.1

## v1.1.0 - 2022-10-04

### What's Changed

- Add support for rendering enums in blade by @hailwood in https://github.com/webfox/laravel-backed-enums/commit/c8724cafa35dbec93ddc23e8f20c9808ab8f4da3
- Add support for rendering enums in blade templates

**Full Changelog**: https://github.com/webfox/laravel-backed-enums/compare/v1.0.1...v1.1.0

Expand Down
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,30 +268,32 @@ returns

An alias for toArray.

### isA/isAn
### is/isA/isAn

Allows you to check if an enum is a given value. Returns a boolean.
> **Note**
> `isAn` is just an alias for `isA`.
> `isA`, `isAn` are just aliases for `is`.
#### Usage

```php
VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::GRAMS); //false
VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::MILLIGRAMS); //true
VolumeUnitEnum::MILLIGRAMS->is(VolumeUnitEnum::MILLIGRAMS); //true
VolumeUnitEnum::MILLIGRAMS->is('MILLIGRAMS'); //true
VolumeUnitEnum::MILLIGRAMS->is('invalid'); //exception
```

### isNotA/isNotAn
### isNot/isNotA/isNotAn

Allows you to check if an enum is not a given value. Returns a boolean.
> **Note**
> `isNotAn` is just an alias for `isNotA`.
> `isNotA` and `isNotAn` are just aliases for `isNot`.
#### Usage

```php
VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::GRAMS); //true
VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::MILLIGRAMS); //false
VolumeUnitEnum::MILLIGRAMS->isNot(VolumeUnitEnum::GRAMS); //true
VolumeUnitEnum::MILLIGRAMS->isNot('GRAMS'); //true
VolumeUnitEnum::MILLIGRAMS->isNot('invalid'); //exception
```

### isAny
Expand All @@ -301,7 +303,7 @@ Allows you to check if an enum is contained in an array. Returns a boolean.
#### Usage

```php
VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::TONNE]); // false
VolumeUnitEnum::MILLIGRAMS->isAny(['GRAMS', VolumeUnitEnum::TONNE]); // false
VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // true
```

Expand All @@ -312,8 +314,8 @@ Allows you to check if an enum is not contained in an array. Returns a boolean.
#### Usage

```php
VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::TONNE]); // true
VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // false
VolumeUnitEnum::MILLIGRAMS->isNotAny(['GRAMS', VolumeUnitEnum::TONNE]); // true
VolumeUnitEnum::MILLIGRAMS->isNotAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // false
```

## Changelog
Expand Down
28 changes: 23 additions & 5 deletions src/IsBackedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,22 @@ public function toJson($options = 0): array
return $this->toArray();
}

public function isA($value): bool
public function is(string|self $value): bool
{
static::ensureImplementsInterface();
return $this->isAny([$value]);
}

public function isAn(string $value): bool
public function isA(string|self $value): bool
{
static::ensureImplementsInterface();
return $this->isA($value);
return $this->is($value);
}

public function isNot(string $value): bool
public function isAn(string|self $value): bool
{
static::ensureImplementsInterface();
return !$this->isA($value);
return $this->is($value);
}

public function isAny(array $values): bool
Expand All @@ -127,6 +127,24 @@ public function isAny(array $values): bool
return in_array($this, $values);
}

public function isNot(string|self $value): bool
{
static::ensureImplementsInterface();
return !$this->isAny([$value]);
}

public function isNotA(string|self $value): bool
{
static::ensureImplementsInterface();
return $this->isNot($value);
}

public function isNotAn(string|self $value): bool
{
static::ensureImplementsInterface();
return $this->isNot($value);
}

public function isNotAny(array $values): bool
{
static::ensureImplementsInterface();
Expand Down

0 comments on commit e0817fb

Please sign in to comment.