Skip to content
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
13 changes: 10 additions & 3 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,20 @@ public function validate(array $arguments, array $options) : array
*
* @return array<int,string>
*/
#[Pure]
protected function validateDefinitions(array $definitions, array $values, string $label) : array
{
$errors = [];
$translator = isset($this->console) ? $this->console->getLanguage() : null;
if ($translator) {
$label = $translator->render('cli', $label, []);
}
foreach ($definitions as $key => $definition) {
$value = $values[$key] ?? null;
if ($value === null || $value === false) {
if (!empty($definition['required'])) {
$errors[] = $label . ' "' . $key . '" is required.';
$errors[] = $translator
? $translator->render('cli', 'validation.required', [$label, (string) $key])
: $label . ' "' . $key . '" is required.';
}
continue;
}
Expand All @@ -374,7 +379,9 @@ protected function validateDefinitions(array $definitions, array $values, string
default => true,
};
if (!$valid) {
$errors[] = $label . ' "' . $key . '" must be of type ' . $type . '.';
$errors[] = $translator
? $translator->render('cli', 'validation.type', [$label, (string) $key, $type])
: $label . ' "' . $key . '" must be of type ' . $type . '.';
}
}
return $errors;
Expand Down
4 changes: 4 additions & 0 deletions src/Languages/en/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'about.line4' => 'Visit our website to know more: https://webisters.com',
'about.line5' => 'Thanks for using Webisters!',
'aliases' => 'Aliases',
'argument' => 'argument',
'availableCommands' => 'Available Commands',
'command' => 'Command',
'commandNotFound' => 'Command not found: "{0}"',
Expand All @@ -30,6 +31,9 @@
'index.description' => 'Shows commands list.',
'index.option.greet' => 'Shows greeting.',
'noDescription' => 'This command does not provide a description.',
'option' => 'option',
'options' => 'Options',
'usage' => 'Usage',
'validation.required' => '{0} "{1}" is required.',
'validation.type' => '{0} "{1}" must be of type {2}.',
];
4 changes: 4 additions & 0 deletions src/Languages/es/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'about.line5' => '¡Gracias por usar Webisters!',
'availableCommands' => 'Comandos Disponibles',
'aliases' => 'Alias',
'argument' => 'argumento',
'command' => 'Comando',
'commandNotFound' => 'Comando no encontrado: "{0}"',
'commands' => 'Comandos',
Expand All @@ -30,6 +31,9 @@
'index.description' => 'Muestra la lista de comandos.',
'index.option.greet' => 'Muestra saludo.',
'noDescription' => 'Este comando no proporciona una descripción.',
'option' => 'opción',
'options' => 'Opciones',
'usage' => 'Uso',
'validation.required' => '{0} "{1}" es obligatorio.',
'validation.type' => '{0} "{1}" debe ser del tipo {2}.',
];
4 changes: 4 additions & 0 deletions src/Languages/pt-br/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'about.line5' => 'Obrigado por usar o Webisters!',
'availableCommands' => 'Comandos Disponíveis',
'aliases' => 'Aliases',
'argument' => 'argumento',
'command' => 'Comando',
'commandNotFound' => 'Comando não encontrado: "{0}"',
'commands' => 'Comandos',
Expand All @@ -30,6 +31,9 @@
'index.description' => 'Mostra a lista de comandos.',
'index.option.greet' => 'Mostra saudação.',
'noDescription' => 'Este comando não fornece uma descrição.',
'option' => 'opção',
'options' => 'Opções',
'usage' => 'Uso',
'validation.required' => '{0} "{1}" é obrigatório.',
'validation.type' => '{0} "{1}" deve ser do tipo {2}.',
];
37 changes: 37 additions & 0 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Framework\CLI\Command;
use Framework\CLI\Streams\Stderr;
use Framework\CLI\Streams\Stdout;
use Framework\Language\Language;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -93,6 +94,42 @@ public function testMissingRequiredOptionReportsAnError() : void
self::assertStringContainsString('option "count" is required', Stderr::getContents());
}

public function testValidationErrorsAreTranslatedToSpanish() : void
{
$console = new ConsoleMock(new Language('es'));
$command = new ValidatedCommandMock($console);
$command->setArgumentDefinitions([
0 => ['type' => 'int', 'required' => true],
]);
$command->setOptionDefinitions([
'count' => ['type' => 'int', 'required' => true],
]);
$console->addCommand($command);
$console->exec('validated');
self::assertStringContainsString('argumento "0" es obligatorio.', Stderr::getContents());
self::assertStringContainsString('opción "count" es obligatorio.', Stderr::getContents());
$console->exec('validated abc');
self::assertStringContainsString('argumento "0" debe ser del tipo int.', Stderr::getContents());
}

public function testValidationErrorsAreTranslatedToBrazilianPortuguese() : void
{
$console = new ConsoleMock(new Language('pt-br'));
$command = new ValidatedCommandMock($console);
$command->setArgumentDefinitions([
0 => ['type' => 'int', 'required' => true],
]);
$command->setOptionDefinitions([
'count' => ['type' => 'int', 'required' => true],
]);
$console->addCommand($command);
$console->exec('validated');
self::assertStringContainsString('argumento "0" é obrigatório.', Stderr::getContents());
self::assertStringContainsString('opção "count" é obrigatório.', Stderr::getContents());
$console->exec('validated abc');
self::assertStringContainsString('argumento "0" deve ser do tipo int.', Stderr::getContents());
}

public function testGettersReturnTheDefinitions() : void
{
$command = new ValidatedCommandMock($this->console);
Expand Down