-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimezone.php
More file actions
74 lines (62 loc) · 1.9 KB
/
Timezone.php
File metadata and controls
74 lines (62 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace TinyBlocks\Country;
use DateTimeZone;
use TinyBlocks\Country\Internal\Exceptions\InvalidTimezone;
use TinyBlocks\Vo\ValueObject;
use TinyBlocks\Vo\ValueObjectBehavior;
/**
* Value Object representing a single IANA timezone identifier (e.g. America/Sao_Paulo).
*/
final readonly class Timezone implements ValueObject
{
use ValueObjectBehavior;
public string $value;
private function __construct(string $identifier)
{
if (empty($identifier) || !in_array($identifier, self::allIdentifiers(), true)) {
throw new InvalidTimezone(identifier: $identifier);
}
$this->value = $identifier;
}
/**
* Creates a Timezone representing UTC.
*
* @return Timezone The UTC Timezone instance.
*/
public static function utc(): Timezone
{
return new Timezone(identifier: 'UTC');
}
/**
* Creates a Timezone from a valid IANA identifier.
*
* @param string $identifier The IANA timezone identifier (e.g. America/Sao_Paulo).
* @return Timezone The created Timezone instance.
* @throws InvalidTimezone If the identifier is not a valid IANA timezone.
*/
public static function from(string $identifier): Timezone
{
return new Timezone(identifier: $identifier);
}
/**
* Returns the IANA timezone identifier as a string.
*
* @return string The IANA timezone identifier.
*/
public function toString(): string
{
return $this->value;
}
/**
* Returns all valid IANA timezone identifiers available in the runtime.
*
* @return list<string> The list of all IANA timezone identifiers.
*/
protected static function allIdentifiers(): array
{
/** @var list<string>|null $identifiers */
static $identifiers = null;
return $identifiers ??= DateTimeZone::listIdentifiers();
}
}