Skip to content

Commit 41af3ca

Browse files
committed
[TypeInfo][Serializer][PropertyInfo][Validator] TypeInfo 7.1 compatibility
1 parent 5bf5687 commit 41af3ca

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

Mapping/Loader/PropertyInfoLoader.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
use Symfony\Component\TypeInfo\Type\BuiltinType;
2020
use Symfony\Component\TypeInfo\Type\CollectionType;
2121
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
22+
use Symfony\Component\TypeInfo\Type\IntersectionType;
2223
use Symfony\Component\TypeInfo\Type\NullableType;
2324
use Symfony\Component\TypeInfo\Type\ObjectType;
24-
use Symfony\Component\TypeInfo\TypeIdentifier;
25+
use Symfony\Component\TypeInfo\Type\UnionType;
2526
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
27+
use Symfony\Component\TypeInfo\TypeIdentifier;
2628
use Symfony\Component\Validator\Constraints\All;
2729
use Symfony\Component\Validator\Constraints\NotBlank;
2830
use Symfony\Component\Validator\Constraints\NotNull;
@@ -141,7 +143,22 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
141143
}
142144

143145
$type = $types;
144-
$nullable = $type->isNullable();
146+
147+
// BC layer for type-info < 7.2
148+
if (!class_exists(NullableType::class)) {
149+
$nullable = false;
150+
151+
if ($type instanceof UnionType && $type->isNullable()) {
152+
$nullable = true;
153+
$type = $type->asNonNullable();
154+
}
155+
} else {
156+
$nullable = $type->isNullable();
157+
158+
if ($type instanceof NullableType) {
159+
$type = $type->getWrappedType();
160+
}
161+
}
145162

146163
if ($type instanceof NullableType) {
147164
$type = $type->getWrappedType();
@@ -194,6 +211,25 @@ private function getTypeConstraintLegacy(string $builtinType, PropertyInfoType $
194211

195212
private function getTypeConstraint(TypeInfoType $type): ?Type
196213
{
214+
// BC layer for type-info < 7.2
215+
if (!interface_exists(CompositeTypeInterface::class)) {
216+
if ($type instanceof UnionType || $type instanceof IntersectionType) {
217+
return ($type->isA(TypeIdentifier::INT) || $type->isA(TypeIdentifier::FLOAT) || $type->isA(TypeIdentifier::STRING) || $type->isA(TypeIdentifier::BOOL)) ? new Type(['type' => 'scalar']) : null;
218+
}
219+
220+
$baseType = $type->getBaseType();
221+
222+
if ($baseType instanceof ObjectType) {
223+
return new Type(['type' => $baseType->getClassName()]);
224+
}
225+
226+
if (TypeIdentifier::MIXED !== $baseType->getTypeIdentifier()) {
227+
return new Type(['type' => $baseType->getTypeIdentifier()->value]);
228+
}
229+
230+
return null;
231+
}
232+
197233
if ($type instanceof CompositeTypeInterface) {
198234
return $type->isIdentifiedBy(
199235
TypeIdentifier::INT,

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"symfony/property-access": "^6.4|^7.0",
4040
"symfony/property-info": "^6.4|^7.0",
4141
"symfony/translation": "^6.4.3|^7.0.3",
42-
"symfony/type-info": "^7.2-RC1",
42+
"symfony/type-info": "^7.1",
4343
"egulias/email-validator": "^2.1.10|^3|^4"
4444
},
4545
"conflict": {

0 commit comments

Comments
 (0)