diff --git a/CHANGELOG.md b/CHANGELOG.md index c210b79..9d2c905 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - `PsrToOrisaiClockAdapter` - makes any PSR clock compatible with `Orisai\Clock\Clock` - `SymfonyToOrisaiClockAdapter` - makes any symfony/clock compatible with `Orisai\Clock\Clock` - `ClockAdapterFactory` - chooses best supported adapter for `Orisai\Clock\Clock` compatibility +- `OrisaiToSymfonyClockAdapter` - makes any symfony/clock compatible with `Symfony\Component\Clock\ClockInterface` - `FrozenClock` - accepts `DateTimeInterface` as the initial time ### Changed diff --git a/docs/README.md b/docs/README.md index 18fb05a..b3a506c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -17,6 +17,7 @@ Provides current time for runtime and controllable time for testing - [Clock adapters](#clock-adapters) - [PSR to Orisai clock adapter](#psr-to-orisai-clock-adapter) - [Symfony to Orisai clock adapter](#symfony-to-orisai-clock-adapter) + - [Orisai to Symfony clock adapter](#orisai-to-symfony-clock-adapter) - [Integrations and extensions](#integrations-and-extensions) ## Setup @@ -206,6 +207,17 @@ use Symfony\Component\Clock\NativeClock; $clock = new SymfonyToOrisaiClockAdapter(new NativeClock()); ``` +### Orisai to Symfony clock adapter + +Decorate any `Orisai\Clock\Clock` implementation to conform interface `Symfony\Component\Clock\ClockInterface`. + +```php +use Orisai\Clock\Adapter\OrisaiToSymfonyClockAdapter; +use Orisai\Clock\SystemClock; + +$clock = new OrisaiToSymfonyClockAdapter(new SystemClock()); +``` + ## Integrations and extensions - [Nette](https://github.com/nette) integration - [orisai/nette-clock](https://github.com/orisai/nette-clock) diff --git a/src/Adapter/OrisaiToSymfonyClockAdapter.php b/src/Adapter/OrisaiToSymfonyClockAdapter.php new file mode 100644 index 0000000..9b93156 --- /dev/null +++ b/src/Adapter/OrisaiToSymfonyClockAdapter.php @@ -0,0 +1,48 @@ +clock = $clock; + } + + public function now(): DateTimeImmutable + { + $dt = $this->clock->now(); + + if ($this->timeZone !== null) { + $dt = $dt->setTimezone($this->timeZone); + } + + return $dt; + } + + public function sleep(float|int $seconds): void + { + $this->clock->sleep(0, 0, (int) round($seconds * 1E6)); + } + + public function withTimeZone(DateTimeZone|string $timezone): static + { + $clone = clone $this; + $clone->timeZone = is_string($timezone) ? new DateTimeZone($timezone) : $timezone; + + return $clone; + } + +} diff --git a/tests/Unit/Adapter/OrisaiToSymfonyClockAdapterTest.php b/tests/Unit/Adapter/OrisaiToSymfonyClockAdapterTest.php new file mode 100644 index 0000000..a6bad32 --- /dev/null +++ b/tests/Unit/Adapter/OrisaiToSymfonyClockAdapterTest.php @@ -0,0 +1,47 @@ +now()->format('U.u'), + ); + + $clock->sleep(1.234_567_8); + self::assertSame( + '1.234568', + $clock->now()->format('U.u'), + ); + + self::assertEquals( + $clock->now()->getTimezone(), + new DateTimeZone('UTC'), + ); + + $clock = $clock->withTimeZone(new DateTimeZone('Europe/Prague')); + self::assertEquals( + $clock->now()->getTimezone(), + new DateTimeZone('Europe/Prague'), + ); + } + +} diff --git a/tools/phpstan.neon b/tools/phpstan.neon index 8437767..1a94b50 100644 --- a/tools/phpstan.neon +++ b/tools/phpstan.neon @@ -3,7 +3,7 @@ includes: - phpstan.baseline.neon parameters: - phpVersion: 70400 + phpVersion: 80100 level: 8 tmpDir: ../var/tools/PHPStan resultCachePath: %currentWorkingDirectory%/var/tools/PHPStan/resultCache.php