Skip to content

Commit 19d9693

Browse files
committed
refactor: detect collection type using TypeHelper
1 parent f3d4afe commit 19d9693

File tree

4 files changed

+9
-45
lines changed

4 files changed

+9
-45
lines changed

src/Hal/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
}

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@
2929
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
3030
use ApiPlatform\Metadata\ResourceClassResolverInterface;
3131
use ApiPlatform\Metadata\UrlGeneratorInterface;
32+
use ApiPlatform\Metadata\Util\TypeHelper;
3233
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
3334
use Symfony\Component\PropertyInfo\Type as LegacyType;
3435
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
3536
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3637
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
3738
use Symfony\Component\TypeInfo\Type;
3839
use Symfony\Component\TypeInfo\Type\CollectionType;
39-
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
4040
use Symfony\Component\TypeInfo\Type\ObjectType;
41-
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
4241
use Symfony\Component\TypeInfo\TypeIdentifier;
4342

4443
use const ApiPlatform\JsonLd\HYDRA_CONTEXT;
@@ -371,19 +370,8 @@ private function getRange(ApiProperty $propertyMetadata): array|string|null
371370
return null;
372371
}
373372

374-
/** @var Type|null $collectionValueType */
375-
$collectionValueType = null;
376-
$typeIsCollection = static function (Type $type) use (&$typeIsCollection, &$collectionValueType): bool {
377-
return match (true) {
378-
$type instanceof CollectionType => null !== $collectionValueType = $type->getCollectionValueType(),
379-
$type instanceof WrappingTypeInterface => $type->wrappedTypeIsSatisfiedBy($typeIsCollection),
380-
$type instanceof CompositeTypeInterface => $type->composedTypesAreSatisfiedBy($typeIsCollection),
381-
default => false,
382-
};
383-
};
384-
385-
if ($nativeType->isSatisfiedBy($typeIsCollection)) {
386-
$nativeType = $collectionValueType;
373+
if ($nativeType->isSatisfiedBy(fn ($t) => $t instanceof CollectionType)) {
374+
$nativeType = TypeHelper::getCollectionValueType($nativeType);
387375
}
388376

389377
// Check for specific types after potentially unwrapping the collection

src/JsonApi/Serializer/ConstraintViolationListNormalizer.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
1818
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
1919
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20-
use Symfony\Component\TypeInfo\Type;
21-
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
2220
use Symfony\Component\TypeInfo\Type\ObjectType;
23-
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
2421
use Symfony\Component\Validator\ConstraintViolationInterface;
2522
use Symfony\Component\Validator\ConstraintViolationListInterface;
2623

@@ -94,15 +91,7 @@ private function getSourcePointerFromViolation(ConstraintViolationInterface $vio
9491
return "data/relationships/$fieldName";
9592
}
9693
} else {
97-
$typeIsObject = static function (Type $type) use (&$typeIsObject): bool {
98-
return match (true) {
99-
$type instanceof WrappingTypeInterface => $type->wrappedTypeIsSatisfiedBy($typeIsObject),
100-
$type instanceof CompositeTypeInterface => $type->composedTypesAreSatisfiedBy($typeIsObject),
101-
default => $type instanceof ObjectType,
102-
};
103-
};
104-
105-
if ($propertyMetadata->getNativeType()?->isSatisfiedBy($typeIsObject)) {
94+
if ($propertyMetadata->getNativeType()?->isSatisfiedBy(fn ($t) => $t instanceof ObjectType)) {
10695
return "data/relationships/$fieldName";
10796
}
10897
}

src/JsonSchema/SchemaFactory.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2323
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2424
use ApiPlatform\Metadata\ResourceClassResolverInterface;
25+
use ApiPlatform\Metadata\Util\TypeHelper;
2526
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
2627
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2728
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
2829
use Symfony\Component\TypeInfo\Type\BuiltinType;
2930
use Symfony\Component\TypeInfo\Type\CollectionType;
3031
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
3132
use Symfony\Component\TypeInfo\Type\ObjectType;
32-
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
3333
use Symfony\Component\TypeInfo\TypeIdentifier;
3434

3535
/**
@@ -331,11 +331,7 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
331331
$isCollection = $t instanceof CollectionType;
332332

333333
if ($isCollection) {
334-
$valueType = $t->getCollectionValueType();
335-
}
336-
337-
while ($valueType instanceof WrappingTypeInterface) {
338-
$valueType = $valueType->getWrappedType();
334+
$valueType = TypeHelper::getCollectionValueType($t);
339335
}
340336

341337
if (!$valueType instanceof ObjectType) {

0 commit comments

Comments
 (0)