diff --git a/src/Concerns/Comparison.php b/src/Concerns/Comparison.php index e793d35..006cbeb 100644 --- a/src/Concerns/Comparison.php +++ b/src/Concerns/Comparison.php @@ -20,7 +20,7 @@ final public function equals(self|string|int ...$equals): bool final public function __call(string $name, array $arguments): self|bool { - $nameIsEnum = !EnumMakers::tryMake(self::class, $name); + $nameIsEnum = !EnumMakers::tryMake(self::class, $name, true); if (((!str_starts_with($name, 'is') && !str_starts_with($name, 'isNot')) || count($arguments)) && $nameIsEnum) { throw new BadMethodCallException(sprintf('Call to undefined method %s::%s(...)', $this::class, $name)); } @@ -31,7 +31,7 @@ final public function __call(string $name, array $arguments): self|bool $value = substr($name, str_starts_with($name, 'isNot') ? 5 : 2); - if (!EnumMakers::tryMake(self::class, $value)) { + if (!EnumMakers::tryMake(self::class, $value, true)) { throw new BadMethodCallException(sprintf('Call to undefined method %s::%s(...)', $this::class, $name)); } if (str_starts_with($name, 'isNot')) { diff --git a/src/Concerns/Constructor.php b/src/Concerns/Constructor.php index f71da7b..2960db9 100644 --- a/src/Concerns/Constructor.php +++ b/src/Concerns/Constructor.php @@ -8,10 +8,6 @@ trait Constructor { public static function __callStatic(string $name, array $arguments) { - if(method_exists(self::class, 'make')) { - return self::make($name); - } - - return EnumMakers::make(self::class, $name); + return EnumMakers::make(self::class, $name, true); } } diff --git a/src/Helpers/EnumMakers.php b/src/Helpers/EnumMakers.php index 81f1289..24a8ec1 100644 --- a/src/Helpers/EnumMakers.php +++ b/src/Helpers/EnumMakers.php @@ -5,13 +5,23 @@ use BackedEnum; use UnitEnum; use ValueError; +use Henzeb\Enumhancer\Concerns\Mappers; + class EnumMakers { - public static function make(string $class, int|string|null $value): mixed + private static function implementsMappers(string $enum): bool + { + return in_array(Mappers::class, class_uses_recursive($enum)); + } + public static function make(string $class, int|string|null $value, bool $useMapper = false): mixed { EnumCheck::check($class); + if($useMapper && self::implementsMappers($class)) { + return $class::make($value); + } + if (null === $value) { throw new ValueError('Invalid value!'); } @@ -35,37 +45,37 @@ public static function make(string $class, int|string|null $value): mixed throw new ValueError('Invalid Enum key!'); } - public static function tryMake(string $class, int|string|null $value): mixed + public static function tryMake(string $class, int|string|null $value, bool $useMapper = false): mixed { EnumCheck::check($class); try { - return self::make($class, $value); + return self::make($class, $value, $useMapper); } catch (ValueError) { return null; } } - public static function makeArray(string $class, iterable $values): array + public static function makeArray(string $class, iterable $values, bool $useMapper = false): array { EnumCheck::check($class); $return = []; foreach($values as $value) { - $return[] = self::make($class, $value); + $return[] = self::make($class, $value, $useMapper); } return $return; } - public static function tryMakeArray(string $class, iterable $values): array + public static function tryMakeArray(string $class, iterable $values, bool $useMapper = false): array { EnumCheck::check($class); $return = []; foreach($values as $value) { - $return[] = self::tryMake($class, $value); + $return[] = self::tryMake($class, $value, $useMapper); } return array_filter($return);