-
Notifications
You must be signed in to change notification settings - Fork 0
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
goncaloasimoes
wants to merge
1
commit into
main
Choose a base branch
from
feature/transformers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||||||||||||||||||||||
|
@@ -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" ); | ||||||||||||||||||||||||||||
|
@@ -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 ); | ||||||||||||||||||||||||||||
Comment on lines
+103
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. O O resto da lógica para remover complexidade e aumentar o que é testado.
Suggested change
|
||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
return $jsons; | ||||||||||||||||||||||||||||
return $data; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ), | ||
], | ||
], | ||
]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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):