|
4 | 4 |
|
5 | 5 | namespace Kami\Cocktail\OpenAPI\Schemas;
|
6 | 6 |
|
| 7 | +use Illuminate\Http\Request; |
7 | 8 | use OpenApi\Attributes as OAT;
|
8 |
| -use Kami\Cocktail\Models\Enums\MenuItemTypeEnum; |
9 | 9 |
|
10 | 10 | #[OAT\Schema(required: ['is_enabled', 'items'])]
|
11 | 11 | class MenuRequest
|
12 | 12 | {
|
13 |
| - #[OAT\Property(property: 'is_enabled')] |
14 |
| - public bool $isEnabled = false; |
15 |
| - /** @var array<mixed> */ |
16 |
| - #[OAT\Property(type: 'array', items: new OAT\Items(type: 'object', properties: [ |
17 |
| - new OAT\Property(type: 'integer', property: 'id', example: 1), |
18 |
| - new OAT\Property(type: 'schema', property: 'type', ref: MenuItemTypeEnum::class), |
19 |
| - new OAT\Property(type: 'string', property: 'category_name', example: 'Category name'), |
20 |
| - new OAT\Property(type: 'integer', property: 'sort', example: 1), |
21 |
| - new OAT\Property(type: 'integer', property: 'price', example: 2252, format: 'minor'), |
22 |
| - new OAT\Property(type: 'string', property: 'currency', example: 'EUR', format: 'ISO 4217'), |
23 |
| - ], required: ['id', 'type', 'category_name', 'sort', 'price', 'currency'])), |
24 |
| - ] |
25 |
| - public array $items = []; |
| 13 | + /** |
| 14 | + * @param array<MenuItemRequest> $items |
| 15 | + */ |
| 16 | + public function __construct( |
| 17 | + #[OAT\Property(property: 'is_enabled')] |
| 18 | + public bool $isEnabled = false, |
| 19 | + #[OAT\Property(items: new OAT\Items(type: MenuItemRequest::class))] |
| 20 | + public array $items = [], |
| 21 | + ) { |
| 22 | + } |
| 23 | + |
| 24 | + public static function fromIlluminateRequest(Request $request): self |
| 25 | + { |
| 26 | + /** @var array<mixed> */ |
| 27 | + $formItems = $request->post('items', []); |
| 28 | + |
| 29 | + $items = []; |
| 30 | + foreach ($formItems as $formItem) { |
| 31 | + $items[] = MenuItemRequest::fromArray($formItem); |
| 32 | + } |
| 33 | + |
| 34 | + return new self( |
| 35 | + isEnabled: $request->boolean('is_enabled', false), |
| 36 | + items: $items, |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @return array<MenuItemRequest> |
| 42 | + */ |
| 43 | + public function getIngredients(): array |
| 44 | + { |
| 45 | + return array_filter($this->items, fn (MenuItemRequest $item) => $item->type === \Kami\Cocktail\Models\Enums\MenuItemTypeEnum::Ingredient); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return array<MenuItemRequest> |
| 50 | + */ |
| 51 | + public function getCocktails(): array |
| 52 | + { |
| 53 | + return array_filter($this->items, fn (MenuItemRequest $item) => $item->type === \Kami\Cocktail\Models\Enums\MenuItemTypeEnum::Cocktail); |
| 54 | + } |
26 | 55 | }
|
0 commit comments