-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christian Kolb
committed
Jun 9, 2024
1 parent
e10fb10
commit 78e5028
Showing
6 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Doctrine; | ||
|
||
use DigitalCraftsman\DateTimePrecision\Weekdays; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\JsonType; | ||
|
||
final class WeekdaysType extends JsonType | ||
{ | ||
/** @codeCoverageIgnore */ | ||
public function getName(): string | ||
{ | ||
return 'dtp_weekdays'; | ||
} | ||
|
||
/** @param Weekdays|null $value */ | ||
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Serializer; | ||
|
||
use DigitalCraftsman\DateTimePrecision\Weekdays; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
final class WeekdaysNormalizer implements NormalizerInterface, DenormalizerInterface | ||
{ | ||
public function supportsNormalization($data, $format = null, array $context = []): bool | ||
{ | ||
return $data instanceof Weekdays; | ||
} | ||
|
||
/** @param class-string $type */ | ||
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool | ||
{ | ||
return $type === Weekdays::class; | ||
} | ||
|
||
/** @param Weekdays|null $object */ | ||
public function normalize($object, $format = null, array $context = []): ?array | ||
{ | ||
return $object?->normalize(); | ||
} | ||
|
||
/** @param ?array $data */ | ||
public function denormalize($data, $type, $format = null, array $context = []): ?Weekdays | ||
{ | ||
return $data === null | ||
? null | ||
: Weekdays::denormalize($data); | ||
} | ||
|
||
/** | ||
* @return array<class-string, bool> | ||
* | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getSupportedTypes(?string $format): array | ||
{ | ||
return [ | ||
Weekdays::class => true, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision; | ||
|
||
final readonly class Weekdays | ||
{ | ||
// Construction | ||
|
||
/** @param array<int, Weekday> $weekdays */ | ||
public function __construct( | ||
/** @var array<int, Weekday> $weekdays */ | ||
public array $weekdays, | ||
) { | ||
// TODO: Validate for unique | ||
} | ||
|
||
// -- Array normalizable | ||
|
||
/** @param array<int, string> $array */ | ||
public static function denormalize(array $array): self | ||
{ | ||
$weekdays = []; | ||
foreach ($array as $value) { | ||
$weekdays[] = Weekday::from($value); | ||
} | ||
|
||
return new self($weekdays); | ||
} | ||
|
||
/** @return array<int, string> */ | ||
public function normalize(): array | ||
{ | ||
$weekdayStrings = []; | ||
foreach ($this->weekdays as $weekday) { | ||
$weekdayStrings[] = $weekday->value; | ||
} | ||
|
||
return $weekdayStrings; | ||
} | ||
|
||
// Accessors | ||
|
||
public function contains(Weekday $weekday): bool | ||
{ | ||
return in_array($weekday, $this->weekdays, true); | ||
} | ||
|
||
public function notContains(Weekday $weekday): bool | ||
{ | ||
return !in_array($weekday, $this->weekdays, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters