From 2ea81190b5eaab9f75c4017445c6c0e1250ae4b8 Mon Sep 17 00:00:00 2001 From: Artem Henvald Date: Tue, 16 May 2023 11:04:16 +0300 Subject: [PATCH] Add PSR-20 (#13) Co-authored-by: Artem Henvald --- .github/workflows/ci.yaml | 1 - .styleci.yml | 2 +- README.md | 12 +++++++++++- composer.json | 13 +++++++------ phpunit.xml.dist | 7 +++++-- src/DateTimeHelper.php | 12 +++++++++++- tests/DateTimeHelperTest.php | 11 ++++++++--- 7 files changed, 43 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6f67fb2..6af32c8 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -94,7 +94,6 @@ jobs: fail-fast: false matrix: php-version: - - '8.1' - '8.2' steps: - name: 'Checkout Code' diff --git a/.styleci.yml b/.styleci.yml index 1c5ec6e..f72ce80 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -1,6 +1,6 @@ preset: recommended -version: 8.1 +version: 8.2 enabled: - combine_consecutive_unsets diff --git a/README.md b/README.md index 527f07a..99fc4ca 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ PHP library that provides additional functions for processing dates & times. ## Requirements -* PHP 8.1 +* PHP 8.2 ## Installation 🌱 @@ -48,6 +48,16 @@ $now5 = $dateTimeHelper->getCurrentDatetimeImmutable(new \DateTimeZone('Europe/K $now6 = $dateTimeHelper->getCurrentDatetimeImmutableUtc(); // Always in UTC ``` +Compatible with [PSR-20: Clock](https://www.php-fig.org/psr/psr-20/). + +```php +use Fresh\DateTime\DateTimeHelper; + +$dateTimeHelper = new DateTimeHelper(); + +$now = $dateTimeHelper->now(); // \DateTimeImmutable in UTC +``` + ### Method for getting current timestamp ```php diff --git a/composer.json b/composer.json index d2fdee0..794ab49 100644 --- a/composer.json +++ b/composer.json @@ -17,15 +17,16 @@ "issues": "https://github.com/fre5h/datetime-php/issues" }, "require": { - "php": ">=8.1" + "php": ">=8.2" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.3", - "phpstan/phpstan": "^1.3", - "phpstan/phpstan-phpunit": "^1.0", - "phpunit/phpunit": "^9.5", + "friendsofphp/php-cs-fixer": "^3.16", + "phpstan/phpstan": "^1.10", + "phpstan/phpstan-phpunit": "^1.3", + "phpunit/phpunit": "^10.1", + "psr/clock": "^1.0", "slam/phpstan-extensions": "^6.0", - "squizlabs/php_codesniffer": "^3.6", + "squizlabs/php_codesniffer": "^3.7", "thecodingmachine/phpstan-strict-rules": "^1.0" }, "autoload": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6b98b88..2366ba2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,7 @@ @@ -11,12 +11,15 @@ - + ./src/ ./src/TimeConstants.php + + + diff --git a/src/DateTimeHelper.php b/src/DateTimeHelper.php index ddb16e8..d3c55d1 100644 --- a/src/DateTimeHelper.php +++ b/src/DateTimeHelper.php @@ -12,12 +12,14 @@ namespace Fresh\DateTime; +use Psr\Clock\ClockInterface; + /** * DateTimeHelper. * * @author Artem Henvald */ -class DateTimeHelper implements DateTimeHelperInterface +class DateTimeHelper implements ClockInterface, DateTimeHelperInterface { private const INTERNAL_DATE_FORMAT = 'Y-m-d'; @@ -26,6 +28,14 @@ class DateTimeHelper implements DateTimeHelperInterface private ?\DateTimeZone $timeZoneUtc = null; + /** + * {@inheritdoc} + */ + public function now(): \DateTimeImmutable + { + return $this->getCurrentDatetimeImmutableUtc(); + } + /** * {@inheritdoc} */ diff --git a/tests/DateTimeHelperTest.php b/tests/DateTimeHelperTest.php index 1a0f539..8960024 100644 --- a/tests/DateTimeHelperTest.php +++ b/tests/DateTimeHelperTest.php @@ -25,9 +25,7 @@ */ class DateTimeHelperTest extends TestCase { - /** @var DateRangeInterface|MockObject */ - private $dateRange; - + private DateRangeInterface|MockObject $dateRange; private DateTimeHelper $dateTimeHelper; protected function setUp(): void @@ -105,6 +103,13 @@ public function testGetCurrentDatetimeImmutable(): void self::assertSame('Europe/Kiev', $now->getTimezone()->getName()); } + public function testNow(): void + { + $now = $this->dateTimeHelper->now(); + self::assertInstanceOf(\DateTimeImmutable::class, $now); + self::assertSame('UTC', $now->getTimezone()->getName()); + } + public function testGetCurrentDatetimeImmutableUtc(): void { $now = $this->dateTimeHelper->getCurrentDatetimeImmutableUtc();