Skip to content

Commit

Permalink
Refactor moment
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kolb committed Dec 6, 2024
1 parent 4935237 commit 20bebb3
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 140 deletions.
40 changes: 11 additions & 29 deletions src/Doctrine/MomentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@
namespace DigitalCraftsman\DateTimePrecision\Doctrine;

use DigitalCraftsman\DateTimePrecision\Moment;
use DigitalCraftsman\SelfAwareNormalizers\Doctrine\StringNormalizableType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Types\Type;

final class MomentType extends Type
/**
* @codeCoverageIgnore
*/
final class MomentType extends StringNormalizableType
{
/** @codeCoverageIgnore */
public function getName(): string
public static function getTypeName(): string
{
return 'dtp_moment';
}

/** @codeCoverageIgnore */
public static function getClass(): string
{
return Moment::class;
}

public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
if ($platform instanceof PostgreSQLPlatform) {
Expand All @@ -31,28 +37,4 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform): st

throw new \RuntimeException('Unsupported platform');
}

/** @param Moment|null $value */
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null) {
return null;
}

return $value->format('Y-m-d H:i:s.u');
}

/** @param string|null $value */
public function convertToPHPValue($value, AbstractPlatform $platform): ?Moment
{
return $value === null
? null
: Moment::fromString($value);
}

/** @codeCoverageIgnore */
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}
}
20 changes: 17 additions & 3 deletions src/Moment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace DigitalCraftsman\DateTimePrecision;

final readonly class Moment implements \Stringable
use DigitalCraftsman\SelfAwareNormalizers\Serializer\StringNormalizable;

final readonly class Moment implements \Stringable, StringNormalizable
{
private const DATE_TIME_FORMAT = \DateTimeInterface::ATOM;
private const string DATE_TIME_FORMAT_INCLUDING_MILLISECONDS = 'Y-m-d H:i:s.u';

// -- Construction

Expand Down Expand Up @@ -39,7 +41,19 @@ public static function fromDateTime(\DateTimeImmutable $dateTime): self

public function __toString(): string
{
return $this->format(self::DATE_TIME_FORMAT);
return $this->format(self::DATE_TIME_FORMAT_INCLUDING_MILLISECONDS);
}

// -- String normalizable

public static function denormalize(string $data): self
{
return new self(new \DateTimeImmutable($data, new \DateTimeZone('UTC')));
}

public function normalize(): string
{
return $this->format(self::DATE_TIME_FORMAT_INCLUDING_MILLISECONDS);
}

// -- Accessors
Expand Down
3 changes: 0 additions & 3 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ services:
class: DigitalCraftsman\DateTimePrecision\Clock\SystemClock

# Normalizers
DigitalCraftsman\DateTimePrecision\Serializer\MomentNormalizer:
tags: [ { name: 'serializer.normalizer' } ]

DigitalCraftsman\DateTimePrecision\Serializer\TimeNormalizer:
tags: [ { name: 'serializer.normalizer' } ]

Expand Down
51 changes: 0 additions & 51 deletions src/Serializer/MomentNormalizer.php

This file was deleted.

54 changes: 0 additions & 54 deletions tests/Doctrine/DateTimeTypeTest.php

This file was deleted.

35 changes: 35 additions & 0 deletions tests/Moment/NormalizeDenormalizeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DigitalCraftsman\DateTimePrecision\Moment;

use DigitalCraftsman\DateTimePrecision\Moment;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Moment
*/
final class NormalizeDenormalizeTest extends TestCase
{
/**
* @test
*
* @covers ::normalize
* @covers ::denormalize
*/
public function normalize_and_denormalize_works(): void
{
// -- Arrange
$moment = Moment::fromString('2022-10-08 15:30:00.123456');
$data = '2022-10-08 15:30:00.123456';

// -- Act
$normalized = $moment->normalize();
$denormalized = Moment::denormalize($data);

// -- Assert
self::assertSame($data, $normalized);
self::assertEquals($moment, $denormalized);
}
}

0 comments on commit 20bebb3

Please sign in to comment.