48 lines
1,003 B
PHP
48 lines
1,003 B
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Shared\ValueObjects\Config;
|
||
|
|
|
||
|
|
use Shared\DDD\Model\Equatable;
|
||
|
|
|
||
|
|
final readonly class AppTimezone implements \Stringable, Equatable
|
||
|
|
{
|
||
|
|
public string $name;
|
||
|
|
|
||
|
|
public function __construct(string $name)
|
||
|
|
{
|
||
|
|
$trimmed = trim($name);
|
||
|
|
|
||
|
|
try {
|
||
|
|
$tz = new \DateTimeZone($trimmed);
|
||
|
|
$this->name = $tz->getName();
|
||
|
|
} catch (\Exception) {
|
||
|
|
throw new \InvalidArgumentException(sprintf('Invalid timezone identifier: "%s".', $name));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function utc(): self
|
||
|
|
{
|
||
|
|
return new self('UTC');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function toDateTimeZone(): \DateTimeZone
|
||
|
|
{
|
||
|
|
return new \DateTimeZone($this->name);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[\Override]
|
||
|
|
public function __toString(): string
|
||
|
|
{
|
||
|
|
return $this->name;
|
||
|
|
}
|
||
|
|
|
||
|
|
#[\Override]
|
||
|
|
public function equals(mixed $other): bool
|
||
|
|
{
|
||
|
|
return $other instanceof self
|
||
|
|
&& $this->name === $other->name;
|
||
|
|
}
|
||
|
|
}
|