Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ jobs:
if: ${{ matrix.composer-stability }}
run: composer config minimum-stability ${{ matrix.composer-stability }}

- name: Install symfony/clock for PHP >= 8.1
if: ${{ matrix.php-version >= '8.1' }}
run: composer require --dev symfony/clock:"^6.4 || ^7.0 || ^8.0" --no-update

- name: Install dependencies with Composer
uses: ramsey/composer-install@v3
with:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ jobs:
tools: "cs2pr"
extensions: pdo_sqlite

- name: Install symfony/clock for PHP >= 8.1
if: ${{ matrix.php-version >= '8.1' }}
run: composer require --dev symfony/clock:"^6.4 || ^7.0 || ^8.0" --no-update

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v3"
with:
Expand Down
5 changes: 4 additions & 1 deletion phpstan/ignore-by-php-version.neon.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php declare(strict_types = 1);

use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\Clock\DatePoint;
use Symfony\Component\Uid\UuidV7;

$includes = [];
Expand Down Expand Up @@ -28,7 +29,9 @@
if(!class_exists(UuidV7::class)) {
$includes[] = __DIR__ . '/no-uuid-7.neon';
}

if (!class_exists(DatePoint::class)) {
$includes[] = __DIR__ . '/no-datepoint.neon';
Comment thread Dismissed
}

$config = [];
$config['includes'] = $includes;
Expand Down
3 changes: 3 additions & 0 deletions phpstan/no-datepoint.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
Comment thread Dismissed
ignoreErrors:
- '~Call to static method createFromInterface\(\) on an unknown class Symfony\\Component\\Clock\\DatePoint.~'
55 changes: 55 additions & 0 deletions src/Handler/DateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
use JMS\Serializer\XmlSerializationVisitor;
use Symfony\Component\Clock\DatePoint;

/**
* @phpstan-import-type TypeArray from Type
Expand Down Expand Up @@ -45,6 +46,11 @@ public static function getSubscribingMethods()
$methods = [];
$types = [\DateTime::class, \DateTimeImmutable::class, \DateInterval::class];

// Add Symfony's DatePoint if available (introduced in Symfony 6.4)
if (class_exists(DatePoint::class)) {
$types[] = DatePoint::class;
}

foreach (['json', 'xml'] as $format) {
foreach ($types as $type) {
$methods[] = [
Expand Down Expand Up @@ -156,6 +162,20 @@ public function serializeDateInterval(SerializationVisitorInterface $visitor, \D
return $visitor->visitString($iso8601DateIntervalString, $type);
}

/**
* @param TypeArray $type
*
* @return \DOMCdataSection|\DOMText|mixed
*/
public function serializeSymfonyComponentClockDatePoint(
SerializationVisitorInterface $visitor,
\DateTimeImmutable $date,
array $type,
SerializationContext $context
) {
return $this->serializeDateTimeInterface($visitor, $date, $type, $context);
}

/**
* @param mixed $data
*/
Expand Down Expand Up @@ -244,6 +264,32 @@ public function deserializeDateIntervalFromJson(DeserializationVisitorInterface
return $this->parseDateInterval($data);
}

/**
* @param mixed $data
* @param TypeArray $type
*/
public function deserializeSymfonyComponentClockDatePointFromXml(DeserializationVisitorInterface $visitor, $data, array $type): ?\DateTimeInterface
{
if ($this->isDataXmlNull($data)) {
return null;
}

return $this->parseDateTimeAsDatePoint($data, $type);
}

/**
* @param mixed $data
* @param TypeArray $type
*/
public function deserializeSymfonyComponentClockDatePointFromJson(DeserializationVisitorInterface $visitor, $data, array $type): ?\DateTimeInterface
{
if (empty($data)) {
return null;
}

return $this->parseDateTimeAsDatePoint($data, $type);
}

/**
* @param mixed $data
* @param TypeArray $type
Expand Down Expand Up @@ -279,6 +325,15 @@ private function parseDateTime($data, array $type, bool $immutable = false): \Da
));
}

/**
* @param mixed $data
* @param TypeArray $type
*/
private function parseDateTimeAsDatePoint($data, array $type): \DateTimeInterface
{
return DatePoint::createFromInterface($this->parseDateTime($data, $type));
}

private function parseDateInterval(string $data): \DateInterval
{
$dateInterval = null;
Expand Down
62 changes: 62 additions & 0 deletions tests/Handler/DateHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Clock\DatePoint;

class DateHandlerTest extends TestCase
{
Expand Down Expand Up @@ -188,4 +189,65 @@ public function testDefaultFormat()
$this->handler->deserializeDateTimeFromJson($visitor, '2017-06-18T17:32:11Z', $type),
);
}

public function testDatePointSerializationJson()
{
if (!class_exists(DatePoint::class)) {
self::markTestSkipped('Symfony Clock component is not available');
}

$context = $this->getMockBuilder(SerializationContext::class)->getMock();

$visitor = $this->getMockBuilder(SerializationVisitorInterface::class)->getMock();
$visitor->method('visitString')->with('2017-06-18');

$datePoint = new DatePoint('2017-06-18 14:30:59', $this->timezone);
$type = ['name' => DatePoint::class, 'params' => ['Y-m-d']];
$this->handler->serializeSymfonyComponentClockDatePoint($visitor, $datePoint, $type, $context);
}

public function testDatePointDeserializationJson()
{
if (!class_exists(DatePoint::class)) {
self::markTestSkipped('Symfony Clock component is not available');
}

$visitor = new JsonDeserializationVisitor();

$type = ['name' => DatePoint::class, 'params' => ['Y-m-d', '', 'Y-m-d|']];
$result = $this->handler->deserializeSymfonyComponentClockDatePointFromJson($visitor, '2017-06-18', $type);

self::assertInstanceOf(DatePoint::class, $result);
self::assertEquals('2017-06-18', $result->format('Y-m-d'));
}

public function testDatePointDeserializationReturnsNullForEmptyString()
{
if (!class_exists(DatePoint::class)) {
self::markTestSkipped('Symfony Clock component is not available');
}

$visitor = new JsonDeserializationVisitor();

$type = ['name' => DatePoint::class, 'params' => ['Y-m-d']];
self::assertNull($this->handler->deserializeSymfonyComponentClockDatePointFromJson($visitor, '', $type));
}

public function testDatePointWithTimezone()
{
if (!class_exists(DatePoint::class)) {
self::markTestSkipped('Symfony Clock component is not available');
}

$visitor = new JsonDeserializationVisitor();

$timestamp = (string) time();
$timezone = 'Europe/Brussels';
$type = ['name' => DatePoint::class, 'params' => ['U', $timezone]];

$result = $this->handler->deserializeSymfonyComponentClockDatePointFromJson($visitor, $timestamp, $type);

self::assertInstanceOf(DatePoint::class, $result);
self::assertEquals($timezone, $result->getTimezone()->getName());
}
}
Loading