Skip to content

Commit

Permalink
Refactor time
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kolb committed Dec 6, 2024
1 parent 58484f8 commit 01f862c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 134 deletions.
38 changes: 10 additions & 28 deletions src/Doctrine/TimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
51 changes: 0 additions & 51 deletions src/Serializer/TimeNormalizer.php

This file was deleted.

51 changes: 0 additions & 51 deletions src/Serializer/YearNormalizer.php

This file was deleted.

22 changes: 18 additions & 4 deletions src/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 01f862c

Please sign in to comment.