Skip to content

Commit

Permalink
Remove LocaleCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Aug 9, 2024
1 parent b034fc0 commit 717b4fa
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 122 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- `LocaleId` is generated from the CLDR. It represents an available locale ID.
- `LocaleNotAvailable` is thrown when a requested locale ID is not available.
- `TerritoryCode` is generated from the CLDR. It represents a territory ID.
- `Currency` is generated from the CLDR. It represents a currency, including fraction information.
- Added the `Warmable` interface to features that can warm the CLDR cache.

Expand All @@ -23,8 +24,9 @@
- `$cldr->locales['fr']` is replaced by `$cldr->locale_for('fr')`.
- `$cldr->territories['FR']` is replaced by `$cldr->territory_for('FR')`.
- `$cldr->currencies['USD']` is replaced by `Currency::of('USD')`.
- Removed `Repository::$currencies`, use `Currency::CODES` instead.
- Removed `Repository::$locales`, use `LocaleId` instead.
- Removed `Repository::$territories`, use `TerritoryCode::CODES` instead.
- Removed `Repository::$currencies`, use `Currency::CODES` instead.
- Removed `Supplemental::$currency_data`, use `Currency` instead.
- Removed `CurrencyData`, use `Currency` instead.
- Removed `Locale::localize()` and repurposed the `Localizable` interface.
Expand Down
35 changes: 0 additions & 35 deletions lib/LocaleCollection.php

This file was deleted.

2 changes: 1 addition & 1 deletion lib/LocalizedTerritory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ class LocalizedTerritory extends LocalizedObject
protected function get_name(): string
{
/** @phpstan-ignore-next-line */
return $this->locale['territories'][$this->target->code];
return $this->locale['territories'][$this->target->code->value];
}
}
22 changes: 11 additions & 11 deletions lib/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function format_currency(
}

/**
* Formats a variable-length lists of scalars.
* Formats variable-length lists of scalars.
*
* @param scalar[] $list
*
Expand All @@ -169,17 +169,18 @@ public function format_list(array $list, ListPattern $list_pattern): string
return $this->list_formatter->format($list, $list_pattern);
}

private LocaleCollection $locales;

/**
* @param string|LocaleId $locale_id
* @param string|LocaleId $id
* A locale ID; for example, fr-BE.
*/
public function locale_for(string|LocaleId $locale_id): Locale
public function locale_for(string|LocaleId $id): Locale
{
$this->locales ??= new LocaleCollection($this);
if (!$id instanceof LocaleId)
{
$id = LocaleId::of($id);
}

return $this->locales->locale_for($locale_id);
return new Locale($this, $id);
}

/**
Expand All @@ -188,10 +189,9 @@ public function locale_for(string|LocaleId $locale_id): Locale
*/
public function territory_for(string|TerritoryCode $code): Territory
{
if ($code instanceof TerritoryCode) {
$code = $code->value;
} else {
TerritoryCode::assert_is_defined($code);
if (!$code instanceof TerritoryCode)
{
$code = TerritoryCode::of($code);
}

return new Territory($this, $code);
Expand Down
10 changes: 5 additions & 5 deletions lib/Territory.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private function get_currencies(): RegionCurrencies
{
return $this->currencies ??= RegionCurrencies::from(
/** @phpstan-ignore-next-line */
$this->repository->supplemental['currencyData']['region'][$this->code]
$this->repository->supplemental['currencyData']['region'][$this->code->value]
);
}

Expand Down Expand Up @@ -141,13 +141,13 @@ private function get_population(): int

public function __construct(
public readonly Repository $repository,
public readonly string $code
public readonly TerritoryCode $code,
) {
}

public function __toString(): string
{
return $this->code;
return $this->code->value;
}

/**
Expand All @@ -171,7 +171,7 @@ public function __get(string $property)
private function retrieve_from_supplemental(string $section): array
{
/** @phpstan-ignore-next-line */
return $this->repository->supplemental[$section][$this->code];
return $this->repository->supplemental[$section][$this->code->value];
}

/**
Expand Down Expand Up @@ -230,7 +230,7 @@ private function resolve_week_data(string $which): string
/** @phpstan-ignore-next-line */
$data = $this->repository->supplemental['weekData'][$which];

return $data[$code] ?? $data['001'];
return $data[$code->value] ?? $data['001'];
}

private function ensure_is_datetime(DateTimeInterface|string|null $datetime): DateTimeInterface
Expand Down
53 changes: 0 additions & 53 deletions tests/LocaleCollectionTest.php

This file was deleted.

3 changes: 2 additions & 1 deletion tests/LocalizedTerritoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ICanBoogie\CLDR\LocalizedTerritory;
use ICanBoogie\CLDR\Territory;
use ICanBoogie\CLDR\TerritoryCode;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

Expand All @@ -12,7 +13,7 @@ final class LocalizedTerritoryTest extends TestCase
#[DataProvider('provide_test_get_name')]
public function test_get_name(string $locale_id, string $territory_code, string $expected): void
{
$territory = new Territory(get_repository(), $territory_code);
$territory = new Territory(get_repository(), TerritoryCode::of($territory_code));
$localized = new LocalizedTerritory($territory, locale_for($locale_id));

$this->assertEquals($expected, $localized->name);
Expand Down
29 changes: 26 additions & 3 deletions tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use ICanBoogie\CLDR\CurrencyFormatter;
use ICanBoogie\CLDR\ListFormatter;
use ICanBoogie\CLDR\Locale\ListPattern;
use ICanBoogie\CLDR\LocaleId;
use ICanBoogie\CLDR\LocaleNotAvailable;
use ICanBoogie\CLDR\NumberFormatter;
use ICanBoogie\CLDR\Plurals;
use ICanBoogie\CLDR\Provider;
Expand Down Expand Up @@ -84,24 +86,45 @@ public function test_format_list(): void
$this->assertSame("one, two, and three", $this->sut->format_list($list, $list_pattern));
}

public function test_locale_for_using_string(): void
{
$actual = $this->sut->locale_for('fr-BE');

$this->assertEquals('fr-BE', $actual->id->value);
}

public function test_locale_for_using_id(): void
{
$actual = $this->sut->locale_for(LocaleId::of('fr-BE'));

$this->assertEquals('fr-BE', $actual->id->value);
}

public function test_locale_for_fails_on_unavailable_id(): void
{
$this->expectException(LocaleNotAvailable::class);

$this->sut->locale_for('foo');
}

public function test_territory_for_using_string(): void
{
$actual = $this->sut->territory_for('CA');

$this->assertEquals('CA', $actual->code);
$this->assertEquals('CA', $actual->code->value);
}

public function test_territory_for_using_code(): void
{
$actual = $this->sut->territory_for(TerritoryCode::of('CA'));

$this->assertEquals('CA', $actual->code);
$this->assertEquals('CA', $actual->code->value);
}

public function test_territory_for_fails_on_undefined_code(): void
{
$this->expectException(TerritoryNotDefined::class);

$this->sut->territory_for('ZZZ');
$this->sut->territory_for('foo');
}
}
25 changes: 13 additions & 12 deletions tests/TerritoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
use ICanBoogie\CLDR\LocaleId;
use ICanBoogie\CLDR\LocalizedTerritory;
use ICanBoogie\CLDR\Territory;
use ICanBoogie\CLDR\TerritoryCode;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class TerritoryTest extends TestCase
{
public function test_get_info(): void
{
$territory = new Territory(get_repository(), 'FR');
$territory = new Territory(get_repository(), TerritoryCode::of('FR'));
$this->assertIsArray($territory->info);
}

public function test_get_containment(): void
{
$territory = new Territory(get_repository(), 'EU');
$territory = new Territory(get_repository(), TerritoryCode::of('EU'));
$this->assertIsArray($territory->containment);
}

public function test_is_containing(): void
{
$territory = new Territory(get_repository(), 'EU');
$territory = new Territory(get_repository(), TerritoryCode::of('EU'));

$this->assertTrue($territory->is_containing('FR'));
$this->assertFalse($territory->is_containing('TA'));
Expand All @@ -33,7 +34,7 @@ public function test_is_containing(): void
#[DataProvider('provide_test_get_currency')]
public function test_get_currency(string $expected, string $territory_code): void
{
$territory = new Territory(get_repository(), $territory_code);
$territory = new Territory(get_repository(), TerritoryCode::of($territory_code));
$actual = $territory->currency;

$this->assertEquals($expected, $actual?->code);
Expand All @@ -56,7 +57,7 @@ public static function provide_test_get_currency(): array
#[DataProvider('provide_test_currency_at')]
public function test_currency_at(string $expected, string $territory_code, mixed $date): void
{
$territory = new Territory(get_repository(), $territory_code);
$territory = new Territory(get_repository(), TerritoryCode::of($territory_code));
$this->assertEquals($expected, $territory->currency_at($date)?->code);
}

Expand All @@ -81,7 +82,7 @@ public static function provide_test_currency_at(): array
#[DataProvider('provide_test_get_language')]
public function test_get_language(string $expected, string $territory_code): void
{
$territory = new Territory(get_repository(), $territory_code);
$territory = new Territory(get_repository(), TerritoryCode::of($territory_code));
$this->assertSame($expected, $territory->language);
}

Expand All @@ -101,14 +102,14 @@ public static function provide_test_get_language(): array

public function test_get_population(): void
{
$territory = new Territory(get_repository(), 'ES');
$territory = new Territory(get_repository(), TerritoryCode::of('ES'));
$this->assertNotEmpty($territory->population);
}

#[DataProvider('provide_test_name_as')]
public function test_name_as(string $expected, string $territory_code, string $locale_id): void
{
$territory = new Territory(get_repository(), $territory_code);
$territory = new Territory(get_repository(), TerritoryCode::of($territory_code));
$this->assertEquals($expected, $territory->name_as(LocaleId::of($locale_id)));
$this->assertEquals($expected, $territory->name_as($locale_id));
}
Expand All @@ -131,7 +132,7 @@ public static function provide_test_name_as(): array
#[DataProvider('provide_test_get_name_as')]
public function test_get_name_as(string $expected, string $territory_code, string $locale_id): void
{
$territory = new Territory(get_repository(), $territory_code);
$territory = new Territory(get_repository(), TerritoryCode::of($territory_code));
$this->assertEquals($expected, $territory->{'name_as_' . $locale_id});
}

Expand All @@ -153,7 +154,7 @@ public static function provide_test_get_name_as(): array
#[DataProvider('provide_test_get_property')]
public function test_get_property(string $expected, string $territory_code, string $property): void
{
$territory = new Territory(get_repository(), $territory_code);
$territory = new Territory(get_repository(), TerritoryCode::of($territory_code));
$this->assertEquals($expected, $territory->$property);
}

Expand Down Expand Up @@ -192,13 +193,13 @@ public static function provide_test_get_property(): array
public function test_to_string(): void
{
$territory_code = 'US';
$territory = new Territory(get_repository(), $territory_code);
$territory = new Territory(get_repository(), TerritoryCode::of($territory_code));
$this->assertEquals($territory_code, (string)$territory);
}

public function test_localize(): void
{
$territory = new Territory(get_repository(), 'FR');
$territory = new Territory(get_repository(), TerritoryCode::of('FR'));
$actual = $territory->localized(LocaleId::of('fr'));

$this->assertInstanceOf(LocalizedTerritory::class, $actual);
Expand Down

0 comments on commit 717b4fa

Please sign in to comment.