Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Data transformers for download #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/Transformers/Jed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace TwentySixB\Translations\Transformers;

use TwentySixB\Translations\Config\Project;

/**
* Transformer for Jed formatted data.
*
* @since 0.0.0
* @package TODO:
* @subpackage TODO:
* @author TODO:
*/
final class Jed implements Transformer {

/**
* Wrap the json data with a `locale_data` key.
*
* @since 0.0.0
* @param mixed $data
* @param Project $config
* @return mixed
*/
public function transform( $data, Project $config ) {
if ( $config->get_wrap_jed() ) {
foreach ( $data as $key => $json ) {
$data[ $key ] = json_encode(
[
'locale_data' => json_decode( $json, true ),
],
JSON_PRETTY_PRINT
);
}
}
return $data;
}
}
25 changes: 25 additions & 0 deletions lib/Transformers/Transformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace TwentySixB\Translations\Transformers;

use TwentySixB\Translations\Config\Project;

/**
* Interface for data transformers.
*
* @since 0.0.0
* @package TODO:
* @subpackage TODO:
* @author TODO:
*/
interface Transformer {

/**
* Transform the data according to the Project config.
*
* @since 0.0.0
* @param mixed $data
* @param Project $config
* @return mixed
*/
public function transform( $data, Project $config );
}
29 changes: 13 additions & 16 deletions lib/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use TwentySixB\Translations\Config\Config;
use TwentySixB\Translations\Config\Project;
use TwentySixB\Translations\Input\Input;
use TwentySixB\Translations\Translations\Download;
use TwentySixB\Translations\Translations\PotMaker;
Expand Down Expand Up @@ -39,9 +40,8 @@ public function download() {
print( "Downloader created successfully.\n" );

$downloads = $downloader->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" );
Expand Down Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually make sense in this way. What we'll have is a string with the class name for the transformer in the config file. So it'll not come as an argument, but will work mostly as is here.

It's more like this (just a rough example):

$transformer_class = $config->get_tranformer() ? $config->get_tranformer() : __NAMESPACE__ . '\\Transformers\\' . ucfirst( $config->get_format() );

$transformer_class = __NAMESPACE__ . '\\Transformers\\' . ucfirst( $config->get_format() );
if ( class_exists( $transformer_class ) ) {
$transfomer = new $transformer_class();
return $transfomer->transform( $data, $config );
Comment on lines +103 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O $transformer_class que tens agora poderá ser a base, mas seria bom poder receber uma classe de fora para ser mais extensível. Futuro pull request ;)

O resto da lógica para remover complexidade e aumentar o que é testado.

Suggested change
if ( class_exists( $transformer_class ) ) {
$transfomer = new $transformer_class();
return $transfomer->transform( $data, $config );
if ( ! class_exists( $transformer_class ) ) {
return $data;
}
$transfomer = new $transformer_class();
if ( ! $transformer instanceof Transformer ) {
return $data;
}
return $transfomer->transform( $data, $config );

}
return $jsons;
return $data;
}
}
101 changes: 101 additions & 0 deletions tests/Transformers/JedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);

namespace Tests\Transformers;

use PHPUnit\Framework\TestCase;
use TwentySixB\Translations\Config\Project;
use TwentySixB\Translations\Transformers\Jed;

/**
* Testing the Jed class.
*
* @since 0.0.0
* @package TODO:
* @subpackage TODO:
* @author TODO:
*/
class JedTest extends TestCase {

/**
* Testing transform returns what is expected.
*
* @since 0.0.0
*
* @dataProvider transformProvider
* @covers ::transform
* @testdox transform - returns what is expected
*
* @return void
*/
public function testTransform( $data, $config, $expected ) : void {
$this->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 ),
],
],
];
}
}