Skip to content

Commit 4562559

Browse files
committed
refactor: detect collection type using TypeHelper
1 parent 01baf0a commit 4562559

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

Serializer/ItemNormalizer.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ApiPlatform\Metadata\ResourceClassResolverInterface;
2222
use ApiPlatform\Metadata\UrlGeneratorInterface;
2323
use ApiPlatform\Metadata\Util\ClassInfoTrait;
24+
use ApiPlatform\Metadata\Util\TypeHelper;
2425
use ApiPlatform\Serializer\AbstractItemNormalizer;
2526
use ApiPlatform\Serializer\CacheKeyTrait;
2627
use ApiPlatform\Serializer\ContextTrait;
@@ -39,7 +40,6 @@
3940
use Symfony\Component\TypeInfo\Type\CollectionType;
4041
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
4142
use Symfony\Component\TypeInfo\Type\ObjectType;
42-
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
4343

4444
/**
4545
* Converts between objects and array including HAL metadata.
@@ -212,17 +212,8 @@ private function getComponents(object $object, ?string $format, array $context):
212212
$isOne = $className && $this->resourceClassResolver->isResourceClass($className);
213213
}
214214
} elseif ($type instanceof Type) {
215-
$typeIsCollection = function (Type $type) use (&$typeIsCollection, &$valueType): bool {
216-
return match (true) {
217-
$type instanceof CollectionType => null !== $valueType = $type->getCollectionValueType(),
218-
$type instanceof WrappingTypeInterface => $type->wrappedTypeIsSatisfiedBy($typeIsCollection),
219-
$type instanceof CompositeTypeInterface => $type->composedTypesAreSatisfiedBy($typeIsCollection),
220-
default => false,
221-
};
222-
};
223-
224-
if ($type->isSatisfiedBy($typeIsCollection)) {
225-
$isMany = $valueType->isSatisfiedBy($typeIsResourceClass);
215+
if ($type->isSatisfiedBy(fn ($t) => $t instanceof CollectionType)) {
216+
$isMany = TypeHelper::getCollectionValueType($type)?->isSatisfiedBy($typeIsResourceClass);
226217
} else {
227218
$isOne = $type->isSatisfiedBy($typeIsResourceClass);
228219
}

0 commit comments

Comments
 (0)