-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Compare.php
49 lines (38 loc) · 1.15 KB
/
Compare.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
namespace Bakame\Aide\Enum;
use ReflectionEnum;
use UnitEnum;
use function is_int;
use function is_string;
trait Compare
{
public function equals(mixed $value): bool
{
return $value instanceof UnitEnum && $value === $this;
}
public function isOneOf(int|string|UnitEnum ...$values): bool
{
$type = (new ReflectionEnum($this))->getBackingType()?->getName();
$isSatisfiedBy = fn (int|string|UnitEnum $value): bool => match (true) {
$value instanceof UnitEnum && $value === $this,
('int' === $type && is_int($value) || 'string' === $type && is_string($value)) && static::tryFrom($value) === $this,
$value === $this->name => true,
default => false,
};
foreach ($values as $value) {
if ($isSatisfiedBy($value)) {
return true;
}
}
return false;
}
public function notEquals(mixed $value): bool
{
return !$this->equals($value);
}
public function isNotOneOf(int|string|UnitEnum ...$value): bool
{
return !$this->isOneOf(...$value);
}
}