Skip to content

Commit 3cf8337

Browse files
committed
update show command and add tests for specifying languages
1 parent e170475 commit 3cf8337

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

src/Commands/ShowCommand.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Console\Command;
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Str;
8+
use Symfony\Component\Console\Exception\InvalidArgumentException;
89
use Themsaid\Langman\Manager;
910

1011
class ShowCommand extends Command
@@ -52,7 +53,7 @@ class ShowCommand extends Command
5253
protected $files;
5354

5455
/**
55-
* Array of selected languages.
56+
* Array of displayable languages.
5657
*
5758
* @var array
5859
*/
@@ -81,14 +82,12 @@ public function handle()
8182

8283
$this->files = $this->filesFromKey();
8384

84-
$this->languages = $this->manager->languages();
85+
try {
86+
$this->languages = $this->getLanguages();
87+
} catch (InvalidArgumentException $e) {
88+
$this->error($e->getMessage());
8589

86-
if ($this->option('lang') != null) {
87-
$languages = explode(',', $this->option('lang'));
88-
if (!empty($diffLangagues = array_diff($languages, $this->languages))) {
89-
return $this->error('Unknown Langauges [ '.implode($diffLangagues,',').' ]');
90-
}
91-
$this->languages = explode(',', $this->option('lang'));
90+
return;
9291
}
9392

9493
$this->table(
@@ -110,11 +109,11 @@ private function tableRows()
110109

111110
foreach ($this->files as $languageKey => $file) {
112111
foreach ($filesContent[$languageKey] = Arr::dot($this->manager->getFileContent($file)) as $key => $value) {
113-
if (!$this->shouldShowKey($key)) {
112+
if (! $this->shouldShowKey($key)) {
114113
continue;
115114
}
116115

117-
$output[$key]['key'] = $key;
116+
$output[$key]['key'] = $key;
118117
$output[$key][$languageKey] = $value;
119118
}
120119
}
@@ -194,15 +193,37 @@ private function shouldShowKey($key)
194193
return true;
195194
}
196195

197-
if (!$this->option('close') && $key != $this->key) {
196+
if (! $this->option('close') && $key != $this->key) {
198197
return false;
199198
}
200199

201-
if ($this->option('close') && !Str::contains($key, $this->key)) {
200+
if ($this->option('close') && ! Str::contains($key, $this->key)) {
202201
return false;
203202
}
204203
}
205204

206205
return true;
207206
}
207+
208+
/**
209+
* Get the languages to be displayed in the command output.
210+
*
211+
* @return array
212+
*/
213+
private function getLanguages()
214+
{
215+
$allLanguages = $this->manager->languages();
216+
217+
if (! $this->option('lang')) {
218+
return $allLanguages;
219+
}
220+
221+
$userLanguages = explode(',', (string) $this->option('lang'));
222+
223+
if ($missingLanguages = array_diff($userLanguages, $allLanguages)) {
224+
throw new InvalidArgumentException('Unknown Language(s) ['.implode(',', $missingLanguages).'].');
225+
}
226+
227+
return $userLanguages;
228+
}
208229
}

tests/ShowCommandTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ public function testCommandOutputForFile()
2525
$this->assertRegExp('/age(?:.*)Age(?:.*)|(?: *)|/', $this->consoleOutput());
2626
}
2727

28+
public function testCommandOutputForFileAndSpecificLanguages()
29+
{
30+
$this->createTempFiles([
31+
'en' => ['user' => "<?php\n return ['name' => 'Name', 'age' => 'Age'];"],
32+
'nl' => ['user' => "<?php\n return ['name' => 'Naam'];"],
33+
'it_lang' => ['user' => "<?php\n return ['name' => 'Nome'];"],
34+
]);
35+
36+
$this->artisan('langman:show', ['key' => 'user', '--lang' => 'en,nl']);
37+
38+
$this->assertRegExp('/key(?:.*)en(?:.*)nl/', $this->consoleOutput());
39+
$this->assertRegExp('/name(?:.*)Name(?:.*)Naam/', $this->consoleOutput());
40+
$this->assertNotContains('Nome', $this->consoleOutput());
41+
$this->assertNotContains('it_lang', $this->consoleOutput());
42+
}
43+
2844
public function testCommandOutputForPackageFile()
2945
{
3046
$this->createTempFiles([

0 commit comments

Comments
 (0)