Passed
Push — 6.0 ( 4ac4e1...87e1d7 )
by Olivier
01:56
created

GenerateCurrencyData::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ICanBoogie\CLDR\Generator\Command;
4
5
use ICanBoogie\CLDR\Repository;
6
use Symfony\Component\Console\Attribute\AsCommand;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\VarExporter\VarExporter;
11
12
use function ICanBoogie\CLDR\Generator\indent;
13
14
#[AsCommand(self::GENERATED_FILE)]
15
final class GenerateCurrencyData extends Command
16
{
17
    private const GENERATED_FILE = 'src/Numbers/CurrencyData.php';
18
19
    public function __construct(
20
        private readonly Repository $repository
21
    ) {
22
        parent::__construct();
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output): int
26
    {
27
        /**
28
         * @var string[] $codes
29
         *
30
         * @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-numbers-full/main/en-001/currencies.json
31
         */
32
        $codes = array_keys($this->repository->locale_for('en-001')['currencies']);
33
34
        /**
35
         * @var array<string, array{
36
         *     _rounding: string,
37
         *     _digits: string,
38
         *     _cashRounding?: string,
39
         *     _cashDigits?: string
40
         * }> $fractions
41
         *
42
         * @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-core/supplemental/currencyData.json
43
         */
44
        $fractions = $this->repository->supplemental['currencyData']['fractions'];
45
46
        $contents = $this->render(
47
            codes: indent(VarExporter::export($codes), 2),
48
            fractions: indent(VarExporter::export($fractions), 2),
49
        );
50
51
        file_put_contents(self::GENERATED_FILE, $contents);
52
53
        return self::SUCCESS;
54
    }
55
56
    private function render(
57
        string $codes,
58
        string $fractions,
59
    ): string {
60
        $class = __CLASS__;
61
62
        return <<<PHP
63
        <?php
64
65
        /**
66
         * CODE GENERATED; DO NOT EDIT.
67
         *
68
         * {@see \\$class}
69
         */
70
71
        namespace ICanBoogie\CLDR\Numbers;
72
73
        /**
74
         * @internal
75
         * @codeCoverageIgnore
76
         */
77
        final class CurrencyData
78
        {
79
            /**
80
             * @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-numbers-modern/main/en-001/currencies.json
81
             */
82
            public const CODES =
83
        $codes;
84
85
            /**
86
             * @link https://github.com/unicode-org/cldr-json/blob/45.0.0/cldr-json/cldr-core/supplemental/currencyData.json
87
             */
88
            public const FRACTIONS =
89
        $fractions;
90
91
            public const FRACTIONS_FALLBACK = 'DEFAULT';
92
93
            private function __construct()
94
            {
95
            }
96
        }
97
98
        PHP;
99
    }
100
}
101