You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
826 B
30 lines
826 B
<?php
|
|
|
|
namespace App\Domains\System\Traits\Enum;
|
|
|
|
/*
|
|
* @mixin \BackedEnum
|
|
*/
|
|
|
|
use BadMethodCallException;
|
|
use Illuminate\Support\Str;
|
|
|
|
trait HasPredicateMethod
|
|
{
|
|
public function __call(string $method, array $arguments): bool
|
|
{
|
|
if (str_starts_with($method, 'is')) {
|
|
$expectedCase = Str::substr($method, 2);
|
|
$expectedCase = Str::pascal($expectedCase);
|
|
foreach ($this::cases() as $case) {
|
|
if ($case->name === $expectedCase || Str::pascal($case->value) === $expectedCase) {
|
|
return $this === $case;
|
|
}
|
|
}
|
|
|
|
throw new BadMethodCallException("Method {$method} does not exist on ".self::class);
|
|
}
|
|
|
|
throw new BadMethodCallException("Method {$method} does not exist on ".self::class);
|
|
}
|
|
}
|
|
|