diff --git a/src/Commands/ShowCommand.php b/src/Commands/ShowCommand.php index 3bab72d..91bf920 100644 --- a/src/Commands/ShowCommand.php +++ b/src/Commands/ShowCommand.php @@ -5,6 +5,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Symfony\Component\Console\Exception\InvalidArgumentException; use Themsaid\Langman\Manager; class ShowCommand extends Command @@ -52,7 +53,7 @@ class ShowCommand extends Command protected $files; /** - * Array of selected languages. + * Array of displayable languages. * * @var array */ @@ -81,14 +82,12 @@ public function handle() $this->files = $this->filesFromKey(); - $this->languages = $this->manager->languages(); + try { + $this->languages = $this->getLanguages(); + } catch (InvalidArgumentException $e) { + $this->error($e->getMessage()); - if ($this->option('lang') != null) { - $languages = explode(',', $this->option('lang')); - if (!empty($diffLangagues = array_diff($languages, $this->languages))) { - return $this->error('Unknown Langauges [ '.implode($diffLangagues,',').' ]'); - } - $this->languages = explode(',', $this->option('lang')); + return; } $this->table( @@ -110,11 +109,11 @@ private function tableRows() foreach ($this->files as $languageKey => $file) { foreach ($filesContent[$languageKey] = Arr::dot($this->manager->getFileContent($file)) as $key => $value) { - if (!$this->shouldShowKey($key)) { + if (! $this->shouldShowKey($key)) { continue; } - $output[$key]['key'] = $key; + $output[$key]['key'] = $key; $output[$key][$languageKey] = $value; } } @@ -194,15 +193,37 @@ private function shouldShowKey($key) return true; } - if (!$this->option('close') && $key != $this->key) { + if (! $this->option('close') && $key != $this->key) { return false; } - if ($this->option('close') && !Str::contains($key, $this->key)) { + if ($this->option('close') && ! Str::contains($key, $this->key)) { return false; } } return true; } + + /** + * Get the languages to be displayed in the command output. + * + * @return array + */ + private function getLanguages() + { + $allLanguages = $this->manager->languages(); + + if (! $this->option('lang')) { + return $allLanguages; + } + + $userLanguages = explode(',', (string) $this->option('lang')); + + if ($missingLanguages = array_diff($userLanguages, $allLanguages)) { + throw new InvalidArgumentException('Unknown Language(s) ['.implode(',', $missingLanguages).'].'); + } + + return $userLanguages; + } } diff --git a/tests/ShowCommandTest.php b/tests/ShowCommandTest.php index c07b95b..af61f43 100644 --- a/tests/ShowCommandTest.php +++ b/tests/ShowCommandTest.php @@ -25,6 +25,22 @@ public function testCommandOutputForFile() $this->assertRegExp('/age(?:.*)Age(?:.*)|(?: *)|/', $this->consoleOutput()); } + public function testCommandOutputForFileAndSpecificLanguages() + { + $this->createTempFiles([ + 'en' => ['user' => " 'Name', 'age' => 'Age'];"], + 'nl' => ['user' => " 'Naam'];"], + 'it_lang' => ['user' => " 'Nome'];"], + ]); + + $this->artisan('langman:show', ['key' => 'user', '--lang' => 'en,nl']); + + $this->assertRegExp('/key(?:.*)en(?:.*)nl/', $this->consoleOutput()); + $this->assertRegExp('/name(?:.*)Name(?:.*)Naam/', $this->consoleOutput()); + $this->assertNotContains('Nome', $this->consoleOutput()); + $this->assertNotContains('it_lang', $this->consoleOutput()); + } + public function testCommandOutputForPackageFile() { $this->createTempFiles([