Skip to content

Commit e0817fb

Browse files
committed
Add additional comparison methods.
1 parent bbd5d25 commit e0817fb

File tree

3 files changed

+47
-45
lines changed

3 files changed

+47
-45
lines changed

CHANGELOG.md

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,35 @@
22

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

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

7-
Right now if you want to use `->isA` or `->isAny` or the other comparison methods you must pass in an enum instance, I.e.
8-
9-
```php
10-
$user->role->isA(MyEnum::from('admin')); // true
11-
$user->role->isA('admin'); // false
12-
13-
$user->role->isA(MyEnum::from('not-a-value')); // exception
14-
$user->role->isA('not-a-value'); // false
15-
16-
$user->role->isAny([MyEnum::from('admin')]); // true
17-
$user->role->isAny(['admin']); // false
18-
19-
$user->role->isAny([MyEnum::from('not-a-value')]); // exception
20-
$user->role->isAny(['not-a-value']); // false
7+
### What's Changed
218

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

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

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

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

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

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

4121
## v1.2.1 - 2023-02-22
4222

4323
### What's Changed
4424

4525
- Add support for laravel 10
4626

27+
**Full Changelog**: https://github.com/webfox/laravel-backed-enums/compare/v1.1.0...v1.2.1
28+
4729
## v1.1.0 - 2022-10-04
4830

4931
### What's Changed
5032

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

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

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -268,30 +268,32 @@ returns
268268

269269
An alias for toArray.
270270

271-
### isA/isAn
271+
### is/isA/isAn
272272

273273
Allows you to check if an enum is a given value. Returns a boolean.
274274
> **Note**
275-
> `isAn` is just an alias for `isA`.
275+
> `isA`, `isAn` are just aliases for `is`.
276276
277277
#### Usage
278278

279279
```php
280-
VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::GRAMS); //false
281-
VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::MILLIGRAMS); //true
280+
VolumeUnitEnum::MILLIGRAMS->is(VolumeUnitEnum::MILLIGRAMS); //true
281+
VolumeUnitEnum::MILLIGRAMS->is('MILLIGRAMS'); //true
282+
VolumeUnitEnum::MILLIGRAMS->is('invalid'); //exception
282283
```
283284

284-
### isNotA/isNotAn
285+
### isNot/isNotA/isNotAn
285286

286287
Allows you to check if an enum is not a given value. Returns a boolean.
287288
> **Note**
288-
> `isNotAn` is just an alias for `isNotA`.
289+
> `isNotA` and `isNotAn` are just aliases for `isNot`.
289290
290291
#### Usage
291292

292293
```php
293-
VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::GRAMS); //true
294-
VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::MILLIGRAMS); //false
294+
VolumeUnitEnum::MILLIGRAMS->isNot(VolumeUnitEnum::GRAMS); //true
295+
VolumeUnitEnum::MILLIGRAMS->isNot('GRAMS'); //true
296+
VolumeUnitEnum::MILLIGRAMS->isNot('invalid'); //exception
295297
```
296298

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

303305
```php
304-
VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::TONNE]); // false
306+
VolumeUnitEnum::MILLIGRAMS->isAny(['GRAMS', VolumeUnitEnum::TONNE]); // false
305307
VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // true
306308
```
307309

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

314316
```php
315-
VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::TONNE]); // true
316-
VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // false
317+
VolumeUnitEnum::MILLIGRAMS->isNotAny(['GRAMS', VolumeUnitEnum::TONNE]); // true
318+
VolumeUnitEnum::MILLIGRAMS->isNotAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // false
317319
```
318320

319321
## Changelog

src/IsBackedEnum.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,22 @@ public function toJson($options = 0): array
9797
return $this->toArray();
9898
}
9999

100-
public function isA($value): bool
100+
public function is(string|self $value): bool
101101
{
102102
static::ensureImplementsInterface();
103103
return $this->isAny([$value]);
104104
}
105105

106-
public function isAn(string $value): bool
106+
public function isA(string|self $value): bool
107107
{
108108
static::ensureImplementsInterface();
109-
return $this->isA($value);
109+
return $this->is($value);
110110
}
111111

112-
public function isNot(string $value): bool
112+
public function isAn(string|self $value): bool
113113
{
114114
static::ensureImplementsInterface();
115-
return !$this->isA($value);
115+
return $this->is($value);
116116
}
117117

118118
public function isAny(array $values): bool
@@ -127,6 +127,24 @@ public function isAny(array $values): bool
127127
return in_array($this, $values);
128128
}
129129

130+
public function isNot(string|self $value): bool
131+
{
132+
static::ensureImplementsInterface();
133+
return !$this->isAny([$value]);
134+
}
135+
136+
public function isNotA(string|self $value): bool
137+
{
138+
static::ensureImplementsInterface();
139+
return $this->isNot($value);
140+
}
141+
142+
public function isNotAn(string|self $value): bool
143+
{
144+
static::ensureImplementsInterface();
145+
return $this->isNot($value);
146+
}
147+
130148
public function isNotAny(array $values): bool
131149
{
132150
static::ensureImplementsInterface();

0 commit comments

Comments
 (0)