diff --git a/src/Doctrine/TimeType.php b/src/Doctrine/TimeType.php index 97e1531..24b45c8 100644 --- a/src/Doctrine/TimeType.php +++ b/src/Doctrine/TimeType.php @@ -5,44 +5,26 @@ namespace DigitalCraftsman\DateTimePrecision\Doctrine; use DigitalCraftsman\DateTimePrecision\Time; +use DigitalCraftsman\SelfAwareNormalizers\Doctrine\StringNormalizableType; use Doctrine\DBAL\Platforms\AbstractPlatform; -use Doctrine\DBAL\Types\Type; -final class TimeType extends Type +/** + * @codeCoverageIgnore + */ +final class TimeType extends StringNormalizableType { - /** @codeCoverageIgnore */ - public function getName(): string + public static function getTypeName(): string { return 'dtp_time'; } - /** @codeCoverageIgnore */ - public function getSQLDeclaration(array $column, AbstractPlatform $platform): string - { - return $platform->getTimeTypeDeclarationSQL($column); - } - - /** @param Time|null $value */ - public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string + public static function getClass(): string { - if ($value === null) { - return null; - } - - return $value->format($platform->getTimeFormatString()); + return Time::class; } - /** @param string|null $value */ - public function convertToPHPValue($value, AbstractPlatform $platform): ?Time - { - return $value === null - ? null - : Time::fromString($value); - } - - /** @codeCoverageIgnore */ - public function requiresSQLCommentHint(AbstractPlatform $platform): bool + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string { - return true; + return $platform->getTimeTypeDeclarationSQL($column); } } diff --git a/src/Serializer/TimeNormalizer.php b/src/Serializer/TimeNormalizer.php deleted file mode 100644 index a3ba4f6..0000000 --- a/src/Serializer/TimeNormalizer.php +++ /dev/null @@ -1,51 +0,0 @@ - - * - * @codeCoverageIgnore - */ - public function getSupportedTypes(?string $format): array - { - return [ - Time::class => true, - ]; - } -} diff --git a/src/Serializer/YearNormalizer.php b/src/Serializer/YearNormalizer.php deleted file mode 100644 index 1ee922b..0000000 --- a/src/Serializer/YearNormalizer.php +++ /dev/null @@ -1,51 +0,0 @@ -year; - } - - /** @param int|null $data */ - public function denormalize($data, $type, $format = null, array $context = []): ?Year - { - return $data === null - ? null - : new Year($data); - } - - /** - * @return array - * - * @codeCoverageIgnore - */ - public function getSupportedTypes(?string $format): array - { - return [ - Year::class => true, - ]; - } -} diff --git a/src/Time.php b/src/Time.php index 2186d9c..79ba359 100644 --- a/src/Time.php +++ b/src/Time.php @@ -4,10 +4,12 @@ namespace DigitalCraftsman\DateTimePrecision; -final readonly class Time implements \Stringable +use DigitalCraftsman\SelfAwareNormalizers\Serializer\StringNormalizable; + +final readonly class Time implements \Stringable, StringNormalizable { - private const TIME_FORMAT = 'H:i:s.u'; - private const MINUTES_IN_AN_HOUR = 60; + private const string TIME_FORMAT = 'H:i:s.u'; + private const int MINUTES_IN_AN_HOUR = 60; // -- Construction @@ -74,13 +76,25 @@ public static function fromString(string $time): self } } - // Stringable + // -- Stringable public function __toString(): string { return $this->format(self::TIME_FORMAT); } + // -- String normalizable + + public static function denormalize(string $data): self + { + return self::fromString($data); + } + + public function normalize(): string + { + return $this->format(self::TIME_FORMAT); + } + // -- Accessors public function isAfter(self $time): bool