66
77use Countable ;
88use TinyBlocks \Country \Internal \TimezoneCatalog ;
9+ use TinyBlocks \Time \Timezone ;
10+ use TinyBlocks \Time \Timezones ;
11+ use TinyBlocks \Vo \ValueObject ;
12+ use TinyBlocks \Vo \ValueObjectBehavior ;
913
1014/**
1115 * Immutable collection of Timezone objects for a country.
1216 *
1317 * Built from PHP's ICU/IANA timezone database — the authoritative source for timezone data.
1418 * The first element is considered the default/primary timezone for the country.
1519 */
16- final readonly class Timezones implements Countable
20+ final readonly class CountryTimezones implements ValueObject, Countable
1721{
18- /**
19- * @param list<Timezone> $items All timezone objects for the country.
20- * @param Timezone $default The default/primary timezone (first in the IANA list, or UTC as fallback).
21- */
22- private function __construct (private array $ items , private Timezone $ default )
22+ use ValueObjectBehavior;
23+
24+ private function __construct (private Timezones $ timezones , private Timezone $ default )
2325 {
2426 }
2527
2628 /**
27- * Creates a Timezones collection from an Alpha-2 country code.
29+ * Creates a CountryTimezones instance from an Alpha-2 country code.
2830 *
29- * @param Alpha2Code $alpha2 The two-letter country code.
30- * @return Timezones The timezones collection for the given country.
31+ * @param Alpha2Code $alpha2 The Alpha-2 country code (e.g. "US" for United States) .
32+ * @return CountryTimezones A new CountryTimezones instance containing the timezones for the specified country.
3133 */
32- public static function fromAlpha2 (Alpha2Code $ alpha2 ): Timezones
34+ public static function fromAlpha2 (Alpha2Code $ alpha2 ): CountryTimezones
3335 {
34- $ items = array_map (
35- static fn (string $ id ): Timezone => Timezone::from (identifier: $ id ),
36- TimezoneCatalog::forAlpha2 (alpha2Value: $ alpha2 ->value )
37- );
36+ $ identifiers = TimezoneCatalog::forAlpha2 (alpha2Value: $ alpha2 ->value );
37+ $ timezones = Timezones::fromStrings (...$ identifiers );
38+ $ default = $ timezones ->all ()[0 ] ?? Timezone::utc ();
3839
39- return new Timezones (items : $ items , default: $ items [ 0 ] ?? Timezone:: utc () );
40+ return new CountryTimezones (timezones : $ timezones , default: $ default );
4041 }
4142
4243 /**
@@ -46,7 +47,7 @@ public static function fromAlpha2(Alpha2Code $alpha2): Timezones
4647 */
4748 public function all (): array
4849 {
49- return $ this ->items ;
50+ return $ this ->timezones -> all () ;
5051 }
5152
5253 /**
@@ -56,7 +57,7 @@ public function all(): array
5657 */
5758 public function count (): int
5859 {
59- return count ( $ this ->items );
60+ return $ this ->timezones -> count ( );
6061 }
6162
6263 /**
@@ -73,43 +74,34 @@ public function default(): Timezone
7374 }
7475
7576 /**
76- * Checks whether the given IANA identifier belongs to this country's timezones .
77+ * Returns all timezone identifiers as plain strings .
7778 *
78- * @param string $iana The IANA timezone identifier to check (e.g. America/New_York).
79- * @return bool True if the identifier belongs to this country, false otherwise.
79+ * @return list<string> The list of IANA timezone identifier strings.
8080 */
81- public function contains ( string $ iana ): bool
81+ public function toStrings ( ): array
8282 {
83- return array_any (
84- $ this ->items ,
85- static fn (Timezone $ timezone ): bool => $ timezone ->value === $ iana
86- );
83+ return $ this ->timezones ->toStrings ();
8784 }
8885
8986 /**
90- * Finds a Timezone by its IANA identifier.
87+ * Checks whether the given IANA identifier belongs to this country's timezones .
9188 *
92- * @param string $iana The IANA timezone identifier to search for (e.g. America/Sao_Paulo ).
93- * @return Timezone The matching Timezone, or UTC if not found in this country.
89+ * @param string $iana The IANA timezone identifier to check (e.g. America/New_York ).
90+ * @return bool True if the identifier belongs to this country, false otherwise .
9491 */
95- public function findByIdentifier (string $ iana ): Timezone
92+ public function contains (string $ iana ): bool
9693 {
97- return array_find (
98- $ this ->items ,
99- static fn (Timezone $ timezone ): bool => $ timezone ->value === $ iana
100- ) ?? Timezone::utc ();
94+ return $ this ->timezones ->contains (iana: $ iana );
10195 }
10296
10397 /**
104- * Returns all timezone identifiers as plain strings .
98+ * Finds a Timezone object by its IANA identifier .
10599 *
106- * @return list<string> The list of IANA timezone identifier strings.
100+ * @param string $iana The IANA timezone identifier to find (e.g. America/New_York).
101+ * @return Timezone The corresponding Timezone object if found, or UTC if not found.
107102 */
108- public function toStrings ( ): array
103+ public function findByIdentifierOrUtc ( string $ iana ): Timezone
109104 {
110- return array_map (
111- static fn (Timezone $ timezone ): string => $ timezone ->toString (),
112- $ this ->items
113- );
105+ return $ this ->timezones ->findByIdentifierOrUtc (iana: $ iana );
114106 }
115107}
0 commit comments