|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Inhere\Console\Component\Interact; |
| 4 | + |
| 5 | +use Inhere\Console\Component\InteractMessage; |
| 6 | +use Inhere\Console\Console; |
| 7 | +use Inhere\Console\Util\Show; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class LimitedAsk |
| 11 | + * @package Inhere\Console\Component\Interact |
| 12 | + */ |
| 13 | +class LimitedAsk extends InteractMessage |
| 14 | +{ |
| 15 | + |
| 16 | + /** |
| 17 | + * Ask a question, ask for a limited number of times |
| 18 | + * 若输入了值且验证成功则返回 输入的结果 |
| 19 | + * 否则,会连续询问 $times 次, 若仍然错误,退出 |
| 20 | + * @param string $question 问题 |
| 21 | + * @param string $default 默认值 |
| 22 | + * @param \Closure $validator (默认验证输入是否为空)自定义回调验证输入是否符合要求; 验证成功返回true 否则 可返回错误消息 |
| 23 | + * @example This is an example |
| 24 | + * |
| 25 | + * ```php |
| 26 | + * // no default value |
| 27 | + * Interact::limitedAsk('please entry you age?', null, function($age) |
| 28 | + * { |
| 29 | + * if ($age<1 || $age>100) { |
| 30 | + * Interact::error('Allow the input range is 1-100'); |
| 31 | + * return false; |
| 32 | + * } |
| 33 | + * return true; |
| 34 | + * } ); |
| 35 | + * |
| 36 | + * // has default value |
| 37 | + * Interact::limitedAsk('please entry you age?', 89, function($age) |
| 38 | + * { |
| 39 | + * if ($age<1 || $age>100) { |
| 40 | + * Interact::error('Allow the input range is 1-100'); |
| 41 | + * return false; |
| 42 | + * } |
| 43 | + * return true; |
| 44 | + * } ); |
| 45 | + * ``` |
| 46 | + * |
| 47 | + * @param int $times Allow input times |
| 48 | + * @return string |
| 49 | + */ |
| 50 | + public static function ask( |
| 51 | + string $question, |
| 52 | + string $default = '', |
| 53 | + \Closure $validator = null, |
| 54 | + int $times = 3 |
| 55 | + ): string { |
| 56 | + if (!$question = \trim($question)) { |
| 57 | + Show::error('Please provide a question text!', 1); |
| 58 | + } |
| 59 | + |
| 60 | + $answer = ''; |
| 61 | + $back = $times = ($times > 6 || $times < 1) ? 3 : $times; |
| 62 | + |
| 63 | + $question = \ucfirst($question); |
| 64 | + $hasDefault = '' !== $default; |
| 65 | + |
| 66 | + if ($hasDefault) { |
| 67 | + $message = "<comment>{$question}</comment>(default: <info>$default</info>) "; |
| 68 | + } else { |
| 69 | + $message = "<comment>{$question}</comment>"; |
| 70 | + Console::write($message); |
| 71 | + } |
| 72 | + |
| 73 | + while ($times--) { |
| 74 | + if ($hasDefault) { |
| 75 | + $answer = Console::readln($message); |
| 76 | + |
| 77 | + if ('' === $answer) { |
| 78 | + $answer = $default; |
| 79 | + break; |
| 80 | + } |
| 81 | + } else { |
| 82 | + $num = $times + 1; |
| 83 | + $answer = Console::readln(\sprintf('(You have [<bold>%s</bold>] chances to enter!) ', $num)); |
| 84 | + } |
| 85 | + |
| 86 | + // If setting verify callback |
| 87 | + if ($validator && true === $validator($answer) ) { |
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | + // no setting verify callback |
| 92 | + if (!$validator && $answer !== '') { |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if ('' !== $answer) { |
| 98 | + return $answer; |
| 99 | + } |
| 100 | + |
| 101 | + if ($hasDefault) { |
| 102 | + return $default; |
| 103 | + } |
| 104 | + |
| 105 | + Show::write("\n You've entered incorrectly <danger>$back</danger> times in a row. exit!", true, 1); |
| 106 | + return ''; |
| 107 | + } |
| 108 | +} |
0 commit comments