master

laravel/framework

Last updated at: 29/12/2023 09:21

Choice.php

TLDR

This file is part of the Illuminate\Console\View\Components namespace and contains a class called Choice. The class has a single method called render which renders a component using the given arguments.

Methods

render

Renders the component using the given arguments. It accepts the following parameters:

  • question: A string representing the question to ask the user.
  • choices: An array of strings representing the choices for the user.
  • default: (Optional) The default choice.
  • attempts: (Optional) The maximum number of attempts the user has to provide a valid choice.
  • multiple: (Optional) A boolean indicating if the user can select multiple choices.

The method returns the result of calling the askQuestion method of the output object, passing a new instance of Symfony\Component\Console\Question\ChoiceQuestion with the provided parameters.

Classes

Choice

Represents a component that can render a choice question to the user.

<?php

namespace Illuminate\Console\View\Components;

use Symfony\Component\Console\Question\ChoiceQuestion;

class Choice extends Component
{
    /**
     * Renders the component using the given arguments.
     *
     * @param  string  $question
     * @param  array<array-key, string>  $choices
     * @param  mixed  $default
     * @param  int  $attempts
     * @param  bool  $multiple
     * @return mixed
     */
    public function render($question, $choices, $default = null, $attempts = null, $multiple = false)
    {
        return $this->usingQuestionHelper(
            fn () => $this->output->askQuestion(
                (new ChoiceQuestion($question, $choices, $default))
                    ->setMaxAttempts($attempts)
                    ->setMultiselect($multiple)
            ),
        );
    }
}