Thanks to visit codestin.com
Credit goes to github.com

Skip to content

cast integer to string in case date time format is „U“ #50272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ public function normalize(mixed $object, string $format = null, array $context =
return $object->format($dateTimeFormat);
}

/**
* @param array $context
*/
public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */): bool
{
return $data instanceof \DateTimeInterface;
Expand All @@ -96,7 +93,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
$timezone = $this->getTimezone($context);

if (null === $data || !\is_string($data) || '' === trim($data)) {
if (null === $data || (!\is_string($data) && !(\is_int($data) && 'U' === $dateTimeFormat)) || '' === trim($data)) {
throw NotNormalizableValueException::createForUnexpectedDataType('The data is either not an string, an empty string, or null; you should pass a string that can be parsed with the passed format or a valid DateTime string.', $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true);
}

Expand Down Expand Up @@ -131,9 +128,6 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
}
}

/**
* @param array $context
*/
public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */): bool
{
return isset(self::SUPPORTED_TYPES[$type]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,11 @@ public function testDenormalizeFormatMismatchThrowsException()
$this->expectException(UnexpectedValueException::class);
$this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class, null, [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d|']);
}

public function testDenormalizeDateTimeIntegerWithTimestampFormat()
{
$timestamp = time();
$denormalizedDate = $this->normalizer->denormalize($timestamp, \DateTimeImmutable::class, null, [DateTimeNormalizer::FORMAT_KEY => 'U']);
$this->assertSame($timestamp, $denormalizedDate->getTimestamp());
}
}