-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathPhpArrayGenerator.php
More file actions
186 lines (157 loc) · 5.04 KB
/
PhpArrayGenerator.php
File metadata and controls
186 lines (157 loc) · 5.04 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
<?php
namespace WP_CLI\I18n;
use Gettext\Generators\PhpArray;
use Gettext\Translation;
use Gettext\Translations;
/**
* PHP array file generator.
*
* Returns output in the form WordPress uses.
*/
class PhpArrayGenerator extends PhpArray {
public static $options = [
'includeHeaders' => false,
'prettyPrint' => false,
];
/**
* {@inheritdoc}
*/
public static function toString( Translations $translations, array $options = [] ) {
$options = array_merge( static::$options, $options );
$array = static::generate( $translations, $options );
return '<?php' . PHP_EOL . 'return ' . static::var_export( $array, $options['prettyPrint'] ) . ';';
}
/**
* Generates an array with the translations.
*
* @param Translations $translations
* @param array $options
*
* @return array
*/
public static function generate( Translations $translations, array $options = [] ) {
$options += static::$options;
return static::toArray( $translations, $options['includeHeaders'] );
}
/**
* Returns an array containing headers and translations.
*
* @param Translations $translations
* @param bool $include_headers
* @param bool $force_array Unused.
*
* @return array
*/
protected static function toArray( Translations $translations, $include_headers, $force_array = false ) {
$messages = [];
$result = [
'domain' => $translations->getDomain(),
'plural-forms' => $translations->getHeader( 'Plural-Forms' ),
];
$language = $translations->getLanguage();
if ( null !== $language ) {
$result['language'] = $language;
}
$headers_allowlist = [
'POT-Creation-Date' => 'pot-creation-date',
'PO-Revision-Date' => 'po-revision-date',
'Project-Id-Version' => 'project-id-version',
'X-Generator' => 'x-generator',
];
foreach ( $translations->getHeaders() as $name => $value ) {
if ( isset( $headers_allowlist[ $name ] ) ) {
$result[ $headers_allowlist[ $name ] ] = $value;
}
}
/**
* @var Translation $translation
*/
foreach ( $translations as $translation ) {
if ( $translation->isDisabled() || ! $translation->hasTranslation() ) {
continue;
}
$context = $translation->getContext();
$original = $translation->getOriginal();
$key = $context ? $context . "\4" . $original : $original;
if ( $translation->hasPluralTranslations() ) {
$msg_translations = $translation->getPluralTranslations();
array_unshift( $msg_translations, $translation->getTranslation() );
$messages[ $key ] = implode( "\0", $msg_translations );
} else {
$messages[ $key ] = $translation->getTranslation();
}
}
$result['messages'] = $messages;
return $result;
}
/**
* Determines if the given array is a list.
*
* An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1.
*
* Polyfill for array_is_list() in PHP 8.1.
*
* @see https://github.com/symfony/polyfill-php81/tree/main
*
* @since 4.0.0
*
* @codeCoverageIgnore
*
* @param array<mixed> $arr The array being evaluated.
* @return bool True if array is a list, false otherwise.
*/
private static function array_is_list( array $arr ) {
if ( function_exists( 'array_is_list' ) ) {
// phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.array_is_listFound
return array_is_list( $arr );
}
if ( ( array() === $arr ) || ( array_values( $arr ) === $arr ) ) {
return true;
}
$next_key = -1;
foreach ( $arr as $k => $v ) {
if ( ++$next_key !== $k ) {
return false;
}
}
return true;
}
/**
* Outputs or returns a parsable string representation of a variable.
*
* Like {@see var_export()} but "minified", using short array syntax
* and no newlines.
*
* @since 4.0.0
*
* @param mixed $value The variable you want to export.
* @param bool $pretty_print Whether to pretty-print the output.
* @param int $indent_level Current indentation level (used for recursion).
* @return string The variable representation.
*/
private static function var_export( $value, $pretty_print = false, $indent_level = 0 ) {
if ( ! is_array( $value ) ) {
return var_export( $value, true );
}
$entries = array();
$is_list = self::array_is_list( $value );
if ( $pretty_print ) {
$indent = str_repeat( "\t", $indent_level + 1 );
$closing_indent = str_repeat( "\t", $indent_level );
$separator = ',' . PHP_EOL;
foreach ( $value as $key => $val ) {
if ( $is_list ) {
$entries[] = $indent . self::var_export( $val, $pretty_print, $indent_level + 1 );
} else {
$entries[] = $indent . var_export( $key, true ) . ' => ' . self::var_export( $val, $pretty_print, $indent_level + 1 );
}
}
return '[' . PHP_EOL . implode( $separator, $entries ) . ',' . PHP_EOL . $closing_indent . ']';
} else {
foreach ( $value as $key => $val ) {
$entries[] = $is_list ? self::var_export( $val, $pretty_print, $indent_level ) : var_export( $key, true ) . '=>' . self::var_export( $val, $pretty_print, $indent_level );
}
return '[' . implode( ',', $entries ) . ']';
}
}
}