Skip to content

Commit

Permalink
Merge pull request #7 from macocci7/add_transform_option
Browse files Browse the repository at this point in the history
Add transform option
  • Loading branch information
macocci7 authored Aug 21, 2024
2 parents 6ec4359 + 07315fd commit a9e6d67
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions playground/fileselector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'.json',
'.php',
],
transform: fn ($value) => realpath($value),
);

var_dump($model);
Expand Down
5 changes: 4 additions & 1 deletion playground/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
! $value => 'Please enter your name.',
default => null,
},
transform: fn ($value) => trim($value),
)
->text(
label: 'Where should we create your project?',
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/FileSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
20 changes: 10 additions & 10 deletions src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,23 @@ 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());
}

/**
* 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());
}

/**
* 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());
}
Expand All @@ -129,7 +129,7 @@ public function password(string $label, string $placeholder = '', bool|string $r
* @param array<int|string, string>|Collection<int|string, string> $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());
}
Expand All @@ -140,15 +140,15 @@ public function select(string $label, array|Collection $options, int|string|null
* @param array<int|string, string>|Collection<int|string, string> $options
* @param array<int|string>|Collection<int, int|string> $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());
}

/**
* 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());
}
Expand All @@ -166,7 +166,7 @@ public function pause(string $message = 'Press enter to continue...', ?string $n
*
* @param array<string>|Collection<int, string>|Closure(string): array<string> $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());
}
Expand All @@ -177,7 +177,7 @@ public function suggest(string $label, array|Collection|Closure $options, string
* @param Closure(string): array<int|string, string> $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());
}
Expand All @@ -187,7 +187,7 @@ public function search(string $label, Closure $options, string $placeholder = ''
*
* @param Closure(string): array<int|string, string> $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());
}
Expand Down Expand Up @@ -288,7 +288,7 @@ public function progress(string $label, iterable|int $steps, ?Closure $callback
*
* @param array<string> $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());
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function form(): FormBuilder
*
* @param array<string> $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();
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Feature/FileSelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});

0 comments on commit a9e6d67

Please sign in to comment.