Skip to content

Commit 255f784

Browse files
committed
fix: regex for input validation
- combine two validation to one while loop
1 parent 9a6331c commit 255f784

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

system/CLI/CLI.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -327,29 +327,20 @@ public static function promptByMultipleKeys(string $text, array $options): array
327327
$input = static::prompt($extraOutput) ?: 0; // 0 is default
328328

329329
// validation
330-
// return the prompt again if $input contain(s) non-numeric charachter, except a comma.
331330
while (true) {
332331
$pattern = preg_match_all('/^\d+(,\d+)*$/', trim($input));
333-
if (! $pattern) {
334-
static::error('Please select correctly.');
335-
CLI::newLine();
336-
$input = static::prompt($extraOutput) ?: 0;
337-
} else {
338-
break;
339-
}
340-
}
341-
// user enters a number that is not in the available options
342-
while (true) {
332+
343333
// separate input by comma and convert all to an int[]
344334
$inputToArray = array_map(static fn ($value) => (int) $value, explode(',', $input));
345335
// find max from key of $options
346336
$maxOptions = array_key_last($options);
347337
// find max from input
348338
$maxInput = max($inputToArray);
349339

350-
// if max from $options less than max from input
340+
// return the prompt again if $input contain(s) non-numeric charachter, except a comma.
341+
// And if max from $options less than max from input
351342
// it is mean user tried to access null value in $options
352-
if ($maxOptions < $maxInput) {
343+
if (! $pattern || $maxOptions < $maxInput) {
353344
static::error('Please select correctly.');
354345
CLI::newLine();
355346
$input = static::prompt($extraOutput) ?: 0;

0 commit comments

Comments
 (0)