Skip to content

Commit 72e5035

Browse files
emenkensjessarcher
andauthored
Add transformation support (#156)
* Add transformation support * Formatting * Add transformation support to FormBuilder --------- Co-authored-by: Jess Archer <[email protected]>
1 parent a86e8d2 commit 72e5035

23 files changed

+194
-24
lines changed

playground/text.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
default => null,
1414
},
1515
hint: 'We will never share your email address with anyone else.',
16+
transform: fn ($value) => strtolower($value),
1617
);
1718

1819
var_dump($email);

playground/textarea.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
require __DIR__.'/../vendor/autoload.php';
66

7-
$email = textarea(
7+
$story = textarea(
88
label: 'Tell me a story',
99
placeholder: 'Weave me a tale',
1010
);
1111

12-
var_dump($email);
12+
var_dump($story);
1313

1414
echo str_repeat(PHP_EOL, 5);

src/ConfirmPrompt.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Laravel\Prompts;
44

5+
use Closure;
6+
57
class ConfirmPrompt extends Prompt
68
{
79
/**
@@ -20,6 +22,7 @@ public function __construct(
2022
public bool|string $required = false,
2123
public mixed $validate = null,
2224
public string $hint = '',
25+
public ?Closure $transform = null,
2326
) {
2427
$this->confirmed = $default;
2528

src/FormBuilder.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,23 @@ public function submit(): array
7878
/**
7979
* Prompt the user for text input.
8080
*/
81-
public function text(string $label, string $placeholder = '', string $default = '', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self
81+
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
8282
{
8383
return $this->runPrompt(text(...), get_defined_vars());
8484
}
8585

8686
/**
8787
* Prompt the user for multiline text input.
8888
*/
89-
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
89+
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
9090
{
9191
return $this->runPrompt(textarea(...), get_defined_vars());
9292
}
9393

9494
/**
9595
* Prompt the user for input, hiding the value.
9696
*/
97-
public function password(string $label, string $placeholder = '', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self
97+
public function password(string $label, string $placeholder = '', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null, ?Closure $transform = null): self
9898
{
9999
return $this->runPrompt(password(...), get_defined_vars());
100100
}
@@ -105,7 +105,7 @@ public function password(string $label, string $placeholder = '', bool|string $r
105105
* @param array<int|string, string>|Collection<int|string, string> $options
106106
* @param true|string $required
107107
*/
108-
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
108+
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
109109
{
110110
return $this->runPrompt(select(...), get_defined_vars());
111111
}
@@ -116,15 +116,15 @@ public function select(string $label, array|Collection $options, int|string|null
116116
* @param array<int|string, string>|Collection<int|string, string> $options
117117
* @param array<int|string>|Collection<int, int|string> $default
118118
*/
119-
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
119+
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
120120
{
121121
return $this->runPrompt(multiselect(...), get_defined_vars());
122122
}
123123

124124
/**
125125
* Prompt the user to confirm an action.
126126
*/
127-
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
127+
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
128128
{
129129
return $this->runPrompt(confirm(...), get_defined_vars());
130130
}
@@ -142,7 +142,7 @@ public function pause(string $message = 'Press enter to continue...', ?string $n
142142
*
143143
* @param array<string>|Collection<int, string>|Closure(string): array<string> $options
144144
*/
145-
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
145+
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
146146
{
147147
return $this->runPrompt(suggest(...), get_defined_vars());
148148
}
@@ -153,7 +153,7 @@ public function suggest(string $label, array|Collection|Closure $options, string
153153
* @param Closure(string): array<int|string, string> $options
154154
* @param true|string $required
155155
*/
156-
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
156+
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
157157
{
158158
return $this->runPrompt(search(...), get_defined_vars());
159159
}
@@ -163,7 +163,7 @@ public function search(string $label, Closure $options, string $placeholder = ''
163163
*
164164
* @param Closure(string): array<int|string, string> $options
165165
*/
166-
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
166+
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
167167
{
168168
return $this->runPrompt(multisearch(...), get_defined_vars());
169169
}

src/MultiSearchPrompt.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function __construct(
4242
public bool|string $required = false,
4343
public mixed $validate = null,
4444
public string $hint = '',
45+
public ?Closure $transform = null,
4546
) {
4647
$this->trackTypedValue(submit: false, ignore: fn ($key) => Key::oneOf([Key::SPACE, Key::HOME, Key::END, Key::CTRL_A, Key::CTRL_E], $key) && $this->highlighted !== null);
4748

src/MultiSelectPrompt.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Laravel\Prompts;
44

5+
use Closure;
56
use Illuminate\Support\Collection;
67

78
class MultiSelectPrompt extends Prompt
@@ -43,6 +44,7 @@ public function __construct(
4344
public bool|string $required = false,
4445
public mixed $validate = null,
4546
public string $hint = '',
47+
public ?Closure $transform = null,
4648
) {
4749
$this->options = $options instanceof Collection ? $options->all() : $options;
4850
$this->default = $default instanceof Collection ? $default->all() : $default;

src/PasswordPrompt.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Laravel\Prompts;
44

5+
use Closure;
6+
57
class PasswordPrompt extends Prompt
68
{
79
use Concerns\TypedValue;
@@ -15,6 +17,7 @@ public function __construct(
1517
public bool|string $required = false,
1618
public mixed $validate = null,
1719
public string $hint = '',
20+
public ?Closure $transform = null,
1821
) {
1922
$this->trackTypedValue();
2023
}

src/Prompt.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ abstract class Prompt
5050
*/
5151
public bool|string $required;
5252

53+
/**
54+
* The transformation callback.
55+
*/
56+
public ?Closure $transform = null;
57+
5358
/**
5459
* The validator callback or rules.
5560
*/
@@ -140,7 +145,7 @@ public function prompt(): mixed
140145
throw new FormRevertedException;
141146
}
142147

143-
return $this->value();
148+
return $this->transformedValue();
144149
}
145150
}
146151
} finally {
@@ -277,7 +282,7 @@ protected function render(): void
277282
*/
278283
protected function submit(): void
279284
{
280-
$this->validate($this->value());
285+
$this->validate($this->transformedValue());
281286

282287
if ($this->state !== 'error') {
283288
$this->state = 'submit';
@@ -322,12 +327,32 @@ private function handleKeyPress(string $key): bool
322327
}
323328

324329
if ($this->validated) {
325-
$this->validate($this->value());
330+
$this->validate($this->transformedValue());
326331
}
327332

328333
return true;
329334
}
330335

336+
/**
337+
* Transform the input.
338+
*/
339+
private function transform(mixed $value): mixed
340+
{
341+
if (is_null($this->transform)) {
342+
return $value;
343+
}
344+
345+
return call_user_func($this->transform, $value);
346+
}
347+
348+
/**
349+
* Get the transformed value of the prompt.
350+
*/
351+
protected function transformedValue(): mixed
352+
{
353+
return $this->transform($this->value());
354+
}
355+
331356
/**
332357
* Validate the input.
333358
*/

src/SearchPrompt.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function __construct(
3131
public mixed $validate = null,
3232
public string $hint = '',
3333
public bool|string $required = true,
34+
public ?Closure $transform = null,
3435
) {
3536
if ($this->required === false) {
3637
throw new InvalidArgumentException('Argument [required] must be true or a string.');

src/SelectPrompt.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Laravel\Prompts;
44

5+
use Closure;
56
use Illuminate\Support\Collection;
67
use InvalidArgumentException;
78

@@ -29,6 +30,7 @@ public function __construct(
2930
public mixed $validate = null,
3031
public string $hint = '',
3132
public bool|string $required = true,
33+
public ?Closure $transform = null,
3234
) {
3335
if ($this->required === false) {
3436
throw new InvalidArgumentException('Argument [required] must be true or a string.');

0 commit comments

Comments
 (0)