Bruna's command

Exercise 2 of 6 in chapter 11 · 270 XP · 5 objectives checked by tests

Topics covered: #[Argument]#[Option]Boolean optionsValidatorInterfaceCommand::INVALID

Bruna invents her dishes late at night, when the dining room is closed. Rather than opening the kitchen form, she would like to add them in a single line:

app:plat:ajouter "Shire Ale" 3.5 --categorie Drinks --specialite

(app:plat:ajouter = add a dish; --categorie = category, --specialite = specialty.)

Arguments and options

In an invokable command, both are declared as parameters of __invoke(), with their type: Symfony converts 4.5 into a float, and --help documents them.

The same rules everywhere

A dish added from the command line must follow the same rules as a dish added through the form: a unique name, a positive price, a real description. These rules already live on the Plat entity (dish): the Validator applies them to any object, without a form.

Your mission

Write app:plat:ajouter:

  1. two arguments, the name (nom) and the price (prix), and the --categorie option: the dish is saved, then the command shows the address of its page (/dish/shire-ale);
  2. --specialite makes it a specialty; --description gives its description (a default description otherwise);
  3. an invalid dish (a price of 0, a description that is too short…) is rejected with the entity's messages, and the code Command::INVALID;
  4. a dish already on the menu is rejected the same way;
  5. without --categorie: Command::INVALID; with an unknown category: Command::FAILURE. In both cases, nothing is saved.

Try it in the Console tab, then check with app:inventaire. A value that contains spaces goes between quotes: --categorie "Main courses".