Skip to content
Open
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
5 changes: 1 addition & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ jobs:
matrix:
php-versions: [ '7.3', '7.4', '8.0' ]
phpunit-versions: [ 'latest' ]
phpstan-versions: [ 'latest' ]
include:
- php-versions: '7.1'
phpunit-versions: '7.5.20'
phpstan-versions: [ 'latest' ]
- php-versions: '7.2'
phpunit-versions: '8.5.15'
phpstan-versions: [ 'latest' ]
steps:
- uses: actions/checkout@v2

Expand All @@ -31,7 +28,7 @@ jobs:
with:
php-version: ${{ matrix.php-versions }}
coverage: xdebug
tools: composer:v2, phpunit:${{ matrix.phpunit-versions }}, phpstan:${{ matrix.phpstan-versions }}
tools: composer:v2, phpunit:${{ matrix.phpunit-versions }}, phpstan

- name: Install dependencies
run: composer update --prefer-dist --no-progress --no-dev
Expand Down
12 changes: 12 additions & 0 deletions src/Abstraction/AbstractDateTimeImmutableNormalizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@
abstract class AbstractDateTimeImmutableNormalizable extends DateTimeImmutable implements NormalizableInterface, DenormalizableInterface
{
use AbstractDateTimeNormalizableTrait;

/**
* @inheritDoc
* @return static
*/
public static function createFromMutable($object)
{
/** @var static $dateTimeImmutable */
$dateTimeImmutable = parent::createFromMutable($object);

return $dateTimeImmutable;
}
}
2 changes: 1 addition & 1 deletion src/Abstraction/AbstractThrowableToNormalizableAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final protected function getNormalized(): array
}

/**
* @return array<string, mixed>
* @return array<int, array<string, mixed>>
*/
abstract protected function getTrace(Throwable $throwable): array;

Expand Down
16 changes: 16 additions & 0 deletions src/Object/DateTimeImmutableRfc3339Normalizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@

namespace Era269\Normalizable\Object;

use DateTimeInterface;
use Era269\Normalizable\Abstraction\AbstractDateTimeImmutableNormalizable;

final class DateTimeImmutableRfc3339Normalizable extends AbstractDateTimeImmutableNormalizable
{
private const FIELD_NAME_DATE_TIME = 'dateTime';

/**
* @inheritDoc
*/
public static function createFromInterface(DateTimeInterface $object): self
{
$phpVersion = 80000;
if (PHP_VERSION_ID < $phpVersion) {
self::throwInvalidMethodCallException($phpVersion, __METHOD__);
}
/** @var self $dateTime */
$dateTime = parent::createFromInterface($object);

return $dateTime;
}

protected static function getDateTimeFieldName(): string
{
return self::FIELD_NAME_DATE_TIME;
Expand Down
16 changes: 16 additions & 0 deletions src/Object/DateTimeRfc3339Normalizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@

namespace Era269\Normalizable\Object;

use DateTimeInterface;
use Era269\Normalizable\Abstraction\AbstractDateTimeNormalizable;

final class DateTimeRfc3339Normalizable extends AbstractDateTimeNormalizable
{
private const FIELD_NAME_DATE_TIME = 'dateTime';

/**
* @inheritDoc
*/
public static function createFromInterface(DateTimeInterface $object): self
{
$phpVersion = 80000;
if (PHP_VERSION_ID < $phpVersion) {
self::throwInvalidMethodCallException($phpVersion, __METHOD__);
}
/** @var self $dateTime */
$dateTime = parent::createFromInterface($object);

return $dateTime;
}

protected static function getDateTimeFieldName(): string
{
return self::FIELD_NAME_DATE_TIME;
Expand Down
19 changes: 19 additions & 0 deletions src/Traits/AbstractDateTimeNormalizableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Era269\Normalizable\Traits;

use BadMethodCallException;
use DateTimeInterface;
use DateTimeZone;
use Exception;
Expand Down Expand Up @@ -53,4 +54,22 @@ final protected function getObjectForNormalization(): DateTimeInterface
}

abstract protected function getDateTimeFormat(): string;

final protected static function throwInvalidMethodCallException(int $phpVersion, string $methodName): void
{
throw new BadMethodCallException(sprintf(
'Method "%s" is available starting with %s PHP version',
$phpVersion,
$methodName
));
}

/**
* @inheritDoc
* @return static|false
*/
public static function createFromFormat($format, $datetime, $timezone = null)
{
return parent::createFromFormat($format, $datetime, $timezone);
}
}
44 changes: 44 additions & 0 deletions tests/Object/DateTimeImmutableRfc3339NormalizableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Era269\Normalizable\Tests\Object;

use BadMethodCallException;
use DateTime;
use DateTimeImmutable;
use Era269\Normalizable\Object\DateTimeImmutableRfc3339Normalizable;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;
Expand All @@ -24,6 +27,47 @@ public function testNormalize(): DateTimeImmutableRfc3339Normalizable
return $datetime;
}

public function testCreateFromInterface(): void
{
$dateTime = new DateTimeImmutable();
if (PHP_VERSION_ID >= 80000) {
$dateTimeFrom = DateTimeImmutableRfc3339Normalizable::createFromInterface($dateTime);
self::assertInstanceOf(DateTimeImmutableRfc3339Normalizable::class, $dateTimeFrom);
self::assertEquals(
$dateTime->format(DATE_RFC3339),
$dateTimeFrom->format(DATE_RFC3339)
);
} else {
$this->expectException(BadMethodCallException::class);
DateTimeImmutableRfc3339Normalizable::createFromInterface($dateTime);
}
}

public function testCreateFromFormat(): void
{
$dateTime = new DateTimeImmutable();
$dateTimeFrom = DateTimeImmutableRfc3339Normalizable::createFromFormat(
DATE_RFC3339,
$dateTime->format(DATE_RFC3339)
);
self::assertInstanceOf(DateTimeImmutableRfc3339Normalizable::class, $dateTimeFrom);
self::assertEquals(
$dateTime->format(DATE_RFC3339),
$dateTimeFrom->format(DATE_RFC3339)
);
}

public function testCreateFromMutable(): void
{
$dateTime = new DateTime();
$dateTimeFrom = DateTimeImmutableRfc3339Normalizable::createFromMutable($dateTime);
self::assertInstanceOf(DateTimeImmutableRfc3339Normalizable::class, $dateTimeFrom);
self::assertEquals(
$dateTime->format(DATE_RFC3339),
$dateTimeFrom->format(DATE_RFC3339)
);
}

/**
* @depends testNormalize
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/Object/DateTimeRfc3339NormalizableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Era269\Normalizable\Tests\Object;

use BadMethodCallException;
use DateTime;
use Era269\Normalizable\Object\DateTimeRfc3339Normalizable;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;
Expand Down Expand Up @@ -35,6 +37,36 @@ public function testDenormalize(DateTimeRfc3339Normalizable $dateTime): void
);
}

public function testCreateFromInterface(): void
{
$dateTime = new DateTime();
if (PHP_VERSION_ID >= 80000) {
$dateTimeFrom = DateTimeRfc3339Normalizable::createFromInterface($dateTime);
self::assertInstanceOf(DateTimeRfc3339Normalizable::class, $dateTimeFrom);
self::assertEquals(
$dateTime->format(DATE_RFC3339),
$dateTimeFrom->format(DATE_RFC3339)
);
} else {
$this->expectException(BadMethodCallException::class);
DateTimeRfc3339Normalizable::createFromInterface($dateTime);
}
}

public function testCreateFromFormat(): void
{
$dateTime = new DateTime();
$dateTimeFrom = DateTimeRfc3339Normalizable::createFromFormat(
DATE_RFC3339,
$dateTime->format(DATE_RFC3339)
);
self::assertInstanceOf(DateTimeRfc3339Normalizable::class, $dateTimeFrom);
self::assertEquals(
$dateTime->format(DATE_RFC3339),
$dateTimeFrom->format(DATE_RFC3339)
);
}

public function testDenormalizeFail(): void
{
$this->expectException(UnexpectedValueException::class);
Expand Down