Skip to content

Commit

Permalink
Update Console::ask
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Apr 22, 2024
1 parent bef2eaf commit a1e1b5e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
18 changes: 9 additions & 9 deletions app/Console/InteractiveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@

final readonly class InteractiveCommand
{
public function __construct(private Console $console)
{
}
public function __construct(private Console $console) {}

#[ConsoleCommand('interactive:validation')]
public function validation(): void
{
$a = $this->console->ask('a', validation: [new Length(min: 2), new Length(max:2)]);
$b = $this->console->ask('b', validation: [new Email()]);
$a = $this->console->ask('a', validation: [new Length(min: 2), new Length(max: 2)]);
$b = $this->console->ask('b', validation: [new Email()]);

$this->console->success("$a $b");
}
Expand Down Expand Up @@ -58,10 +56,12 @@ public function option(): void
#[ConsoleCommand('interactive:multiple')]
public function multiple(): void
{
$result = $this->console->component(new MultipleChoiceComponent(
'Pick several',
['a', 'b', 'c'],
), validation: [new Count(min: 1)]);
$result = $this->console->ask(
question: 'Pick several',
options: ['a', 'b', 'c'],
multiple: true,
validation: [new Count(min: 1)],
);

$result = json_encode($result);

Expand Down
6 changes: 4 additions & 2 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public function component(ConsoleComponent $component, array $validation = []):

/**
* @param string $question
* @param array $options
* @param array|null $options
* @param bool $multiple
* @param \Tempest\Validation\Rule[] $validation
* @return string
*/
public function ask(string $question, ?array $options = null, array $validation = []): string;
public function ask(string $question, ?array $options = null, bool $multiple = false, array $validation = []): string|array;

public function confirm(string $question, bool $default = false): bool;

Expand Down
13 changes: 9 additions & 4 deletions src/GenericConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Tempest\Console\Actions\RenderConsoleComponent;
use Tempest\Console\Components\ConfirmComponent;
use Tempest\Console\Components\MultipleChoiceComponent;
use Tempest\Console\Components\PasswordComponent;
use Tempest\Console\Components\ProgressBarComponent;
use Tempest\Console\Components\QuestionComponent;
Expand Down Expand Up @@ -74,11 +75,15 @@ public function component(ConsoleComponent $component, array $validation = []):
return (new RenderConsoleComponent($this))($component, $validation);
}

public function ask(string $question, ?array $options = null, array $validation = []): string
public function ask(string $question, ?array $options = null, bool $multiple = false, array $validation = []): string|array
{
$component = ($options === null || $options === [])
? new TextBoxComponent($question)
: new QuestionComponent($question, $options);
if ($options === null || $options === []) {
$component = new TextBoxComponent($question);
} elseif ($multiple) {
$component = new MultipleChoiceComponent($question, $options);
} else {
$component = new QuestionComponent($question, $options);
}

return $this->component($component, $validation);
}
Expand Down

0 comments on commit a1e1b5e

Please sign in to comment.