public function ask(InputInterface $input, OutputInterface $output, Question $question) { $validator = $question->getValidator(); $question->setValidator(function ($value) use($validator) { if (null !== $validator) { $value = $validator($value); } if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) { throw new LogicException('A value is required.'); } return $value; }); return parent::ask($input, $output, $question); }
/** * Validates an attempt. * * @param callable $interviewer A callable that will ask for a question and return the result * @param OutputInterface $output An Output instance * @param Question $question A Question instance * * @return string The validated response * * @throws \Exception In case the max number of attempts has been reached and no valid response has been given */ private function validateAttempts(callable $interviewer, OutputInterface $output, Question $question) { $error = null; $attempts = $question->getMaxAttempts(); while (null === $attempts || $attempts--) { if (null !== $error) { $this->writeError($output, $error); } try { return call_user_func($question->getValidator(), $interviewer()); } catch (\Exception $error) { } } throw $error; }
/** * Validates an attempt. * * @param callable $interviewer A callable that will ask for a question and return the result * @param OutputInterface $output An Output instance * @param Question $question A Question instance * * @return string The validated response * * @throws \Exception In case the max number of attempts has been reached and no valid response has been given */ private function validateAttempts($interviewer, OutputInterface $output, Question $question) { $error = null; $attempts = $question->getMaxAttempts(); while (null === $attempts || $attempts--) { if (null !== $error) { if (null !== $this->getHelperSet() && $this->getHelperSet()->has('formatter')) { $message = $this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'); } else { $message = '<error>' . $error->getMessage() . '</error>'; } $output->writeln($message); } try { return call_user_func($question->getValidator(), $interviewer()); } catch (\Exception $error) { } } throw $error; }