Skip to content

Commit d31823a

Browse files
committed
introduce enums for meals and foods
1 parent be80b06 commit d31823a

File tree

4 files changed

+104
-42
lines changed

4 files changed

+104
-42
lines changed

ux.symfony.com/src/Enum/Food.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace App\Enum;
4+
5+
enum Food: string
6+
{
7+
case Eggs = 'eggs';
8+
case Bacon = 'bacon';
9+
case Strawberries = 'strawberries';
10+
case Croissant = 'croissant';
11+
case Bagel = 'bagel';
12+
case Kiwi = 'kiwi';
13+
case Avocado = 'avocado';
14+
case Waffles = 'waffles';
15+
case Pancakes = 'pancakes';
16+
case Salad = 'salad';
17+
case Tea = 'tea️';
18+
case Sandwich = 'sandwich';
19+
case Cheese = 'cheese';
20+
case Sushi = 'sushi';
21+
case Pizza = 'pizza';
22+
case Pint = 'pint🍺';
23+
case Pasta = 'pasta';
24+
25+
public function getReadable(): string
26+
{
27+
return match ($this) {
28+
self::Eggs => 'Eggs 🍳',
29+
self::Bacon => 'Bacon 🥓',
30+
self::Strawberries => 'Strawberries 🍓',
31+
self::Croissant => 'Croissant 🥐',
32+
self::Bagel => 'Bagel 🥯',
33+
self::Kiwi => 'Kiwi 🥝',
34+
self::Avocado => 'Avocado 🥑',
35+
self::Waffles => 'Waffles 🧇',
36+
self::Pancakes => 'Pancakes 🥞',
37+
self::Salad => 'Salad 🥙',
38+
self::Tea => 'Tea ☕️',
39+
self::Sandwich => 'Sandwich 🥪',
40+
self::Cheese => 'Cheese 🧀',
41+
self::Sushi => 'Sushi 🍱',
42+
self::Pizza => 'Pizza 🍕',
43+
self::Pint => 'A Pint 🍺',
44+
self::Pasta => 'Pasta 🍝',
45+
};
46+
}
47+
}

ux.symfony.com/src/Enum/Meal.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Enum;
4+
5+
enum Meal: string
6+
{
7+
case Breakfast = 'breakfast';
8+
case SecondBreakfast = 'second_breakfast';
9+
case Elevenses = 'elevenses';
10+
case Lunch = 'lunch';
11+
case Dinner = 'dinner';
12+
13+
public function getReadable(): string
14+
{
15+
return match ($this) {
16+
self::Breakfast => 'Breakfast',
17+
self::SecondBreakfast => 'Second Breakfast',
18+
self::Elevenses => 'Elevenses',
19+
self::Lunch => 'Lunch',
20+
self::Dinner => 'Dinner',
21+
};
22+
}
23+
24+
/**
25+
* @return list<Food>
26+
*/
27+
public function getFoodChoices(): array
28+
{
29+
return match ($this) {
30+
self::Breakfast => [Food::Eggs, Food::Bacon, Food::Strawberries, Food::Croissant],
31+
self::SecondBreakfast => [Food::Bagel, Food::Kiwi, Food::Avocado, Food::Waffles],
32+
self::Elevenses => [Food::Pancakes, Food::Strawberries, Food::Tea],
33+
self::Lunch => [Food::Sandwich, Food::Cheese, Food::Sushi],
34+
self::Dinner => [Food::Pizza, Food::Pint, Food::Pasta],
35+
};
36+
}
37+
}

ux.symfony.com/src/Form/MealPlannerForm.php

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace App\Form;
44

5+
use App\Enum\Food;
6+
use App\Enum\Meal;
57
use App\Model\MealPlan;
68
use Symfony\Component\Form\AbstractType;
7-
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
9+
use Symfony\Component\Form\Extension\Core\Type\EnumType;
810
use Symfony\Component\Form\FormBuilderInterface;
911
use Symfony\Component\Form\FormEvent;
1012
use Symfony\Component\Form\FormEvents;
@@ -13,29 +15,16 @@
1315

1416
class MealPlannerForm extends AbstractType
1517
{
16-
public const MEAL_BREAKFAST = 'breakfast';
17-
public const MEAL_SECOND_BREAKFAST = 'second breakfast';
18-
public const MEAL_ELEVENSES = 'elevenses';
19-
public const MEAL_LUNCH = 'lunch';
20-
public const MEAL_DINNER = 'dinner';
21-
2218
/**
2319
* @var array<string, mixed>
2420
*/
2521
private $dependencies = [];
2622

2723
public function buildForm(FormBuilderInterface $builder, array $options)
2824
{
29-
$choices = [
30-
'Breakfast' => self::MEAL_BREAKFAST,
31-
'Second Breakfast' => self::MEAL_SECOND_BREAKFAST,
32-
'Elevenses' => self::MEAL_ELEVENSES,
33-
'Lunch' => self::MEAL_LUNCH,
34-
'Dinner' => self::MEAL_DINNER,
35-
];
36-
37-
$builder->add('meal', ChoiceType::class, [
38-
'choices' => $choices,
25+
$builder->add('meal', EnumType::class, [
26+
'class' => Meal::class,
27+
'choice_label' => fn (Meal $meal): string => $meal->getReadable(),
3928
'placeholder' => 'Which meal is it?',
4029
'autocomplete' => true,
4130
]);
@@ -73,26 +62,13 @@ public function onPostSubmitMeal(FormEvent $event): void
7362
);
7463
}
7564

76-
private function getAvailableFoodChoices(string $meal): array
65+
public function addFoodField(FormInterface $form, ?Meal $meal): void
7766
{
78-
$foods = match ($meal) {
79-
self::MEAL_BREAKFAST => ['Eggs 🍳', 'Bacon 🥓', 'Strawberries 🍓', 'Croissant 🥐'],
80-
self::MEAL_SECOND_BREAKFAST => ['Bagel 🥯', 'Kiwi 🥝', 'Avocado 🥑', 'Waffles 🧇'],
81-
self::MEAL_ELEVENSES => ['Pancakes 🥞', 'Salad 🥙', 'Tea ☕️'],
82-
self::MEAL_LUNCH => ['Sandwich 🥪', 'Cheese 🧀', 'Sushi 🍱'],
83-
self::MEAL_DINNER => ['Pizza 🍕', 'A Pint 🍺', 'Pasta 🍝'],
84-
};
85-
86-
return array_combine($foods, $foods);
87-
}
88-
89-
public function addFoodField(FormInterface $form, ?string $meal): void
90-
{
91-
$foodChoices = null === $meal ? [] : $this->getAvailableFoodChoices($meal);
92-
93-
$form->add('mainFood', ChoiceType::class, [
94-
'placeholder' => null === $meal ? 'Select a meal first' : sprintf('What\'s for %s?', $meal),
95-
'choices' => $foodChoices,
67+
$form->add('mainFood', EnumType::class, [
68+
'class' => Food::class,
69+
'placeholder' => null === $meal ? 'Select a meal first' : sprintf('What\'s for %s?', $meal->getReadable()),
70+
'choices' => $meal?->getFoodChoices(),
71+
'choice_label' => fn (Food $food): string => $food->getReadable(),
9672
'disabled' => null === $meal,
9773
// silence real-time "invalid" message when switching "meals"
9874
'invalid_message' => false,

ux.symfony.com/src/Model/MealPlan.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,34 @@
22

33
namespace App\Model;
44

5+
use App\Enum\Food;
6+
use App\Enum\Meal;
57
use Symfony\Component\Validator\Constraints\NotBlank;
68

79
class MealPlan
810
{
911
#[NotBlank]
10-
private ?string $meal = null;
12+
private ?Meal $meal = null;
1113

1214
#[NotBlank]
13-
private ?string $mainFood = null;
15+
private ?Food $mainFood = null;
1416

15-
public function getMeal(): ?string
17+
public function getMeal(): ?Meal
1618
{
1719
return $this->meal;
1820
}
1921

20-
public function setMeal(?string $meal): void
22+
public function setMeal(?Meal $meal): void
2123
{
2224
$this->meal = $meal;
2325
}
2426

25-
public function getMainFood(): ?string
27+
public function getMainFood(): ?Food
2628
{
2729
return $this->mainFood;
2830
}
2931

30-
public function setMainFood(?string $mainFood): void
32+
public function setMainFood(?Food $mainFood): void
3133
{
3234
$this->mainFood = $mainFood;
3335
}

0 commit comments

Comments
 (0)