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
|
|
|
|
11
|
|
|
#[AsCommand(self::GENERATED_FILE)] |
12
|
|
|
final class GenerateUnitsCompanion extends Command |
13
|
|
|
{ |
14
|
|
|
private const GENERATED_FILE = 'src/Supplemental/Units/UnitsCompanion.php'; |
15
|
|
|
|
16
|
|
|
public function __construct( |
17
|
|
|
private readonly Repository $repository |
18
|
|
|
) { |
19
|
|
|
parent::__construct(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
23
|
|
|
{ |
24
|
|
|
$units = $this->repository->locale_for('en-001')['units']['long']; |
25
|
|
|
$properties = []; |
26
|
|
|
$methods = []; |
27
|
|
|
|
28
|
|
|
foreach ($units as $name => $unit) { |
29
|
|
|
if (empty($unit['unitPattern-count-one'])) { |
30
|
|
|
continue; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$normalized = strtr($name, [ '-' => '_' ]); |
34
|
|
|
|
35
|
|
|
$properties[] = <<<TXT |
36
|
|
|
* @property-read Unit \$$normalized |
37
|
|
|
TXT; |
38
|
|
|
|
39
|
|
|
$methods[] = <<<PHP |
40
|
|
|
/** |
41
|
|
|
* @param float|int|numeric-string \$number |
42
|
|
|
*/ |
43
|
|
|
public function $normalized(float|int|string \$number): NumberWithUnit |
44
|
|
|
{ |
45
|
|
|
return new NumberWithUnit(\$number, "$name", \$this); |
46
|
|
|
} |
47
|
|
|
PHP; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$contents = $this->render( |
51
|
|
|
properties: implode("\n", $properties), |
52
|
|
|
methods: implode("\n\n", $methods), |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
file_put_contents(self::GENERATED_FILE, $contents); |
56
|
|
|
|
57
|
|
|
return self::SUCCESS; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function render( |
61
|
|
|
string $properties, |
62
|
|
|
string $methods, |
63
|
|
|
): string { |
64
|
|
|
$class = __CLASS__; |
65
|
|
|
|
66
|
|
|
return <<<PHP |
67
|
|
|
<?php |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* CODE GENERATED; DO NOT EDIT. |
71
|
|
|
* |
72
|
|
|
* {@see \\$class} |
73
|
|
|
*/ |
74
|
|
|
|
75
|
|
|
namespace ICanBoogie\CLDR\Supplemental\Units; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @internal |
79
|
|
|
* @codeCoverageIgnore |
80
|
|
|
* |
81
|
|
|
$properties |
82
|
|
|
*/ |
83
|
|
|
trait UnitsCompanion |
84
|
|
|
{ |
85
|
|
|
$methods |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
PHP; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|