From 07315fdac241283ea0a9acebe8de773154502028 Mon Sep 17 00:00:00 2001 From: macocci7 Date: Fri, 2 Aug 2024 21:17:59 +0900 Subject: [PATCH] Add transform option --- playground/fileselector.php | 1 + playground/form.php | 5 ++++- src/FileSelector.php | 1 + src/FormBuilder.php | 20 ++++++++++---------- src/helpers.php | 2 +- tests/Feature/FileSelectorTest.php | 11 +++++++++++ 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/playground/fileselector.php b/playground/fileselector.php index efacb08..f7cd450 100644 --- a/playground/fileselector.php +++ b/playground/fileselector.php @@ -16,6 +16,7 @@ '.json', '.php', ], + transform: fn ($value) => realpath($value), ); var_dump($model); diff --git a/playground/form.php b/playground/form.php index b730928..230ff16 100644 --- a/playground/form.php +++ b/playground/form.php @@ -27,6 +27,7 @@ ! $value => 'Please enter your name.', default => null, }, + transform: fn ($value) => trim($value), ) ->text( label: 'Where should we create your project?', @@ -36,13 +37,15 @@ $value[0] !== '.' => 'Please enter a relative path', default => null, }, - name: 'path' + name: 'path', + transform: fn ($value) => trim($value), ) ->textarea('Describe your project') ->fileselector( label: 'Select the logo image.', placeholder: './public/img/logo.svg', hint: 'skip to use the default image.', + transform: fn ($value) => trim($value), ) ->pause() ->submit(); diff --git a/src/FileSelector.php b/src/FileSelector.php index f425fce..1bdf043 100644 --- a/src/FileSelector.php +++ b/src/FileSelector.php @@ -42,6 +42,7 @@ public function __construct( public mixed $validate = null, public string $hint = '', public array $extensions = [], + public ?Closure $transform = null, ) { static::$themes['default'][static::class] = FileSelectorRenderer::class; diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 58a47e8..ccb58c6 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -102,7 +102,7 @@ public function submit(): array /** * Prompt the user for text input. */ - public function text(string $label, string $placeholder = '', string $default = '', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self + public function text(string $label, string $placeholder = '', string $default = '', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null, ?Closure $transform = null): self { return $this->runPrompt(text(...), get_defined_vars()); } @@ -110,7 +110,7 @@ public function text(string $label, string $placeholder = '', string $default = /** * Prompt the user for multiline text input. */ - public function textarea(string $label, string $placeholder = '', string $default = '', bool|string $required = false, ?Closure $validate = null, string $hint = '', int $rows = 5, ?string $name = null): self + public function textarea(string $label, string $placeholder = '', string $default = '', bool|string $required = false, ?Closure $validate = null, string $hint = '', int $rows = 5, ?string $name = null, ?Closure $transform = null): self { return $this->runPrompt(textarea(...), get_defined_vars()); } @@ -118,7 +118,7 @@ public function textarea(string $label, string $placeholder = '', string $defaul /** * Prompt the user for input, hiding the value. */ - public function password(string $label, string $placeholder = '', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self + public function password(string $label, string $placeholder = '', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null, ?Closure $transform = null): self { return $this->runPrompt(password(...), get_defined_vars()); } @@ -129,7 +129,7 @@ public function password(string $label, string $placeholder = '', bool|string $r * @param array|Collection $options * @param true|string $required */ - public function select(string $label, array|Collection $options, int|string|null $default = null, int $scroll = 5, mixed $validate = null, string $hint = '', bool|string $required = true, ?string $name = null): self + public function select(string $label, array|Collection $options, int|string|null $default = null, int $scroll = 5, mixed $validate = null, string $hint = '', bool|string $required = true, ?string $name = null, ?Closure $transform = null): self { return $this->runPrompt(select(...), get_defined_vars()); } @@ -140,7 +140,7 @@ public function select(string $label, array|Collection $options, int|string|null * @param array|Collection $options * @param array|Collection $default */ - public function multiselect(string $label, array|Collection $options, array|Collection $default = [], int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = 'Use the space bar to select options.', ?string $name = null): self + public function multiselect(string $label, array|Collection $options, array|Collection $default = [], int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = 'Use the space bar to select options.', ?string $name = null, ?Closure $transform = null): self { return $this->runPrompt(multiselect(...), get_defined_vars()); } @@ -148,7 +148,7 @@ public function multiselect(string $label, array|Collection $options, array|Coll /** * Prompt the user to confirm an action. */ - public function confirm(string $label, bool $default = true, string $yes = 'Yes', string $no = 'No', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self + public function confirm(string $label, bool $default = true, string $yes = 'Yes', string $no = 'No', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null, ?Closure $transform = null): self { return $this->runPrompt(confirm(...), get_defined_vars()); } @@ -166,7 +166,7 @@ public function pause(string $message = 'Press enter to continue...', ?string $n * * @param array|Collection|Closure(string): array $options */ - public function suggest(string $label, array|Collection|Closure $options, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self + public function suggest(string $label, array|Collection|Closure $options, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null, ?Closure $transform = null): self { return $this->runPrompt(suggest(...), get_defined_vars()); } @@ -177,7 +177,7 @@ public function suggest(string $label, array|Collection|Closure $options, string * @param Closure(string): array $options * @param true|string $required */ - public function search(string $label, Closure $options, string $placeholder = '', int $scroll = 5, mixed $validate = null, string $hint = '', bool|string $required = true, ?string $name = null): self + public function search(string $label, Closure $options, string $placeholder = '', int $scroll = 5, mixed $validate = null, string $hint = '', bool|string $required = true, ?string $name = null, ?Closure $transform = null): self { return $this->runPrompt(search(...), get_defined_vars()); } @@ -187,7 +187,7 @@ public function search(string $label, Closure $options, string $placeholder = '' * * @param Closure(string): array $options */ - public function multisearch(string $label, Closure $options, string $placeholder = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = 'Use the space bar to select options.', ?string $name = null): self + public function multisearch(string $label, Closure $options, string $placeholder = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = 'Use the space bar to select options.', ?string $name = null, ?Closure $transform = null): self { return $this->runPrompt(multisearch(...), get_defined_vars()); } @@ -288,7 +288,7 @@ public function progress(string $label, iterable|int $steps, ?Closure $callback * * @param array $extensions */ - public function fileselector(string $label, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null, array $extensions = []): self + public function fileselector(string $label, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null, array $extensions = [], ?Closure $transform = null): self { return $this->runPrompt(fileselector(...), get_defined_vars()); } diff --git a/src/helpers.php b/src/helpers.php index c82b69a..58f9e86 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -20,7 +20,7 @@ function form(): FormBuilder * * @param array $extensions */ - function fileselector(string $label, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = '', array $extensions = []): string + function fileselector(string $label, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = '', array $extensions = [], ?Closure $transform = null): string { return (new FileSelector(...func_get_args()))->prompt(); } diff --git a/tests/Feature/FileSelectorTest.php b/tests/Feature/FileSelectorTest.php index 4c8a870..368c58e 100644 --- a/tests/Feature/FileSelectorTest.php +++ b/tests/Feature/FileSelectorTest.php @@ -147,3 +147,14 @@ expect($result)->toBe('./composer.json'); }); + +it('transforms values', function () { + Prompt::fake(['v', 'e', 'n', 'd', 'o', 'r', Key::ENTER]); + + $result = fileselector( + label: 'Select a file.', + transform: fn ($value) => realpath($value), + ); + + expect($result)->toBe(realpath('vendor')); +});