Uploaded From CV. Swandhana Server
This commit is contained in:
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\Clock;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Psr\Clock\ClockInterface;
|
||||
|
||||
interface Clock extends ClockInterface
|
||||
{
|
||||
public function now(): DateTimeImmutable;
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lcobucci\Clock;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
|
||||
use function date_default_timezone_get;
|
||||
|
||||
/** @immutable */
|
||||
final class SystemClock implements Clock
|
||||
{
|
||||
public function __construct(private readonly DateTimeZone $timezone)
|
||||
{
|
||||
}
|
||||
|
||||
public static function fromUTC(): self
|
||||
{
|
||||
return new self(new DateTimeZone('UTC'));
|
||||
}
|
||||
|
||||
public static function fromSystemTimezone(): self
|
||||
{
|
||||
return new self(new DateTimeZone(date_default_timezone_get()));
|
||||
}
|
||||
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('now', $this->timezone);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user