From f9d63fdf5b15240c72dba886c9987623354b0bb7 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 12 Jul 2023 15:13:45 +1000 Subject: [PATCH] Fix return type for integer keys in associative arrays --- src/MultiSelectPrompt.php | 4 ++-- src/SelectPrompt.php | 2 +- src/helpers.php | 6 +++--- tests/Feature/MultiselectPromptTest.php | 15 +++++++++++++++ tests/Feature/SelectPromptTest.php | 15 +++++++++++++++ 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/MultiSelectPrompt.php b/src/MultiSelectPrompt.php index 9a0cbac2..795d01ed 100644 --- a/src/MultiSelectPrompt.php +++ b/src/MultiSelectPrompt.php @@ -22,7 +22,7 @@ class MultiSelectPrompt extends Prompt * Create a new SelectPrompt instance. * * @param array $options - * @param array $default + * @param array $default */ public function __construct( public string $label, @@ -46,7 +46,7 @@ public function __construct( /** * Get the selected values. * - * @return array + * @return array */ public function value(): array { diff --git a/src/SelectPrompt.php b/src/SelectPrompt.php index 51cc20b7..1bd86a8d 100644 --- a/src/SelectPrompt.php +++ b/src/SelectPrompt.php @@ -42,7 +42,7 @@ public function __construct( /** * Get the selected value. */ - public function value(): string + public function value(): int|string { if (array_is_list($this->options)) { return $this->options[$this->highlighted] ?? null; diff --git a/src/helpers.php b/src/helpers.php index 577ba460..a2e7f905 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -25,7 +25,7 @@ function password(string $label, string $placeholder = '', bool|string $required * * @param array $options */ -function select(string $label, array $options, ?string $default = null, int $scroll = 5, ?Closure $validate = null): string +function select(string $label, array $options, int|string|null $default = null, int $scroll = 5, ?Closure $validate = null): int|string { return (new SelectPrompt($label, $options, $default, $scroll, $validate))->prompt(); } @@ -34,8 +34,8 @@ function select(string $label, array $options, ?string $default = null, int $scr * Prompt the user to select multiple options. * * @param array $options - * @param array $default - * @return array + * @param array $default + * @return array */ function multiselect(string $label, array $options, array $default = [], int $scroll = 5, bool|string $required = false, ?Closure $validate = null): array { diff --git a/tests/Feature/MultiselectPromptTest.php b/tests/Feature/MultiselectPromptTest.php index 3ae68802..c172ae88 100644 --- a/tests/Feature/MultiselectPromptTest.php +++ b/tests/Feature/MultiselectPromptTest.php @@ -35,6 +35,21 @@ expect($result)->toBe(['green', 'blue']); }); +it('accepts an associate array with integer keys', function () { + Prompt::fake([Key::DOWN, Key::SPACE, Key::DOWN, Key::SPACE, Key::ENTER]); + + $result = multiselect( + label: 'What are your favorite colors?', + options: [ + 1 => 'Red', + 2 => 'Green', + 3 => 'Blue', + ] + ); + + expect($result)->toBe([2, 3]); +}); + it('accepts default values when the options are labels', function () { Prompt::fake([Key::ENTER]); diff --git a/tests/Feature/SelectPromptTest.php b/tests/Feature/SelectPromptTest.php index 6c734904..10e20e49 100644 --- a/tests/Feature/SelectPromptTest.php +++ b/tests/Feature/SelectPromptTest.php @@ -35,6 +35,21 @@ expect($result)->toBe('green'); }); +it('accepts an associate array with integer keys', function () { + Prompt::fake([Key::DOWN, Key::ENTER]); + + $result = select( + label: 'What is your favorite color?', + options: [ + 1 => 'Red', + 2 => 'Green', + 3 => 'Blue', + ] + ); + + expect($result)->toBe(2); +}); + it('accepts default values when the options are labels', function () { Prompt::fake([Key::ENTER]);