Skip to content

Commit 39a0d0b

Browse files
committed
[Serializer][PropertyInfo][Validator] TypeInfo 7.2 compatibility
1 parent 06044be commit 39a0d0b

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

Mapping/Loader/PropertyInfoLoader.php

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
1717
use Symfony\Component\PropertyInfo\Type as PropertyInfoType;
1818
use Symfony\Component\TypeInfo\Type as TypeInfoType;
19+
use Symfony\Component\TypeInfo\Type\BuiltinType;
1920
use Symfony\Component\TypeInfo\Type\CollectionType;
21+
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
2022
use Symfony\Component\TypeInfo\Type\IntersectionType;
23+
use Symfony\Component\TypeInfo\Type\NullableType;
2124
use Symfony\Component\TypeInfo\Type\ObjectType;
2225
use Symfony\Component\TypeInfo\Type\UnionType;
26+
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
2327
use Symfony\Component\TypeInfo\TypeIdentifier;
2428
use Symfony\Component\Validator\Constraints\All;
2529
use Symfony\Component\Validator\Constraints\NotBlank;
@@ -143,7 +147,23 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
143147
}
144148

145149
$type = $types;
146-
$nullable = false;
150+
151+
// BC layer for type-info < 7.2
152+
if (!class_exists(NullableType::class)) {
153+
$nullable = false;
154+
155+
if ($type instanceof UnionType && $type->isNullable()) {
156+
$nullable = true;
157+
$type = $type->asNonNullable();
158+
}
159+
} else {
160+
$nullable = $type->isNullable();
161+
162+
if ($type instanceof NullableType) {
163+
$type = $type->getWrappedType();
164+
}
165+
}
166+
147167

148168
if ($type instanceof UnionType && $type->isNullable()) {
149169
$nullable = true;
@@ -197,18 +217,46 @@ private function getTypeConstraintLegacy(string $builtinType, PropertyInfoType $
197217

198218
private function getTypeConstraint(TypeInfoType $type): ?Type
199219
{
200-
if ($type instanceof UnionType || $type instanceof IntersectionType) {
201-
return ($type->isA(TypeIdentifier::INT) || $type->isA(TypeIdentifier::FLOAT) || $type->isA(TypeIdentifier::STRING) || $type->isA(TypeIdentifier::BOOL)) ? new Type(['type' => 'scalar']) : null;
220+
// BC layer for type-info < 7.2
221+
if (!interface_exists(CompositeTypeInterface::class)) {
222+
if ($type instanceof UnionType || $type instanceof IntersectionType) {
223+
return ($type->isA(TypeIdentifier::INT) || $type->isA(TypeIdentifier::FLOAT) || $type->isA(TypeIdentifier::STRING) || $type->isA(TypeIdentifier::BOOL)) ? new Type(['type' => 'scalar']) : null;
224+
}
225+
226+
$baseType = $type->getBaseType();
227+
228+
if ($baseType instanceof ObjectType) {
229+
return new Type(['type' => $baseType->getClassName()]);
230+
}
231+
232+
if (TypeIdentifier::MIXED !== $baseType->getTypeIdentifier()) {
233+
return new Type(['type' => $baseType->getTypeIdentifier()->value]);
234+
}
235+
236+
return null;
237+
}
238+
239+
if ($type instanceof CompositeTypeInterface) {
240+
return $type->isIdentifiedBy(
241+
TypeIdentifier::INT,
242+
TypeIdentifier::FLOAT,
243+
TypeIdentifier::STRING,
244+
TypeIdentifier::BOOL,
245+
TypeIdentifier::TRUE,
246+
TypeIdentifier::FALSE,
247+
) ? new Type(['type' => 'scalar']) : null;
202248
}
203249

204-
$baseType = $type->getBaseType();
250+
while ($type instanceof WrappingTypeInterface) {
251+
$type = $type->getWrappedType();
252+
}
205253

206-
if ($baseType instanceof ObjectType) {
207-
return new Type(['type' => $baseType->getClassName()]);
254+
if ($type instanceof ObjectType) {
255+
return new Type(['type' => $type->getClassName()]);
208256
}
209257

210-
if (TypeIdentifier::MIXED !== $baseType->getTypeIdentifier()) {
211-
return new Type(['type' => $baseType->getTypeIdentifier()->value]);
258+
if ($type instanceof BuiltinType && TypeIdentifier::MIXED !== $type->getTypeIdentifier()) {
259+
return new Type(['type' => $type->getTypeIdentifier()->value]);
212260
}
213261

214262
return null;

0 commit comments

Comments
 (0)