Skip to content

Commit 1e7dc3d

Browse files
Added isAnyOf(), isNoneOf() and doesNotEqualTo() methods
1 parent 29453c2 commit 1e7dc3d

File tree

4 files changed

+134
-10
lines changed

4 files changed

+134
-10
lines changed

Changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## 4.x
44

5+
## Unreleased
6+
###### 2024-XX-YY
7+
8+
- Added the `isAnyOf()`, `isNoneOf()` and `doesNotEqualTo()` comparison methods
9+
510
## 4.1.0
611
###### 2023-06-07
712

docs/compare.md

+44-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ var_dump(
3131
// bool(true)
3232
```
3333

34-
Many developers prefer to avoid using negative conditions in their code
35-
like:
34+
### Does Not Equal To
35+
36+
Many developers prefer to avoid using negative conditions in their code like:
3637

3738
```php
3839
if (!$one->equals($two)) //...
@@ -41,6 +42,9 @@ if (!$one->equals($two)) //...
4142
thus the `notEquals()` method is available (since v2.2) for improved
4243
code readability. It's simply just the negation of `equals()`.
4344

45+
In v4.2, the `doesNotEqualTo(EnumInterface $enum): bool` method has been added, which is equivalent to the `notEquals()` method,
46+
but it only accepts `EnumInterface` as argument.
47+
4448
### Type Check
4549

4650
The `equals()` method does a type check so two different types of the same value won't be equal:
@@ -141,6 +145,44 @@ const ONE = 1 ==> isOne()
141145
const LUCKY_LUKE = 'll' ==> isLuckyLuke()
142146
```
143147

148+
## The `isAnyOf()` and `isNoneOf()` Methods
149+
150+
> This is a v4.2+ feature
151+
152+
It is possible to check whether an enum "is any of" or "is none of" multiple enum instances:
153+
154+
```php
155+
class Status extends \Konekt\Enum\Enum
156+
{
157+
public const NEW = 'new';
158+
public const PENDING = 'pending';
159+
public const COMPLETE = 'complete';
160+
public const CANCELED = 'canceled';
161+
}
162+
163+
$new = Status::NEW();
164+
$new->isAnyOf(Status::PENDING(), Status::NEW());
165+
// => true
166+
167+
$complete = Status::COMPLETE();
168+
$complete->isNoneOf(Status::PENDING(), Status::NEW());
169+
// => true
170+
171+
$canceled = Status::CANCELED();
172+
$canceled->isAnyOf(Status::NEW(), Status::PENDING());
173+
// => false
174+
175+
$pending = Status::PENDING();
176+
$pending->isNoneOf(Status::PENDING(), Status::COMPLETE());
177+
// => false
178+
```
179+
180+
You can pass any number of arguments to the `isNoneOf()` and `isAnyOf()` methods,
181+
but all of them have to be `EnumInterface` instances.
182+
183+
The methods will use the `equals()` method under the hood to compare the enums, so the same rules described above apply
184+
here as well.
185+
144186
---
145187

146188
**Next**: [Labels »](labels.md)

src/Enum.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ public function label(): string
162162
/**
163163
* Checks if two enums are equal. Value and class are both matched.
164164
* Value check is not type strict.
165-
*
166-
* @param mixed $object
167-
*
168-
* @return bool True if enums are equal
169165
*/
170166
public function equals(object $object): bool
171167
{
@@ -179,16 +175,27 @@ public function equals(object $object): bool
179175
/**
180176
* Checks if two enums are NOT equal. Value and class are both matched.
181177
* Value check is not type strict.
182-
*
183-
* @param mixed $object
184-
*
185-
* @return bool True if enums do not equal
186178
*/
187179
public function notEquals(object $object): bool
188180
{
189181
return !$this->equals($object);
190182
}
191183

184+
public function doesNotEqualTo(EnumInterface $enum): bool
185+
{
186+
return $this->notEquals($enum);
187+
}
188+
189+
public function isAnyOf(EnumInterface ...$enums): bool
190+
{
191+
return !$this->isNoneOf(...$enums);
192+
}
193+
194+
public function isNoneOf(EnumInterface ...$enums): bool
195+
{
196+
return empty(array_filter($enums, fn (Enum $enum) => $this->equals($enum)));
197+
}
198+
192199
/**
193200
* Returns the default value of the class. Equals to the __DEFAULT constant.
194201
*/

tests/IsAnyOfTest.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Contains the IsAnyOfTest class.
7+
*
8+
* @copyright Copyright (c) 2024 Attila Fulop
9+
* @author Attila Fulop
10+
* @license MIT
11+
* @since 2024-02-29
12+
*
13+
*/
14+
15+
namespace Konekt\Enum\Tests;
16+
17+
use Konekt\Enum\Tests\Fixture\Another123;
18+
use Konekt\Enum\Tests\Fixture\Sample123;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class IsAnyOfTest extends TestCase
22+
{
23+
/** @test */
24+
public function is_any_of_returns_true_when_passing_the_same_instance()
25+
{
26+
$one = Sample123::create(1);
27+
28+
$this->assertTrue($one->isAnyOf($one));
29+
}
30+
31+
/** @test */
32+
public function is_any_of_returns_true_when_passing_the_same_value_but_separate_instances()
33+
{
34+
$one = Sample123::create(1);
35+
36+
$this->assertTrue($one->isAnyOf(Sample123::create(1)));
37+
}
38+
39+
/** @test */
40+
public function is_any_of_returns_true_when_passing_multiple_values_of_which_one_equals_the()
41+
{
42+
$one = Sample123::create(1);
43+
44+
$this->assertTrue($one->isAnyOf(Sample123::ONE(), Sample123::TWO()));
45+
}
46+
47+
/** @test */
48+
public function is_none_of_returns_true_when_passing_nothing()
49+
{
50+
$one = Sample123::create(1);
51+
52+
$this->assertTrue($one->isNoneOf());
53+
}
54+
55+
/** @test */
56+
public function is_none_of_returns_true_when_passing_enums_of_which_none_equals_the_base_enum()
57+
{
58+
$three = Sample123::create(3);
59+
60+
$this->assertTrue($three->isNoneOf(Sample123::ONE(), Sample123::TWO()));
61+
}
62+
63+
/** @test */
64+
public function mixing_separate_enums_with_same_underlying_values_will_not_match()
65+
{
66+
$two = Sample123::TWO();
67+
68+
$this->assertTrue($two->isNoneOf(Another123::TWO()));
69+
}
70+
}

0 commit comments

Comments
 (0)