diff --git a/src/Commands/I18n.php b/src/Commands/I18n.php index b19bb25..8d5aaeb 100644 --- a/src/Commands/I18n.php +++ b/src/Commands/I18n.php @@ -6,6 +6,7 @@ use GuzzleHttp\Psr7\Request; use StellarWP\Pup\App; use StellarWP\Pup\Command\Command; +use StellarWP\Pup\I18nConfig; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -49,7 +50,7 @@ protected function execute( InputInterface $input, OutputInterface $output ) { if ( $results !== 0 ) { $io->writeln( 'Failed to download language files.' ); $io->writeln( 'Config:' ); - $io->writeln( json_encode( $i18n_config, JSON_PRETTY_PRINT ) ); + $io->writeln( (string) json_encode( $i18n_config, JSON_PRETTY_PRINT ) ); return $results; } } @@ -64,25 +65,25 @@ protected function execute( InputInterface $input, OutputInterface $output ) { /** * Downloads language files. * - * @param array $i18n_config + * @param I18nConfig $i18n_config * * @throws \GuzzleHttp\Exception\GuzzleException * * @return int */ - protected function download_language_files( array $i18n_config ): int { + protected function download_language_files( I18nConfig $i18n_config ): int { $io = $this->getIO(); $options = (object) [ - 'domain_path' => $i18n_config['path'] ?? '', - 'url' => $i18n_config['url'] ?? '', - 'slug' => $i18n_config['slug'] ?? '', - 'text_domain' => $i18n_config['textdomain'] ?? '', - 'file_format' => $i18n_config['file_format'] ?? '', - 'formats' => $i18n_config['formats'] ?? [], - 'filter' => (object) $i18n_config['filter'] ?? [], + 'domain_path' => $i18n_config->getPath(), + 'url' => $i18n_config->getUrl(), + 'slug' => $i18n_config->getSlug(), + 'text_domain' => $i18n_config->getTextdomain(), + 'file_format' => $i18n_config->getFileFormat(), + 'formats' => $i18n_config->getFormats(), + 'filter' => $i18n_config->getFilter(), ]; - $io->writeln( "Fetching language files for {$options->text_domain} from {$options->url}" ); + $io->writeln( "Fetching language files for {$options->text_domain} from {$options->url}" ); // @phpstan-ignore-line: Those are strings. $client = new Client(); @@ -108,7 +109,7 @@ protected function download_language_files( array $i18n_config ): int { } // Skip any translation set that doest match our min translated. - if ( $options->filter->minimum_percentage > $translation->percent_translated ) { + if ( $options->filter['minimum_percentage'] > $translation->percent_translated ) { continue; } diff --git a/src/Config.php b/src/Config.php index c08a58b..054f17f 100644 --- a/src/Config.php +++ b/src/Config.php @@ -22,6 +22,11 @@ class Config implements \JsonSerializable { */ protected $has_invalid_puprc = false; + /** + * @var array + */ + protected $i18n = []; + /** * @var string */ @@ -267,9 +272,13 @@ protected function getDefaultConfig() : array { /** * Returns the i18n settings. * - * @return array + * @return array */ public function getI18n(): array { + if ( ! empty( $this->i18n ) ) { + return $this->i18n; + } + $defaults = $this->getI18nDefaults(); $i18n = $this->config->i18n ? (array) $this->config->i18n : []; @@ -298,13 +307,17 @@ public function getI18n(): array { } } - return $i18n; + foreach ( $i18n as $i18n_config ) { + $this->i18n[] = new I18nConfig( $i18n_config ); + } + + return $this->i18n; } /** * Returns the default i18n settings. * - * @return array + * @return array> */ public function getI18nDefaults(): array { return $this->config->i18n_defaults ? (array) $this->config->i18n_defaults : []; diff --git a/src/I18nConfig.php b/src/I18nConfig.php new file mode 100644 index 0000000..5cd7592 --- /dev/null +++ b/src/I18nConfig.php @@ -0,0 +1,126 @@ + + */ + protected $formats = []; + + /** + * @var array + */ + protected $filter = []; + + /** + * @param array> $args + */ + public function __construct( $args = [] ) { + if ( ! empty( $args['file_format'] ) && is_string( $args['file_format'] ) ) { + $this->file_format = $args['file_format']; + } + + if ( ! empty( $args['path'] ) && is_string( $args['path'] ) ) { + $this->path = $args['path']; + } + + if ( ! empty( $args['url'] ) && is_string( $args['url'] ) ) { + $this->url = $args['url']; + } + + if ( ! empty( $args['slug'] ) && is_string( $args['slug'] ) ) { + $this->slug = $args['slug']; + } + + if ( ! empty( $args['textdomain'] ) && is_string( $args['textdomain'] ) ) { + $this->textdomain = $args['textdomain']; + } + + if ( ! empty( $args['formats'] ) && is_array( $args['formats'] ) ) { + foreach ( $args['formats'] as $format ) { + $this->formats[] = (string) $format; + } + } + + if ( ! empty( $args['filter'] ) && is_array( $args['filter'] ) ) { + foreach ( $args['filter'] as $key => $filter ) { + $this->filter[ (string) $key ] = $filter; + } + } + } + + /** + * @return string + */ + public function getFileFormat(): string { + return $this->file_format; + } + + /** + * @return string + */ + public function getPath(): string { + return $this->path; + } + + /** + * @return string + */ + public function getUrl(): string { + return $this->url; + } + + /** + * @return string + */ + public function getSlug(): string { + return $this->slug; + } + + /** + * @return string + */ + public function getTextdomain(): string { + return $this->textdomain; + } + + /** + * @return array + */ + public function getFormats(): array { + return $this->formats; + } + + /** + * @return array + */ + public function getFilter(): array { + return $this->filter; + } +}