Skip to content

Commit

Permalink
Add Repository::territory_for()
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Aug 9, 2024
1 parent c15d8d8 commit b034fc0
Show file tree
Hide file tree
Showing 13 changed files with 608 additions and 170 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
- `Units::LENGTH_*` constants are replaced by the `UnitLength` enum.
- `LocalizedListFormatter:TYPE_*` constants are replaced by the `ListType` enum.
- `DateTimeFormatter::WIDTH_*` constants are replaced by the `DateTimeFormatLength` enum.
- `$clrd->locales['fr']` is replaced by `$clrd->locale_for('fr')`.
- `$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::$territories`, use `TerritoryCode::CODES` instead.
- Removed `Supplemental::$currency_data`, use `Currency` instead.
- Removed `CurrencyData`, use `Currency` instead.
- Removed `Locale::localize()` and repurposed the `Localizable` interface.
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ generate: \
lib/Currency.php \
lib/LocaleId.php \
lib/Locale/HasContextTransforms.php \
lib/TerritoryCode.php \
lib/Units/SequenceCompanion.php \
lib/Units/UnitsCompanion.php

Expand All @@ -75,5 +76,8 @@ lib/Locale/HasContextTransforms.php: generator/src/Command/GenerateHasContextTra
lib/Units/SequenceCompanion.php: generator/src/Command/GenerateSequenceCompanion.php
$(GENERATE) $@

lib/TerritoryCode.php: generator/src/Command/GenerateTerritoryCode.php
$(GENERATE) $@

lib/Units/UnitsCompanion.php: generator/src/Command/GenerateUnitsCompanion.php
$(GENERATE) $@
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ echo $fr_euro->name_for(10); // euros
echo $fr_euro->format(12345.67); // 12 345,67 €

# You can access territories and their localized data
$territory = $repository->territories['FR'];
$territory = $repository->territory_for('FR');
echo $territory; // FR
echo $territory->currency; // EUR
echo $territory->currency_at('1977-06-06'); // FRF
echo $territory->currency_at('now'); // EUR
echo $territory->name_as('fr'); // France
echo $territory->name_as('it'); // Francia
echo $territory->name_as('ja'); // フランス
echo $repository->territories['FR']->first_day; // mon
echo $repository->territories['EG']->first_day; // sat
echo $repository->territories['BS']->first_day; // sun
echo $repository->territories['AE']->weekend_start; // fri
echo $repository->territories['AE']->weekend_end; // sat
echo $repository->territory_for('FR')->first_day; // mon
echo $repository->territory_for('EG')->first_day; // sat
echo $repository->territory_for('BS')->first_day; // sun
echo $repository->territory_for('AE')->weekend_start; // fri
echo $repository->territory_for('AE')->weekend_end; // sat
echo $territory->localized('fr')->name; // France
echo $territory->localized('it')->name; // Francia
echo $territory->localized('ja')->name; // フランス
Expand Down
2 changes: 1 addition & 1 deletion docs/Core.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Instances that can be localized usually implement the `localize()` method.

/* @var ICanBoogie\CLDR\Repository $repository */

echo $repository->territories['FR']->localized('fr')->name; // France
echo $repository->territory_for('FR')->localized('fr')->name; // France
```


Expand Down
39 changes: 19 additions & 20 deletions docs/Supplemental.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,30 @@ information that is actually scattered across the CLDR.
* @var ICanBoogie\CLDR\Repository $repository
*/

$territory = $repository->territories['FR'];
$territory = $repository->territory_for('FR');

echo $territory; // FR
echo $territory->currency; // EUR
echo $territory->currency_at('1977-06-06'); // FRF
echo $territory->currency_at('now'); // EUR
echo $territory; // FR
echo $territory->currency; // EUR
echo $territory->currency_at('1977-06-06'); // FRF
echo $territory->currency_at('now'); // EUR

echo $territory->language; // fr
echo $territory->population; // 66259000
echo $territory->language; // fr
echo $territory->population; // 66259000

echo $territory->name_as('fr-FR'); // France
echo $territory->name_as('it'); // Francia
echo $territory->name_as('ja'); // フランス
echo $territory->name_as('fr-FR'); // France
echo $territory->name_as('it'); // Francia
echo $territory->name_as('ja'); // フランス

echo $territory->name_as_fr_FR; // France
echo $territory->name_as_it; // Francia
echo $territory->name_as_ja; // フランス
echo $territory->name_as_fr_FR; // France
echo $territory->name_as_it; // Francia
echo $territory->name_as_ja; // フランス

echo $repository->territories['FR']->first_day; // mon
echo $repository->territories['EG']->first_day; // sat
echo $repository->territories['BS']->first_day; // sun
echo $repository->territory_for('FR')->first_day; // mon
echo $repository->territory_for('EG')->first_day; // sat
echo $repository->territory_for('BS')->first_day; // sun

echo $repository->territories['AE']->weekend_start; // fri
echo $repository->territories['AE']->weekend_end; // sat
echo $repository->territory_for('AE')->weekend_start; // fri
echo $repository->territory_for('AE')->weekend_end; // sat
```


Expand All @@ -77,8 +77,7 @@ use ICanBoogie\CLDR\LocaleId;
* @var ICanBoogie\CLDR\Repository $repository
*/

$territory = $repository->territories['FR'];

$territory = $repository->territory_for('FR');
$localized_territory = $territory->localized('fr');

echo $territory->localized('fr')->name; // France
Expand Down
144 changes: 144 additions & 0 deletions generator/src/Command/GenerateTerritoryCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace ICanBoogie\CLDR\Generator\Command;

use ICanBoogie\CLDR\Repository;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\VarExporter\VarExporter;
use function ICanBoogie\CLDR\Generator\indent;

#[AsCommand('lib/TerritoryCode.php')]
final class GenerateTerritoryCode extends Command
{
private const GENERATED_FILE = 'lib/TerritoryCode.php';

public function __construct(
private readonly Repository $repository
) {
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
/**
* @var string[] $codes
*
* @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-localenames-full/main/en-001/territories.json
* @phpstan-ignore-next-line
*/
$codes = array_keys($this->repository->locale_for('en-001')['territories']);
$codes = array_values(array_filter($codes, fn($code) => !str_contains($code, '-alt')));

$contents = $this->render(
codes: indent(VarExporter::export($codes), 1),
);

file_put_contents(self::GENERATED_FILE, $contents);

return self::SUCCESS;
}

private function render(
string $codes,
): string {
$class = __CLASS__;

return <<<PHP
<?php
/**
* CODE GENERATED; DO NOT EDIT.
*
* {@see \\$class}
*/
namespace ICanBoogie\CLDR;
/**
* A territory code.
*/
final class TerritoryCode
{
/**
* @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-localenames-full/main/en-001/territories.json
*/
public const CODES =
$codes;
/**
* Whether a currency code is defined.
*
* @param string \$code
* A currency code; for example, EUR.
*/
public static function is_defined(string \$code): bool
{
return in_array(\$code, self::CODES);
}
/**
* @param string \$code
* A currency code; for example, EUR.
*
* @throws TerritoryNotDefined
*/
public static function assert_is_defined(string \$code): void
{
self::is_defined(\$code)
or throw new TerritoryNotDefined(\$code);
}
/**
* Returns a {@see CurrencyCode} of the specified code.
*
* @param string \$code
* A currency code; for example, EUR.
*
* @throws TerritoryNotDefined
*/
public static function of(string \$code): self
{
static \$instances;
self::assert_is_defined(\$code);
return \$instances[\$code] ??= new self(\$code);
}
/**
* @param string \$value
* A territory value; for example, CA.
*/
private function __construct(
public readonly string \$value,
) {
}
/**
* Returns the {@see \$value} of the currency.
*/
public function __toString() : string
{
return \$this->value;
}
public function __serialize(): array
{
return [ 'value' => \$this->value ];
}
/**
* @param array{ value: string } \$data
*/
public function __unserialize(array \$data): void
{
\$this->value = \$data['value'];
}
}
PHP;
}
}
2 changes: 2 additions & 0 deletions generator/src/ContainerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ICanBoogie\CLDR\Generator\Command\GenerateHasContextTransforms;
use ICanBoogie\CLDR\Generator\Command\GenerateLocaleId;
use ICanBoogie\CLDR\Generator\Command\GenerateSequenceCompanion;
use ICanBoogie\CLDR\Generator\Command\GenerateTerritoryCode;
use ICanBoogie\CLDR\Generator\Command\GenerateUnitsCompanion;
use ICanBoogie\CLDR\Provider;
use ICanBoogie\CLDR\Repository;
Expand All @@ -23,6 +24,7 @@ final class ContainerProvider
GenerateHasContextTransforms::class,
GenerateCurrency::class,
GenerateSequenceCompanion::class,
GenerateTerritoryCode::class,
GenerateUnitsCompanion::class,
];

Expand Down
40 changes: 19 additions & 21 deletions lib/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,8 @@
/**
* Representation of the CLDR.
*
* <pre>
* <?php
*
* namespace ICanBoogie\CLDR;
*
* $repository = new Repository($provider);
*
* var_dump($repository->locale_fr('fr'));
* var_dump($repository->territories['FR']);
* </pre>
*
* @property-read Supplemental $supplemental
* @uses self::get_supplemental()
* @property-read TerritoryCollection $territories
* @uses self::get_territories()
* @property-read NumberFormatter $number_formatter
* @uses self::get_number_formatter()
* @property-read CurrencyFormatter $currency_formatter
Expand All @@ -44,7 +31,6 @@ final class Repository
{
/**
* @uses get_supplemental
* @uses get_territories
* @uses get_number_formatter
* @uses get_currency_formatter
* @uses get_list_formatter
Expand All @@ -66,13 +52,6 @@ private function get_supplemental(): Supplemental
return $this->supplemental ??= new Supplemental($this);
}

private TerritoryCollection $territories;

private function get_territories(): TerritoryCollection
{
return $this->territories ??= new TerritoryCollection($this);
}

private NumberFormatter $number_formatter;

private function get_number_formatter(): NumberFormatter
Expand Down Expand Up @@ -192,10 +171,29 @@ public function format_list(array $list, ListPattern $list_pattern): string

private LocaleCollection $locales;

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

return $this->locales->locale_for($locale_id);
}

/**
* @param string|TerritoryCode $code
* A territory code; for example, CA.
*/
public function territory_for(string|TerritoryCode $code): Territory
{
if ($code instanceof TerritoryCode) {
$code = $code->value;
} else {
TerritoryCode::assert_is_defined($code);
}

return new Territory($this, $code);
}
}
1 change: 0 additions & 1 deletion lib/Territory.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ public function __construct(
public readonly Repository $repository,
public readonly string $code
) {
$repository->territories->assert_defined($code);
}

public function __toString(): string
Expand Down
Loading

0 comments on commit b034fc0

Please sign in to comment.