You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// according to https://www.w3.org/TR/xmlschema-2/#boolean, valid representations are "false", "true", "0" and "1"
475
-
if ('false' === $data || '0' === $data) {
476
-
$data = false;
477
-
} elseif ('true' === $data || '1' === $data) {
478
-
$data = true;
479
-
} else {
480
-
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute for class "%s" must be bool ("%s" given).', $attribute, $currentClass, $data), $data, [Type::BUILTIN_TYPE_BOOL], $context['deserialization_path'] ?? null);
481
-
}
482
-
break;
483
-
case Type::BUILTIN_TYPE_INT:
484
-
if (ctype_digit($data) || '-' === $data[0] && ctype_digit(substr($data, 1))) {
485
-
$data = (int) $data;
486
-
} else {
487
-
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute for class "%s" must be int ("%s" given).', $attribute, $currentClass, $data), $data, [Type::BUILTIN_TYPE_INT], $context['deserialization_path'] ?? null);
488
-
}
489
-
break;
490
-
case Type::BUILTIN_TYPE_FLOAT:
491
-
if (is_numeric($data)) {
492
-
return (float) $data;
459
+
// This try-catch should cover all NotNormalizableValueException (and all return branches after the first
460
+
// exception) so we could try denormalizing all types of an union type. If the target type is not an union
461
+
// type, we will just re-throw the catched exception.
462
+
// In the case of no denormalization succeeds with an union type, it will fall back to the default exception
463
+
// with the acceptable types list.
464
+
try {
465
+
// In XML and CSV all basic datatypes are represented as strings, it is e.g. not possible to determine,
466
+
// if a value is meant to be a string, float, int or a boolean value from the serialized representation.
467
+
// That's why we have to transform the values, if one of these non-string basic datatypes is expected.
if (Type::BUILTIN_TYPE_ARRAY === $builtinType = $type->getBuiltinType()) {
471
+
return [];
493
472
}
494
473
495
-
switch ($data) {
496
-
case'NaN':
497
-
return \NAN;
498
-
case'INF':
499
-
return \INF;
500
-
case'-INF':
501
-
return -\INF;
502
-
default:
503
-
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute for class "%s" must be float ("%s" given).', $attribute, $currentClass, $data), $data, [Type::BUILTIN_TYPE_FLOAT], $context['deserialization_path'] ?? null);
474
+
if ($type->isNullable() && \in_array($builtinType, [Type::BUILTIN_TYPE_BOOL, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT], true)) {
// according to https://www.w3.org/TR/xmlschema-2/#boolean, valid representations are "false", "true", "0" and "1"
482
+
if ('false' === $data || '0' === $data) {
483
+
$data = false;
484
+
} elseif ('true' === $data || '1' === $data) {
485
+
$data = true;
486
+
} else {
487
+
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute for class "%s" must be bool ("%s" given).', $attribute, $currentClass, $data), $data, [Type::BUILTIN_TYPE_BOOL], $context['deserialization_path'] ?? null);
488
+
}
489
+
break;
490
+
case Type::BUILTIN_TYPE_INT:
491
+
if (ctype_digit($data) || '-' === $data[0] && ctype_digit(substr($data, 1))) {
492
+
$data = (int) $data;
493
+
} else {
494
+
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute for class "%s" must be int ("%s" given).', $attribute, $currentClass, $data), $data, [Type::BUILTIN_TYPE_INT], $context['deserialization_path'] ?? null);
495
+
}
496
+
break;
497
+
case Type::BUILTIN_TYPE_FLOAT:
498
+
if (is_numeric($data)) {
499
+
return (float) $data;
500
+
}
501
+
502
+
switch ($data) {
503
+
case'NaN':
504
+
return \NAN;
505
+
case'INF':
506
+
return \INF;
507
+
case'-INF':
508
+
return -\INF;
509
+
default:
510
+
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute for class "%s" must be float ("%s" given).', $attribute, $currentClass, $data), $data, [Type::BUILTIN_TYPE_FLOAT], $context['deserialization_path'] ?? null);
511
+
}
512
+
}
505
513
}
506
-
}
507
514
508
-
if (null !== $collectionValueType && Type::BUILTIN_TYPE_OBJECT === $collectionValueType->getBuiltinType()) {
if (!$this->serializerinstanceof DenormalizerInterface) {
544
-
thrownewLogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer.', $attribute, $class));
545
-
}
549
+
if (Type::BUILTIN_TYPE_OBJECT === $builtinType) {
550
+
if (!$this->serializerinstanceof DenormalizerInterface) {
551
+
thrownewLogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer.', $attribute, $class));
0 commit comments