From 312e4f95a7cee2ba8d2af47b8fbe0476740d4f8d Mon Sep 17 00:00:00 2001 From: Goncalo Simoes Date: Mon, 28 Dec 2020 12:08:33 +0000 Subject: [PATCH] Add Data transformers for download --- lib/Transformers/Jed.php | 37 +++++++++++ lib/Transformers/Transformer.php | 25 ++++++++ lib/Translations.php | 29 ++++----- tests/Transformers/JedTest.php | 101 +++++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+), 16 deletions(-) create mode 100644 lib/Transformers/Jed.php create mode 100644 lib/Transformers/Transformer.php create mode 100644 tests/Transformers/JedTest.php diff --git a/lib/Transformers/Jed.php b/lib/Transformers/Jed.php new file mode 100644 index 0000000..48e5a1e --- /dev/null +++ b/lib/Transformers/Jed.php @@ -0,0 +1,37 @@ +get_wrap_jed() ) { + foreach ( $data as $key => $json ) { + $data[ $key ] = json_encode( + [ + 'locale_data' => json_decode( $json, true ), + ], + JSON_PRETTY_PRINT + ); + } + } + return $data; + } +} diff --git a/lib/Transformers/Transformer.php b/lib/Transformers/Transformer.php new file mode 100644 index 0000000..92cc096 --- /dev/null +++ b/lib/Transformers/Transformer.php @@ -0,0 +1,25 @@ +download(); - if ( $config->get_format() === 'jed' && $config->get_wrap_jed() ) { - $downloads = $this->wrap_jsons( $downloads ); - } + + $downloads = $this->apply_transformer( $downloads, $config ); $downloader->save( $downloads ); print( "Translation files downloaded and saved successfully.\n" ); @@ -92,21 +92,18 @@ public function make_pots() { } /** - * Wrap jsons with a layer with the key 'locale_data'. + * Apply transformer given the format for the data. * - * @since 0.0.0 - * @param array $jsons - * @return array + * @since 0.0.0 + * @return mixed */ - private function wrap_jsons( array $jsons ) : array { - foreach ( $jsons as $key => $json ) { - $jsons[ $key ] = json_encode( - [ - 'locale_data' => json_decode( $json, true ), - ], - JSON_PRETTY_PRINT - ); + private function apply_transformer( $data, Project $config ) { + //TODO: receive outside classes. + $transformer_class = __NAMESPACE__ . '\\Transformers\\' . ucfirst( $config->get_format() ); + if ( class_exists( $transformer_class ) ) { + $transfomer = new $transformer_class(); + return $transfomer->transform( $data, $config ); } - return $jsons; + return $data; } } diff --git a/tests/Transformers/JedTest.php b/tests/Transformers/JedTest.php new file mode 100644 index 0000000..2456781 --- /dev/null +++ b/tests/Transformers/JedTest.php @@ -0,0 +1,101 @@ +assertSame( $expected, ( new Jed() )->transform( $data, $config ) ); + } + + /** + * Data for testing transform. + * + * @since 0.0.0 + * @return array + */ + public function transformProvider() : array { + return [ + 'Empty data' => [ + [], + new Project( [ 'format' => 'jed' ] ), + [], + ], + 'Empty data with wrap' => [ + [], + new Project( + [ + 'format' => 'jed', + 'wrap-jed' => true, + ] + ), + [], + ], + 'Empty json data, default wrap' => [ + [ json_encode( [] ) ], + new Project( [ 'format' => 'jed' ] ), + [ json_encode( [ 'locale_data' => [] ], JSON_PRETTY_PRINT ) ], + ], + 'Empty json data with wrap' => [ + [ json_encode( [] ) ], + new Project( + [ + 'format' => 'jed', + 'wrap-jed' => true, + ] + ), + [ json_encode( [ 'locale_data' => [] ], JSON_PRETTY_PRINT ) ], + ], + 'Empty json data with wrap off' => [ + [ json_encode( [] ) ], + new Project( + [ + 'format' => 'jed', + 'wrap-jed' => False, + ] + ), + [ json_encode( [], JSON_PRETTY_PRINT ) ], + ], + 'Data with two jsons with wrap' => [ + [ + json_encode( [ 'test' => '1' ] ), + json_encode( [ 'test' => '2' ] ) + ], + new Project( + [ + 'format' => 'jed', + 'wrap-jed' => True, + ] + ), + [ + json_encode( [ 'locale_data' => [ 'test' => '1' ] ], JSON_PRETTY_PRINT ), + json_encode( [ 'locale_data' => [ 'test' => '2' ] ], JSON_PRETTY_PRINT ), + ], + ], + ]; + } +}