GenerateTerritoryData   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 21
c 1
b 0
f 0
dl 0
loc 58
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 18 1
A render() 0 27 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
use function ICanBoogie\CLDR\Generator\indent;
12
13
#[AsCommand(self::GENERATED_FILE)]
14
final class GenerateTerritoryData extends Command
15
{
16
    private const GENERATED_FILE = 'src/Supplemental/Territory/TerritoryData.php';
17
18
    public function __construct(
19
        private readonly Repository $repository
20
    ) {
21
        parent::__construct();
22
    }
23
24
    protected function execute(InputInterface $input, OutputInterface $output): int
25
    {
26
        /**
27
         * @var string[] $codes
28
         *
29
         * @link https://github.com/unicode-org/cldr-json/blob/47.0.0/cldr-json/cldr-localenames-full/main/en-001/territories.json
30
         */
31
        $codes = array_keys($this->repository->locale_for('en-001')['territories']);
32
        $codes = array_values(array_filter($codes, fn($code) => !str_contains($code, '-alt')));
33
        $codes = array_map(fn ($v) => (string) $v, $codes);;
34
35
        $contents = $this->render(
36
            codes: indent(VarExporter::export($codes), 2),
37
        );
38
39
        file_put_contents(self::GENERATED_FILE, $contents);
40
41
        return self::SUCCESS;
42
    }
43
44
    private function render(
45
        string $codes,
46
    ): string {
47
        $class = __CLASS__;
48
49
        return <<<PHP
50
        <?php
51
52
        /**
53
         * CODE GENERATED; DO NOT EDIT.
54
         *
55
         * {@see \\$class}
56
         */
57
58
        namespace ICanBoogie\CLDR\Supplemental\Territory;
59
60
        /**
61
         * @internal
62
         * @codeCoverageIgnore
63
         */
64
        final class TerritoryData
65
        {
66
            /**
67
             * @link https://github.com/unicode-org/cldr-json/blob/47.0.0/cldr-json/cldr-localenames-full/main/en-001/territories.json
68
             */
69
            public const CODES =
70
        $codes;
71
72
            private function __construct()
73
            {
74
            }
75
        }
76
77
        PHP;
78
    }
79
}
80