CoreFiles by mySites.guru
FrozenClock.php51 lines, 1,322 bytes, from Joomla_6.0.0-beta1-Beta-Full_Package.zip
Raw History Compare with 5.4.0-beta3
md5
4aea130d56e9756c090d3a522fe8651e
sha1
5576df85cdb4ddfe81c3adcbac8b37445e9d55d3
sha256
0b7e91b424607759f21c2145ce88deb89724c44563f45d32ea602c838d1c5d4e
<?php
declare(strict_types=1);

namespace Lcobucci\Clock;

use DateMalformedStringException;
use DateTimeImmutable;
use DateTimeZone;
use InvalidArgumentException;

final class FrozenClock implements Clock
{
    public function __construct(private DateTimeImmutable $now)
    {
    }

    public static function fromUTC(): self
    {
        return new self(new DateTimeImmutable('now', new DateTimeZone('UTC')));
    }

    public function setTo(DateTimeImmutable $now): void
    {
        $this->now = $now;
    }

    /**
     * Adjusts the current time by a given modifier.
     *
     * @param string $modifier @see https://www.php.net/manual/en/datetime.formats.php
     *
     * @throws InvalidArgumentException When an invalid format string is passed (PHP < 8.3).
     * @throws DateMalformedStringException When an invalid date/time string is passed (PHP 8.3+).
     */
    public function adjustTime(string $modifier): void
    {
        $modifiedTime = @$this->now->modify($modifier);

        // PHP < 8.3 won't throw exceptions on invalid modifiers
        if ($modifiedTime === false) {
            throw new InvalidArgumentException('The given modifier is invalid');
        }

        $this->now = $modifiedTime;
    }

    public function now(): DateTimeImmutable
    {
        return $this->now;
    }
}

The same content is in 17 releases, Joomla 6.0.0-beta1 to 6.1.0-alpha1. See them all, or every version of this file.