mirror of
https://github.com/beste/clock.git
synced 2026-08-17 17:47:13 +00:00
The stella-maris/clock package provides an interface based on the currently proposed status of PSR20. Due to the inactivity of the PSR20 working group this is a way to already provide interoperability while still maintaining forward compatibility. When the current status of PSR20 will be released at one point in time the stella-maris/clock package will extend the PSR20 interface so that this package becomes immeadiately PSR20 compatible without any further work necessary. In the long run the stella-maris/clock package will then be marked deprecated so that people can then use the PSR20 provided implementation. Should the implementation of PSR20 change between now and a possible release then this interface will still exist and a possible implementation will need more code-changes anyhow so this interface will still provide some way of interoperability. The implementation of the PSR20 polyfill has been moved to the stella-maris/clock interface which can be implemented independentyl from PSR20 and might allow some interoperability before the working group of the FIG manages to decide upon something.
48 lines
937 B
PHP
48 lines
937 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Beste\Clock;
|
|
|
|
use Beste\Clock;
|
|
use DateTimeImmutable;
|
|
use DateTimeZone;
|
|
use StellaMaris\Clock\ClockInterface;
|
|
|
|
final class FrozenClock implements Clock
|
|
{
|
|
private DateTimeImmutable $frozenAt;
|
|
|
|
private function __construct(DateTimeImmutable $frozenAt)
|
|
{
|
|
$this->frozenAt = $frozenAt;
|
|
}
|
|
|
|
public static function at(DateTimeImmutable $time): self
|
|
{
|
|
return new self($time);
|
|
}
|
|
|
|
public static function withNowFrom(ClockInterface $clock): self
|
|
{
|
|
return new self($clock->now());
|
|
}
|
|
|
|
public static function fromUTC(): self
|
|
{
|
|
return new self(new DateTimeImmutable('now', new DateTimeZone('UTC')));
|
|
}
|
|
|
|
public function setTo(DateTimeImmutable $time): self
|
|
{
|
|
$this->frozenAt = $time;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function now(): DateTimeImmutable
|
|
{
|
|
return clone $this->frozenAt;
|
|
}
|
|
}
|