Skip to content

Commit

Permalink
Update code & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Oct 3, 2023
1 parent d3f06fa commit 804c005
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ wp i18n make-php <source> [<destination>]

**EXAMPLES**

# Create PHP files for all MO files in the current directory.
# Create PHP files for all PO files in the current directory.
$ wp i18n make-php .

# Create a PHP file from a single PO file in a specific directory.
Expand Down
4 changes: 2 additions & 2 deletions features/makephp.feature
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Feature: Generate PHP files from PO files
"""
And the foo-plugin/foo-plugin-de_DE.php file should contain:
"""
'messages'=>[''=>['Foo Plugin'=>[0=>'Foo Plugin']]]
'messages'=>[''=>['Foo Plugin'=>['Foo Plugin']]]
"""

Scenario: Does include translations
Expand Down Expand Up @@ -163,5 +163,5 @@ Feature: Generate PHP files from PO files
And the return code should be 0
And the foo-plugin/foo-plugin-de_DE.php file should contain:
"""
'messages'=>[''=>['Foo Plugin'=>[0=>'Bar Plugin']]]
'messages'=>[''=>['Foo Plugin'=>['Bar Plugin']]]
"""
2 changes: 1 addition & 1 deletion src/MakePhpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MakePhpCommand extends WP_CLI_Command {
*
* ## EXAMPLES
*
* # Create PHP files for all MO files in the current directory.
* # Create PHP files for all PO files in the current directory.
* $ wp i18n make-php .
*
* # Create a PHP file from a single PO file in a specific directory.
Expand Down
75 changes: 55 additions & 20 deletions src/PhpArrayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,58 @@ class PhpArrayGenerator extends PhpArray {
public static function toString( Translations $translations, array $options = [] ) {
$array = static::generate( $translations, $options );

$language = $translations->getLanguage();
if ( null !== $language ) {
$array['language'] = $language;
}

$headers = [
'Plural-Forms' => 'plural-forms',
'X-Generator' => 'generator',
'X-Domain' => 'domain',
'Language' => 'language',
'X-Generator' => 'x-generator',
];

foreach ( $translations->getHeaders() as $name => $value ) {
if ( ! isset( $headers[ $name ] ) ) {
continue;
if ( isset( $headers[ $name ] ) ) {
$array[ $headers[ $name ] ] = $value;
}
}

return '<?php' . PHP_EOL . 'return ' . static::var_export( $array ) . ';';
}

/**
* 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' ) ) {
return array_is_list( $arr );
}

if ( ( array() === $arr ) || ( array_values( $arr ) === $arr ) ) {
return true;
}

$next_key = -1;

$array[ $headers[ $name ] ] = $value;
foreach ( $arr as $k => $v ) {
if ( ++$next_key !== $k ) {
return false;
}
}

return '<?php' . PHP_EOL . 'return ' . static::var_export( $array, true ) . ';';
return true;
}

/**
Expand All @@ -45,25 +81,24 @@ public static function toString( Translations $translations, array $options = []
* Like {@see var_export()} but "minified", using short array syntax
* and no newlines.
*
* @param mixed $value The variable you want to export.
* @param bool $return_only Optional. Whether to return the variable representation instead of outputing it. Default false.
* @return string|void The variable representation or void.
* @since 4.0.0
*
* @param mixed $value The variable you want to export.
* @return string The variable representation.
*/
public static function var_export( $value, $return_only = false ) {
private static function var_export( $value ) {
if ( ! is_array( $value ) ) {
return var_export( $value, $return_only );
return var_export( $value, true );
}

$entries = array();
foreach ( $value as $key => $val ) {
$entries[] = var_export( $key, true ) . '=>' . static::var_export( $val, true );
}

$code = '[' . implode( ',', $entries ) . ']';
if ( $return_only ) {
return $code;
$is_list = self::array_is_list( $value );

foreach ( $value as $key => $val ) {
$entries[] = $is_list ? self::var_export( $val ) : var_export( $key, true ) . '=>' . self::var_export( $val );
}

echo $code;
return '[' . implode( ',', $entries ) . ']';
}
}

0 comments on commit 804c005

Please sign in to comment.