forked from php-gettext/Translator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayGenerator.php
More file actions
196 lines (171 loc) · 6.01 KB
/
Copy pathArrayGenerator.php
File metadata and controls
196 lines (171 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types = 1);
namespace Gettext\Generator;
use Gettext\Headers;
use Gettext\Translation;
use Gettext\Translations;
final class ArrayGenerator extends Generator
{
/**
* @private
*/
const PRETTY_INDENT = ' ';
/**
* @private
*/
const FORMAT_NORMAL = 'normal';
/**
* @private
*/
const FORMAT_PRETTY = 'pretty';
/**
* @private
*/
const FORMAT_COMPACT = 'compact';
/**
* @var bool
*/
private $includeEmpty;
/**
* @var bool
*/
private $strictTypes;
/**
* @var string
*/
private $format;
/**
* Constructs a new ArrayGenerator
* @param array {
* includeEmpty?: bool, // Controls whether empty translations should be included (default: false)
* strictTypes?: bool, // Add declare(strict_types=1) (default: false)
* format?: 'normal'|'pretty'|'compact' // How to format the code (default: 'normal')
* pretty?: bool, // Deprecated: use format instead (default: false)
* } | null $options
*/
public function __construct(?array $options = null)
{
$this->includeEmpty = (bool) ($options['includeEmpty'] ?? false);
$this->strictTypes = (bool) ($options['strictTypes'] ?? false);
if (!isset($options['format'])) {
$this->format = ($options['pretty'] ?? false) ? self::FORMAT_PRETTY : self::FORMAT_NORMAL;
} elseif (in_array($options['format'], [self::FORMAT_PRETTY, self::FORMAT_COMPACT], true)) {
$this->format = $options['format'];
} else {
$this->format = self::FORMAT_NORMAL;
}
}
public function generateString(Translations $translations): string
{
$array = $this->generateArray($translations);
$result = '<?php';
if ($this->strictTypes) {
switch ($this->format) {
case self::FORMAT_PRETTY:
$result .= "\n\ndeclare(strict_types=1);\n\n";
break;
case self::FORMAT_COMPACT:
$result .= ' declare(strict_types=1);';
break;
case self::FORMAT_NORMAL:
default:
$result .= ' declare(strict_types=1); ';
break;
}
} else {
$result .= $this->format === self::FORMAT_PRETTY ? "\n\n" : ' ';
}
switch ($this->format) {
case self::FORMAT_PRETTY:
$result .= self::format($array, false);
break;
case self::FORMAT_COMPACT:
$result .= self::format($array, true);
break;
case self::FORMAT_NORMAL:
default:
$result .= 'return ' . var_export($array, true) . ';';
break;
}
return $result;
}
public function generateArray(Translations $translations): array
{
$pluralForm = $translations->getHeaders()->getPluralForm();
$pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null;
$messages = [];
foreach ($translations as $translation) {
if ((!$this->includeEmpty && !$translation->getTranslation()) || $translation->isDisabled()) {
continue;
}
$context = $translation->getContext() ?: '';
$original = $translation->getOriginal();
if (!isset($messages[$context])) {
$messages[$context] = [];
}
if (self::hasPluralTranslations($translation)) {
$messages[$context][$original] = $translation->getPluralTranslations($pluralSize);
array_unshift($messages[$context][$original], $translation->getTranslation());
} elseif ($pluralSize !== null && $pluralSize > 1 && (string) $translation->getPlural() !== '') {
$messages[$context][$original] = array_fill(0, $pluralSize, '');
} else {
$messages[$context][$original] = (string) $translation->getTranslation();
}
}
return [
'domain' => $translations->getDomain(),
'plural-forms' => $translations->getHeaders()->get(Headers::HEADER_PLURAL),
'messages' => $messages,
];
}
private static function hasPluralTranslations(Translation $translation): bool
{
return implode('', $translation->getPluralTranslations()) !== '';
}
private static function format(array &$array, bool $compact): string
{
return 'return ' . self::formatArray($array, $compact, 0) . ($compact ? ';' : ";\n");
}
private static function formatArray(array &$array, bool $compact, int $depth): string
{
if ($array === []) {
return '[]';
}
$result = '[';
$isList = self::isList($array);
foreach ($array as $key => $value) {
if (!$compact) {
$result .= "\n" . str_repeat(self::PRETTY_INDENT, $depth + 1);
}
if (!$isList) {
$result .= var_export($key, true) . ($compact ? '=>' : ' => ');
}
if (is_array($value)) {
$result .= self::formatArray($value, $compact, $depth + 1);
} else {
$result .= self::formatScalar($value);
}
$result .= ',';
}
if ($compact) {
$result = substr($result, 0, -1);
} else {
$result .= "\n" . str_repeat(self::PRETTY_INDENT, $depth);
}
return $result . ']';
}
private static function formatScalar($value): string
{
return $value === null ? 'null' : var_export($value, true);
}
private static function isList(array &$value): bool
{
if ($value === []) {
return true;
}
if (function_exists('array_is_list')) {
return \array_is_list($value);
}
return array_keys($value) === range(0, count($value) - 1);
}
}