Skip to content

Commit

Permalink
Refactor year and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kolb committed Dec 6, 2024
1 parent 01f862c commit bec95c4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
38 changes: 8 additions & 30 deletions src/Doctrine/YearType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,20 @@
namespace DigitalCraftsman\DateTimePrecision\Doctrine;

use DigitalCraftsman\DateTimePrecision\Year;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use DigitalCraftsman\SelfAwareNormalizers\Doctrine\IntNormalizableType;

final class YearType extends Type
/**
* @codeCoverageIgnore
*/
final class YearType extends IntNormalizableType
{
/** @codeCoverageIgnore */
public function getName(): string
public static function getTypeName(): string
{
return 'dtp_year';
}

/** @codeCoverageIgnore */
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
public static function getClass(): string
{
return $platform->getIntegerTypeDeclarationSQL($column);
}

/** @param Year|null $value */
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int
{
return $value === null
? null
: $value->year;
}

/** @param int|null $value */
public function convertToPHPValue($value, AbstractPlatform $platform): ?Year
{
return $value === null
? null
: new Year($value);
}

/** @codeCoverageIgnore */
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
return Year::class;
}
}
16 changes: 15 additions & 1 deletion src/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace DigitalCraftsman\DateTimePrecision;

final readonly class Year
use DigitalCraftsman\SelfAwareNormalizers\Serializer\IntNormalizable;

final readonly class Year implements IntNormalizable
{
// -- Construction

Expand All @@ -31,6 +33,18 @@ public static function fromString(string $year): self
return self::fromDateTime($dateTime);
}

// Int normalizable

public static function denormalize(int $data): self
{
return new self($data);
}

public function normalize(): int
{
return $this->year;
}

// -- Accessors

public function isEqualTo(self $year): bool
Expand Down
8 changes: 5 additions & 3 deletions tests/Moment/ToStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use DigitalCraftsman\DateTimePrecision\Moment;
use PHPUnit\Framework\TestCase;

/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Moment */
/**
* @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Moment
*/
final class ToStringTest extends TestCase
{
/**
Expand All @@ -18,9 +20,9 @@ final class ToStringTest extends TestCase
public function to_string_works(): void
{
// -- Arrange & Act
$dateTime = Moment::fromStringInTimeZone('2022-10-08 15:00:00', new \DateTimeZone('Europe/Berlin'));
$dateTime = Moment::fromStringInTimeZone('2022-10-08 15:00:00.000000', new \DateTimeZone('Europe/Berlin'));

// -- Assert
self::assertEquals('2022-10-08T13:00:00+00:00', (string) $dateTime);
self::assertEquals('2022-10-08 13:00:00.000000', (string) $dateTime);
}
}

0 comments on commit bec95c4

Please sign in to comment.