Skip to content

Commit

Permalink
small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Jun 10, 2022
1 parent caab592 commit a0b9ab7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/Concerns/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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')) {
Expand Down
6 changes: 1 addition & 5 deletions src/Concerns/Constructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
24 changes: 17 additions & 7 deletions src/Helpers/EnumMakers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}
Expand All @@ -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);
Expand Down

0 comments on commit a0b9ab7

Please sign in to comment.