Down another rabbit hole: this is a simple function to return console input while checking that it is vaguely what is expected for the field (alpha, integer, etc.) and of acceptable length. The control features in the outer switch statement work fine, but nothing happens when entering input. What am I missing? [PHP CLI 8.3.21 on xubuntu 24.04]
function console(int $length, string $type) : string {
$input = [];
readline_callback_handler_install("", function() {});
$keypress = NULL;
while (is_null($keypress)) {
$keypress = stream_get_contents(STDIN, 1);
switch (ord($keypress)) {
case 10 : // return
return join($input);
break;
case 27 : // escape
return "\e";
break;
case 127 : // backspace
array_pop($input);
fwrite(STDOUT, chr(8) . chr(32) . chr(8));
break;
default :
if (count($input) < $length) {
switch ($type) {
case "I" : // integer
if (in_array($keypress, range("0", "9"))) {
$input[] = $keypress;
fwrite(STDOUT, $keypress);
}
break;
case "N" : // number
if (in_array($keypress, array_merge(range("0", "9"), [".", ",", "+", "-"]))) {
$input[] = $keypress;
fwrite(STDOUT, $keypress);
}
break;
case "D" : // date
if (in_array($keypress, array_merge(range("0", "9"), ["-"]))) {
$input[] = $keypress;
fwrite(STDOUT, $keypress);
}
break;
case "A" : // alphanum
$input[] = $keypress;
fwrite(STDOUT, $keypress);
break;
}
}
break;
}
$keypress = NULL;
}
}