Available steps - shomisha/laravel-console-wizard GitHub Wiki
Available Wizard steps
The package comes with several predefined Step
types for prompting users for input. Most of the steps rely on Laravel's input/output methods, but some add extra functionality. All of the steps are listed and explained below.
TextStep
The TextStep
is the most basic step type and it is a basic question that expects any text input as the answer. To have your Wizard ask a text question you need to create an instance of the TextQuestion
, pass it the text of your question and return it from your Wizards getSteps()
method.
<?php
namespace App\Console\Commands;
use Shomisha\LaravelConsoleWizard\Command\Wizard;
use Shomisha\LaravelConsoleWizard\Steps\TextStep;
class WizardTest extends Wizard
{
protected $signature = 'wizard:test';
protected $description = 'Wizard description';
function getSteps(): array
{
return [
'first-name' => new TextStep('Your first name'),
'last-name' => new TextStep('Your last name'),
];
}
function completed()
{
$firstName = $this->answers->get('first-name');
$lastName = $this->answers->get('last-name');
}
}
The wizard above would produce the following output
shomisha:laravel-console-wizard shomisha$ php artisan wizard:test
Your first name:
> Misa
Your last name:
> Kovic
The answer returned from TextQuestion
is just the string provided from the user. In the example above, $this->answers->get('first-name');
would return Misa
and $this->answers->get('last-name');
would return Kovic
MultipleAnswerTextStep
The MultipleAnswerTextStep
is rather similar to the TextStep
apart from the difference that it excepts multiple answers instead of a single one, and returns an array of strings instead of a single string.
Furthermore, in addition to accepting the text of the question when instantiating, MultipleAnswerTextStep
accepts additional options as an array. The options you can provide are end_keyword
which is a string and retain_end_keyword
which is a boolean. The end_keyword
is a keyword which when input by the user will stop asking the user for input, and when retain_end_keyword
is passed in as true it will return the end keyword as part of the answers. Since both of these parameters are optional, the default end keyword is Done
and the default for retaining it in the answers is false, meaning the end keyword will not be returned as part of the results.
<?php
namespace App\Console\Commands;
use Shomisha\LaravelConsoleWizard\Command\Wizard;
use Shomisha\LaravelConsoleWizard\Steps\MultipleAnswerTextStep;
class WizardTest extends Wizard
{
protected $signature = 'wizard:test';
protected $description = 'Wizard description';
function getSteps(): array
{
return [
'favourite-songs' => new MultipleAnswerTextStep('What are your favourite songs?'),
'favourite-movies' => new MultipleAnswerTextStep("\nWhat are your favourite movies?", [
'end_keyword' => 'No more',
'retain_end_keyword' => true,
]),
];
}
function completed()
{
$songs = $this->answers->get('favourite-songs');
$movies = $this->answers->get('favourite-movies');
}
}
The above Wizard would produce the following output
shomisha:laravel-console-wizard shomisha$ php artisan wizard:test
What are your favourite songs?
Ben Howard - End of The Affair
Milo Greene - Heartless
Done
What are your favourite movies?
The Prestige
About Time
No more
The answers returned from MultipleAnswerTextStep
are arrays of strings. In the example above, $this->answers->get('favourite-songs');
would return the following array ['Ben Howard - End of The Affair', 'Milo Greene - Heartles']
, whereas $this->answers->get('favourite-movies');
would return ['The Prestige', 'About Time', 'No more']
. The reason the string No more
is present in the results of the favourite-movies
step is that the retain_end_keyword
was passed in as true
, thus keeping the end keyword as part of the answers.
Please keep in mind that the end keyword is case insensitive. Inputting Done
or done
would both stop prompting the user for answers if the end keyword is either Done
or done
.
Choice step
The ChoiceStep
presents the user with multiple choices and allows them to select one. To instantiate a ChoiceStep
you are required to pass in two arguments, the string to be used as the question as the first argument, and an array of choices as the second argument.
<?php
namespace App\Console\Commands;
use Shomisha\LaravelConsoleWizard\Command\Wizard;
use Shomisha\LaravelConsoleWizard\Steps\ChoiceStep;
class WizardTest extends Wizard
{
protected $signature = 'wizard:test';
protected $description = 'Wizard description';
function getSteps(): array
{
return [
'favourite-language' => new ChoiceStep("What's your favourite programming language?", [
'PHP',
'JavaScript',
'Java',
'Python',
'C#'
]),
'favourite-php-framework' => new ChoiceStep("What's your favourite PHP framework?", [
'Laravel',
'Symfony',
'CodeIgniter',
'CakePHP',
'Yii'
]),
];
}
function completed()
{
$language = $this->answers->get('favourite-language');
$framework = $this->answers->get('favourite-php-framework');
}
}
The example above produces the following output
shomisha:laravel-console-wizard shomisha$ php artisan wizard:test
What's your favourite programming language?:
[0] PHP
[1] JavaScript
[2] Java
[3] Python
[4] C#
> 0
What's your favourite PHP framework?:
[0] Laravel
[1] Symfony
[2] CodeIgniter
[3] CakePHP
[4] Yii
> 0
The answers returned from the ChoiceStep
are simply the strings selected by the user. In the example above, $this->answers->get('favourite-language');
would be PHP
while $this->answers->get('favourite-php-framework');
would return Laravel
.
MultipleChoiceStep
The MultipleChoiceStep
is similar to the ChoiceStep
except it will keep asking the user for a selection until instructed to stop. Much like the MultipleAnswerTextStep
it accepts an array that can contain the end_keyword
and retain_end_keyword
parameters and modify the steps behaviour in the same way it does with the MultipleAnswerTextStep
.
<?php
namespace App\Console\Commands;
use Shomisha\LaravelConsoleWizard\Command\Wizard;
use Shomisha\LaravelConsoleWizard\Steps\MultipleChoiceStep;
class WizardTest extends Wizard
{
protected $signature = 'wizard:test';
protected $description = 'Wizard description';
function getSteps(): array
{
return [
'daily-foods' => new MultipleChoiceStep("What did you eat today?", [
'Bread',
'Butter',
'Meat',
'Cheese',
'Vegetables',
]),
'daily-beverages' => new MultipleChoiceStep("What did you drink today?", [
'Water',
'Milk',
'Juice',
'Soda',
'Beer',
'Wine',
], [
'end_keyword' => "That's all",
'retain_end_keyword' => true,
]),
];
}
function completed()
{
$foods = $this->answers->get('daily-foods');
$beverages = $this->answers->get('daily-beverages');
}
}
The example above produces the following output
shomisha:laravel-console-wizard shomisha$ php artisan wizard:test
What did you eat today?:
[0] Bread
[1] Butter
[2] Meat
[3] Cheese
[4] Vegetables
[5] Done
> 0
What did you eat today?:
[0] Bread
[1] Butter
[2] Meat
[3] Cheese
[4] Vegetables
[5] Done
> 2
What did you eat today?:
[0] Bread
[1] Butter
[2] Meat
[3] Cheese
[4] Vegetables
[5] Done
> 0
What did you eat today?:
[0] Bread
[1] Butter
[2] Meat
[3] Cheese
[4] Vegetables
[5] Done
> 3
What did you eat today?:
[0] Bread
[1] Butter
[2] Meat
[3] Cheese
[4] Vegetables
[5] Done
> 5
What did you drink today?:
[0] Water
[1] Milk
[2] Juice
[3] Soda
[4] Beer
[5] Wine
[6] That's all
> 0
What did you drink today?:
[0] Water
[1] Milk
[2] Juice
[3] Soda
[4] Beer
[5] Wine
[6] That's all
> 0
What did you drink today?:
[0] Water
[1] Milk
[2] Juice
[3] Soda
[4] Beer
[5] Wine
[6] That's all
> 2
What did you drink today?:
[0] Water
[1] Milk
[2] Juice
[3] Soda
[4] Beer
[5] Wine
[6] That's all
> 3
What did you drink today?:
[0] Water
[1] Milk
[2] Juice
[3] Soda
[4] Beer
[5] Wine
[6] That's all
> 3
What did you drink today?:
[0] Water
[1] Milk
[2] Juice
[3] Soda
[4] Beer
[5] Wine
[6] That's all
> 3
What did you drink today?:
[0] Water
[1] Milk
[2] Juice
[3] Soda
[4] Beer
[5] Wine
[6] That's all
> 6
MultipleChoiceStep
returns an array containing the strings the user selected, in the order they were selected. Namely, in the example above $this->answers->get('daily-foods');
would return ['Bread', 'Meat', 'Bread', 'Cheese']
, whereas $this->answers->get('daily-beverages');
would return ['Water', 'Water', 'Juice', 'Soda', 'Soda', 'Soda', 'That\'s all']
. As with the MultipleAnswerTextQuestion
, the reason That's all
is present in the answers is that the retain_end_keyword
option was set to true.
Please keep in mind that the user is allowed to select any choice any number of times they like. In this version it is impossible to constrain the number of choices a user can make using the MultipleChoiceStep
, but this is planned for a near-future release. If you want to allow a user to select more options, but constrain them to selecting each option no more than once, please take a look at the UniqueMultipleChoiceStep
below.
UniqueMultipleChoiceStep
The UniqueMultipleChoiceStep
is identical to the MultipleChoiceStep
with the slight difference of not allowing users to select a single choice more than once. It excepts the same arguments when instantiating, the text of the question asked, the choices for the user, and an array with the end_keyword
and retain_keyword
options.
<?php
namespace App\Console\Commands;
use Shomisha\LaravelConsoleWizard\Command\Wizard;
use Shomisha\LaravelConsoleWizard\Steps\UniqueMultipleChoiceStep;
class WizardTest extends Wizard
{
protected $signature = 'wizard:test';
protected $description = 'Wizard description';
function getSteps(): array
{
return [
'countries-visited' => new UniqueMultipleChoiceStep("Which countries did you visit?", [
'Serbia',
'UK',
'Hungary',
'Austria',
'Canada',
'France'
]),
'cities-visited' => new UniqueMultipleChoiceStep("Which cities did you visit?", [
'Belgrade',
'Nis',
'London',
'Manchester',
'Budapest',
'Szeged',
'Vienna',
'Salzburg',
'Ottawa',
'Toronto',
'Paris',
'Bordeaux',
], [
'end_keyword' => "That's all",
'retain_end_keyword' => true,
]),
];
}
function completed()
{
$countries = $this->answers->get('countries-visited');
$cities = $this->answers->get('cities-visited');
}
}
The example above would produce the following output
shomisha:laravel-console-wizard shomisha$ php artisan wizard:test
Which countries did you visit?:
[0] Serbia
[1] UK
[2] Hungary
[3] Austria
[4] Canada
[5] France
[6] Done
> 0
Which countries did you visit?:
[1] UK
[2] Hungary
[3] Austria
[4] Canada
[5] France
[6] Done
> 1
Which countries did you visit?:
[2] Hungary
[3] Austria
[4] Canada
[5] France
[6] Done
> 3
Which countries did you visit?:
[2] Hungary
[4] Canada
[5] France
[6] Done
> 6
Which cities did you visit?:
[0 ] Belgrade
[1 ] Nis
[2 ] London
[3 ] Manchester
[4 ] Budapest
[5 ] Szeged
[6 ] Vienna
[7 ] Salzburg
[8 ] Ottawa
[9 ] Toronto
[10] Paris
[11] Bordeaux
[12] That's all
> 0
Which cities did you visit?:
[1 ] Nis
[2 ] London
[3 ] Manchester
[4 ] Budapest
[5 ] Szeged
[6 ] Vienna
[7 ] Salzburg
[8 ] Ottawa
[9 ] Toronto
[10] Paris
[11] Bordeaux
[12] That's all
> 1
Which cities did you visit?:
[2 ] London
[3 ] Manchester
[4 ] Budapest
[5 ] Szeged
[6 ] Vienna
[7 ] Salzburg
[8 ] Ottawa
[9 ] Toronto
[10] Paris
[11] Bordeaux
[12] That's all
> 2
Which cities did you visit?:
[3 ] Manchester
[4 ] Budapest
[5 ] Szeged
[6 ] Vienna
[7 ] Salzburg
[8 ] Ottawa
[9 ] Toronto
[10] Paris
[11] Bordeaux
[12] That's all
> 6
Which cities did you visit?:
[3 ] Manchester
[4 ] Budapest
[5 ] Szeged
[7 ] Salzburg
[8 ] Ottawa
[9 ] Toronto
[10] Paris
[11] Bordeaux
[12] That's all
> 10
Which cities did you visit?:
[3 ] Manchester
[4 ] Budapest
[5 ] Szeged
[7 ] Salzburg
[8 ] Ottawa
[9 ] Toronto
[11] Bordeaux
[12] That's all
> 12
The UniqueMultipleChoiceStep
returns an array of strings selected as its answers. In the example above, $this->answers->get('countries-visited');
would return this array ['Serbia', 'UK', 'Austria']
and $this->answers->get('cities-visited');
would return ['Belgrade', 'Nis', 'London', 'Vienna', 'Paris', 'That\'s all']
. Again, the end keyword is present in the cities answer because the retain_end_keyword
option was set to true when instantiating that step.
Notice how with each selection made that choice is no longer available in the next iteration. By removing a choice as soon as the user selects is the Wizard ensures that no choice is selected more than once.
ConfirmStep
The ConfirmStep
is a simple confirmation prompt. Instantiating it requires merely passing in the string for the user to be asked. In addition, you may pass a boolean as the second argument which will determine the default answer for this step, true symbolizing confirmation and false the opposite.
<?php
namespace App\Console\Commands;
use Shomisha\LaravelConsoleWizard\Command\Wizard;
use Shomisha\LaravelConsoleWizard\Steps\ConfirmStep;
class WizardTest extends Wizard
{
protected $signature = 'wizard:test';
protected $description = 'Wizard description';
function getSteps(): array
{
return [
'is-developer' => new ConfirmStep("Are you a developer?", true),
'is-backend-developer' => new ConfirmStep("Are you a backend developer?", false),
];
}
function completed()
{
$isDeveloper = $this->answers->get('is-developer');
$isBackendDeveloper = $this->answers->get('is-backend-developer');
}
}
The example above would produce the following output
shomisha:laravel-console-wizard shomisha$ php artisan wizard:test
Are you a developer? (yes/no) [yes]:
> yes
Are you a backend developer? (yes/no) [no]:
> yes
As answers, the ConfirmStep
returns booleans. $this->answers->get('is-developer');
would return true
, as would $this->answers->get('is-backend-developer');
. If the users inputs anything other than yes
when prompted false
would be returned.