public function ask(IQuestion $question, IResponse $response)
    {
        $response->write("<question>{$question->getText()}</question>");

        if ($question instanceof MultipleChoice) {
            /** @var MultipleChoice $question */
            $response->writeln('');
            $choicesAreAssociative = $question->choicesAreAssociative();
            $choiceTexts = [];

            foreach ($question->getChoices() as $key => $choice) {
                if (!$choicesAreAssociative) {
                    // Make the choice 1-indexed
                    ++$key;
                }

                $choiceTexts[] = [$key . ')', $choice];
            }

            $response->writeln($this->paddingFormatter->format($choiceTexts, function ($row) {
                return "  {$row[0]} {$row[1]}";
            }));
            $response->write($question->getAnswerLineString());
        }

        $answer = fgets($this->inputStream, 4096);

        if ($answer === false) {
            throw new RuntimeException('Failed to get answer');
        }

        $answer = trim($answer);

        if ($answer === '') {
            $answer = $question->getDefaultAnswer();
        }

        return $question->formatAnswer($answer);
    }
public function ask(IQuestion $question, IResponse $response)
    {
        $response->write("<question>{$question->getText()}</question>");

        if ($question instanceof MultipleChoice) {
            /** @var MultipleChoice $question */
            $response->writeln('');
            $choicesAreAssociative = $question->choicesAreAssociative();
            $choiceTexts = [];

            foreach ($question->getChoices() as $key => $choice) {
                if (!$choicesAreAssociative) {
                    // Make the choice 1-indexed
                    ++$key;
                }

                $choiceTexts[] = [$key . ')', $choice];
            }

            $response->writeln($this->paddingFormatter->format($choiceTexts, function ($row) {
                return "  {$row[0]} {$row[1]}";
            }));
            $response->write($question->getAnswerLineString());
        }

        $answer = fgets($this->inputStream, 4096);

        if ($answer === false) {
            throw new RuntimeException('Failed to get answer');
        }

        $answer = trim($answer);

        if ($answer === '') {
            $answer = $question->getDefaultAnswer();
        }

        return $question->formatAnswer($answer);
    }
public function ask ( IQuestion $ question , IResponse $ response ) { $ response -> write ( "<question>{$question->getText()}</question>" ) ; if ( $ question instanceof MultipleChoice ) { /** @var MultipleChoice $question */ $ response -> writeln ( '' ) ; $ choicesAreAssociative = $ question -> choicesAreAssociative ( ) ; $ choiceTexts = [ ] ; foreach ( $ question -> getChoices ( ) as $ key => $ choice ) { if ( ! $ choicesAreAssociative ) { // Make the choice 1-indexed ++ $ key ; } $ choiceTexts [ ] = [ $ key . ')' , $ choice ] ; } $ response -> writeln ( $ this -> paddingFormatter -> format ( $ choiceTexts , function ( $ row ) { return "  {$row[0]} {$row[1]}" ; } ) ) ; $ response -> write ( $ question -> getAnswerLineString ( ) ) ; } $ answer = fgets ( $ this -> inputStream , 4096 ) ; if ( $ answer === false ) { throw new RuntimeException ( 'Failed to get answer' ) ; } $ answer = trim ( $ answer ) ; if ( $ answer === '' ) { $ answer = $ question -> getDefaultAnswer ( ) ; } return $ question -> formatAnswer ( $ answer ) ; }
Prompts the user to answer a question

@param IQuestion $question The question to ask
@param IResponse $response The response to write output to
@return mixed The user's answer to the question
@throws RuntimeException Thrown if we failed to get the user's answer
Prompts the user to answer a question