Skip to content

Commit

Permalink
Merge pull request #78 from innocenzi/feat/enum-collector
Browse files Browse the repository at this point in the history
feat(enum-collector): improve extensibility of `EnumTransformer`
  • Loading branch information
rubenvanassche authored May 3, 2024
2 parents b4c233c + 63c0c96 commit cc6969d
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/Collectors/EnumCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ class EnumCollector extends DefaultCollector
{
public function getTransformedType(ReflectionClass $class): ?TransformedType
{
$transformers = array_map('get_class', $this->config->getTransformers());

if (! \in_array(EnumTransformer::class, $transformers, true)) {
if (!$this->shouldCollect($class)) {
return null;
}

$reflector = ClassTypeReflector::create($class);

if (! $reflector->getReflectionClass()->implementsInterface(BackedEnum::class)) {
return null;
}

$transformedType = $reflector->getType()
? $this->resolveAlreadyTransformedType($reflector)
: $this->resolveTypeViaTransformer($reflector);
Expand All @@ -35,4 +29,29 @@ public function getTransformedType(ReflectionClass $class): ?TransformedType

return $transformedType;
}

protected function shouldCollect(ReflectionClass $class): bool
{
$transformers = array_map('get_class', $this->config->getTransformers());

$hasEnumTransformer = \count(
array_filter($transformers, function (string $transformer) {
if ($transformer === EnumTransformer::class) {
return true;
}

return is_subclass_of($transformer, EnumTransformer::class);
}),
) > 0;

if (!$hasEnumTransformer) {
return false;
}

if (!$class->implementsInterface(BackedEnum::class)) {
return false;
}

return true;
}
}

0 comments on commit cc6969d

Please sign in to comment.