From e2f8363033239d3883179dd2d14482f33db2f75d Mon Sep 17 00:00:00 2001 From: sauvank Date: Mon, 3 Aug 2020 22:37:58 +0200 Subject: [PATCH] Added choice of confirmation message; --- src/ConsoleTable/Readline.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/ConsoleTable/Readline.php b/src/ConsoleTable/Readline.php index 52c8a27..947b717 100644 --- a/src/ConsoleTable/Readline.php +++ b/src/ConsoleTable/Readline.php @@ -45,11 +45,25 @@ public function getAnswer(){ return $this->answer; } - public function confirm() :bool { + /** + * Show confirm message to CLI + * @param string $msg message to show, by default : Do you want to confirm your choice ? + * @param array|string[] $option list of options available by default [y,n] + * @param int $defaultIndexValue index of the array of the default value (if the user validates without giving an answer). default 1 + * @param int $indexConfirmOption Index of the $option value that corresponds to the validation + * @return bool + * @throws \Exception + */ + public function confirm(string $msg = 'Do you want to confirm your choice ?', array $option = ['y', 'n'], $defaultIndexValue = 1, $indexConfirmOption = 0) :bool { + + if(!isset($option[$indexConfirmOption])){ + throw new \Exception("invalid defaultIndexValue params !\n"); + } + $previousChoice = $this->answer; - $this->choices("Choice: ".$this->answer."\nyou confirmed your choice ?", ['y', 'n'], 1); + $this->choices($msg, $option, $defaultIndexValue); $choice = $this->answer; $this->answer = $previousChoice; - return strtolower($choice) === "y"; + return strtolower($choice) === $option[$indexConfirmOption]; } }