Skip to content

Commit

Permalink
Refactor weekdays
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kolb committed Dec 6, 2024
1 parent ce76c40 commit 58484f8
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 628 deletions.
48 changes: 8 additions & 40 deletions src/Doctrine/WeekdaysType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,20 @@
namespace DigitalCraftsman\DateTimePrecision\Doctrine;

use DigitalCraftsman\DateTimePrecision\Weekdays;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\JsonType;
use DigitalCraftsman\SelfAwareNormalizers\Doctrine\ArrayNormalizableType;

final class WeekdaysType extends JsonType
/**
* @codeCoverageIgnore
*/
final class WeekdaysType extends ArrayNormalizableType
{
/** @codeCoverageIgnore */
public function getName(): string
public static function getTypeName(): string
{
return 'dtp_weekdays';
}

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

$array = $value->normalize();

return json_encode($array, JSON_THROW_ON_ERROR);
}

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

$array = json_decode($value, true, 512, JSON_THROW_ON_ERROR);

return Weekdays::denormalize($array);
}

/** @codeCoverageIgnore */
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
$column['jsonb'] = true;

return $platform->getJsonTypeDeclarationSQL($column);
}

/** @codeCoverageIgnore */
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
return Weekdays::class;
}
}
10 changes: 0 additions & 10 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,3 @@ services:

DigitalCraftsman\DateTimePrecision\Clock\SystemClock:
class: DigitalCraftsman\DateTimePrecision\Clock\SystemClock

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

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

DigitalCraftsman\DateTimePrecision\Serializer\YearNormalizer:
tags: [ { name: 'serializer.normalizer' } ]
49 changes: 0 additions & 49 deletions src/Serializer/WeekdaysNormalizer.php

This file was deleted.

28 changes: 19 additions & 9 deletions src/Weekdays.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

namespace DigitalCraftsman\DateTimePrecision;

final readonly class Weekdays
use DigitalCraftsman\SelfAwareNormalizers\Serializer\ArrayNormalizable;

final readonly class Weekdays implements ArrayNormalizable
{
// Construction
// -- Construction

/** @param array<int, Weekday> $weekdays */
/**
* @param array<int, Weekday> $weekdays
*/
public function __construct(
/** @var array<int, Weekday> $weekdays */
/**
* @var array<int, Weekday> $weekdays
*/
public array $weekdays,
) {
$enumValues = [];
Expand All @@ -24,18 +30,22 @@ public function __construct(

// -- Array normalizable

/** @param array<int, string> $array */
public static function denormalize(array $array): self
/**
* @param array<int, string> $data
*/
public static function denormalize(array $data): self
{
$weekdays = [];
foreach ($array as $value) {
foreach ($data as $value) {
$weekdays[] = Weekday::from($value);
}

return new self($weekdays);
}

/** @return array<int, string> */
/**
* @return array<int, string>
*/
public function normalize(): array
{
$weekdayStrings = [];
Expand All @@ -46,7 +56,7 @@ public function normalize(): array
return $weekdayStrings;
}

// Accessors
// -- Accessors

public function contains(Weekday $weekday): bool
{
Expand Down
54 changes: 0 additions & 54 deletions tests/Doctrine/TimeTypeTest.php

This file was deleted.

63 changes: 0 additions & 63 deletions tests/Doctrine/WeekdaysTypeTest.php

This file was deleted.

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

This file was deleted.

Loading

0 comments on commit 58484f8

Please sign in to comment.