Skip to content

Commit

Permalink
Merge pull request #14 from sitegeist/bugfix/ensureAnExceptionIsThrow…
Browse files Browse the repository at this point in the history
…nWhenDatesCannotBeDeseiralized

BUGFIX: Ensure failed date denormalization will yield an exception instead of `false`
  • Loading branch information
mficzel authored Apr 17, 2024
2 parents 3c09dea + 46aaee8 commit fdc1db7
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions Classes/Domain/Schema/SchemaDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,11 @@ private static function convertValue(null|int|bool|string|float|array $value, st
} elseif ($targetType === 'bool') {
return (bool) $value;
} elseif ($targetType === \DateTime::class) {
return match (true) {
is_string($value) => \DateTime::createFromFormat(\DateTimeInterface::RFC3339, $value),
default => throw new \DomainException('Can only denormalize \DateTime from an RFC 3339 string')
};
return self::convertDateTime($value);
} elseif ($targetType === \DateTimeImmutable::class) {
return match (true) {
is_string($value) => \DateTimeImmutable::createFromFormat(\DateTimeInterface::RFC3339, $value),
default => throw new \DomainException('Can only denormalize \DateTimeImmutable from an RFC 3339 string')
};
return self::convertDateTimeImmutable($value);
} elseif ($targetType === \DateInterval::class) {
return match (true) {
is_string($value) => new \DateInterval($value),
default => throw new \DomainException('Can only denormalize \DateInterval from string')
};
return self::convertDateInterval($value);
} elseif (
// Enums are final, so is_a suffices
is_a($targetType, \BackedEnum::class, true)
Expand Down Expand Up @@ -114,4 +105,49 @@ private static function convertValueObject(array|int|float|string|bool $value, s
}
throw new \DomainException('Only single value objects can be serialized as single value');
}

/**
* @param array<string,mixed>|int|float|string|bool $value
*/
protected static function convertDateTime(array|float|bool|int|string $value): \DateTime
{
$converted = match (true) {
is_string($value) => \DateTime::createFromFormat(\DateTimeInterface::RFC3339, $value),
default => false,
};
if ($converted === false) {
throw new \DomainException('Can only denormalize \DateTime from an RFC 3339 string');
}
return $converted;
}

/**
* @param array<string,mixed>|int|float|string|bool $value
*/
protected static function convertDateTimeImmutable(array|float|bool|int|string $value): \DateTimeImmutable
{
$converted = match (true) {
is_string($value) => \DateTimeImmutable::createFromFormat(\DateTimeInterface::RFC3339, $value),
default => false,
};
if ($converted === false) {
throw new \DomainException('Can only denormalize \DateTimeImmutable from an RFC 3339 string');
}
return $converted;
}

/**
* @param array<string,mixed>|int|float|string|bool $value
*/
protected static function convertDateInterval(array|float|bool|int|string $value): \DateInterval
{
$converted = match (true) {
is_string($value) => new \DateInterval($value),
default => false,
};
if ($converted === false) {
throw new \DomainException('Can only denormalize \DateInterval from an ISO 8601 string');
}
return $converted;
}
}

0 comments on commit fdc1db7

Please sign in to comment.