GenerateHasContextTransforms::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 21
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace ICanBoogie\CLDR\Generator\Command;
4
5
use ICanBoogie\CLDR\Provider\ResourceNotFound;
6
use ICanBoogie\CLDR\Repository;
7
use Symfony\Component\Console\Attribute\AsCommand;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\VarExporter\VarExporter;
12
13
use function ICanBoogie\CLDR\Generator\indent;
14
15
#[AsCommand(self::GENERATED_FILE)]
16
final class GenerateHasContextTransforms extends Command
17
{
18
    private const GENERATED_FILE = 'src/General/Transforms/HasContextTransforms.php';
19
20
    public function __construct(
21
        private readonly Repository $repository
22
    ) {
23
        parent::__construct();
24
    }
25
26
    protected function execute(InputInterface $input, OutputInterface $output): int
27
    {
28
        $w = $output->writeln(...);
29
        $has_by_locale = [];
30
31
        foreach ($this->repository->available_locales as $locale_id) {
32
            $has = true;
33
34
            try {
35
                $w("Checking $locale_id");
36
                $this->repository->fetch("misc/$locale_id/contextTransforms");
37
            } catch (ResourceNotFound) {
38
                $has = false;
39
            }
40
41
            $has_by_locale[$locale_id] = $has;
42
        }
43
44
        $contents = $this->render(
45
            has_by_locale: indent(VarExporter::export($has_by_locale), 2),
46
        );
47
48
        file_put_contents(self::GENERATED_FILE, $contents);
49
50
        return self::SUCCESS;
51
    }
52
53
    private function render(string $has_by_locale): string
54
    {
55
        $class = __CLASS__;
56
57
        return <<<PHP
58
        <?php
59
60
        /**
61
         * CODE GENERATED; DO NOT EDIT.
62
         *
63
         * {@see \\$class}
64
         */
65
66
        namespace ICanBoogie\CLDR\General\Transforms;
67
68
        use ICanBoogie\CLDR\Core\LocaleId;
69
70
        final class HasContextTransforms
71
        {
72
            private const HAS_CONTEXT_TRANSFORMS =
73
        $has_by_locale;
74
75
            /**
76
             * Whether a locale has context transforms.
77
             */
78
            public static function for_locale(LocaleId \$locale_id): bool
79
            {
80
                return self::HAS_CONTEXT_TRANSFORMS[\$locale_id->value];
81
            }
82
83
            /**
84
             * @codeCoverageIgnore
85
             */
86
            private function __construct()
87
            {
88
            }
89
        }
90
91
        PHP;
92
    }
93
}
94