Skip to content

Commit

Permalink
Generate data into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Aug 11, 2024
1 parent ef39818 commit 10c7da9
Show file tree
Hide file tree
Showing 16 changed files with 2,202 additions and 2,071 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,23 @@ GENERATE=./generator/generate

.PHONY=generate
generate: \
src/Core/LocaleId.php \
src/Core/LocaleData.php \
src/General/Transforms/HasContextTransforms.php \
src/Numbers/Currency.php \
src/Supplemental/Territory/TerritoryCode.php \
src/Numbers/CurrencyData.php \
src/Supplemental/Territory/TerritoryData.php \
src/Units/SequenceCompanion.php \
src/Units/UnitsCompanion.php

src/Core/LocaleId.php: generator/src/Command/GenerateLocaleId.php
src/Core/LocaleData.php: generator/src/Command/GenerateLocaleData.php
$(GENERATE) $@

src/General/Transforms/HasContextTransforms.php: generator/src/Command/GenerateHasContextTransforms.php
$(GENERATE) $@

src/Numbers/Currency.php: generator/src/Command/GenerateCurrency.php
src/Numbers/CurrencyData.php: generator/src/Command/GenerateCurrencyData.php
$(GENERATE) $@

src/Supplemental/Territory/TerritoryCode.php: generator/src/Command/GenerateTerritoryCode.php
src/Supplemental/Territory/TerritoryData.php: generator/src/Command/GenerateTerritoryData.php
$(GENERATE) $@

src/Units/SequenceCompanion.php: generator/src/Command/GenerateSequenceCompanion.php
Expand Down
201 changes: 0 additions & 201 deletions generator/src/Command/GenerateCurrency.php

This file was deleted.

100 changes: 100 additions & 0 deletions generator/src/Command/GenerateCurrencyData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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(self::GENERATED_FILE)]
final class GenerateCurrencyData extends Command
{
private const GENERATED_FILE = 'src/Numbers/CurrencyData.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-numbers-full/main/en-001/currencies.json
*/
$codes = array_keys($this->repository->locale_for('en-001')['currencies']);

/**
* @var array<string, array{
* _rounding: string,
* _digits: string,
* _cashRounding?: string,
* _cashDigits?: string
* }> $fractions
*
* @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-core/supplemental/currencyData.json
*/
$fractions = $this->repository->supplemental['currencyData']['fractions'];

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

file_put_contents(self::GENERATED_FILE, $contents);

return self::SUCCESS;
}

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

return <<<PHP
<?php
/**
* CODE GENERATED; DO NOT EDIT.
*
* {@see \\$class}
*/
namespace ICanBoogie\CLDR\Numbers;
/**
* @internal
* @codeCoverageIgnore
*/
final class CurrencyData
{
/**
* @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-numbers-modern/main/en-001/currencies.json
*/
public const CODES =
$codes;
/**
* @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-core/supplemental/currencyData.json
*/
public const FRACTIONS =
$fractions;
public const FRACTIONS_FALLBACK = 'DEFAULT';
private function __construct()
{
}
}
PHP;
}
}
Loading

0 comments on commit 10c7da9

Please sign in to comment.