termyn /
timekeeper
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Termyn\Timekeeper; |
||
| 6 | |||
| 7 | use DateTimeImmutable; |
||
| 8 | use InvalidArgumentException; |
||
| 9 | |||
| 10 | final class FixedTimeService implements TimeService |
||
| 11 | { |
||
| 12 | public function __construct( |
||
| 13 | private readonly DateTimeImmutable $dateTime, |
||
| 14 | ) { |
||
| 15 | } |
||
| 16 | |||
| 17 | public static function fromStamp(int $seconds): self |
||
| 18 | { |
||
| 19 | return self::fromString(sprintf('%s', $seconds), 'U'); |
||
| 20 | } |
||
| 21 | |||
| 22 | public static function fromString(string $dateTime, string $format): self |
||
| 23 | { |
||
| 24 | $dateTime = DateTimeImmutable::createFromFormat($format, $dateTime); |
||
| 25 | if (! $dateTime) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 26 | throw new InvalidArgumentException(sprintf('Invalid datetime "%s" (%s).', $dateTime, $format)); |
||
| 27 | } |
||
| 28 | |||
| 29 | return new self($dateTime); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function measure(): DateTimeImmutable |
||
| 33 | { |
||
| 34 | return $this->dateTime; |
||
| 35 | } |
||
| 36 | } |
||
| 37 |