ldc-legaulois/app/Http/Resources/Fo/Recipes/RecipeCommericalSheetResource.php

86 lines
3.1 KiB
PHP

<?php
namespace App\Http\Resources\Fo\Recipes;
use App\Models\Recipe;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class RecipeCommericalSheetResource extends JsonResource
{
/** @var \App\Models\Recipe */
private $recipeModel;
/** @var array */
private static $skeleton = [
'id' => '',
'slug' => '',
'title' => '',
'tips' => '',
'picture' => '',
'picture_alt' => '',
'ingredients_list' => [],
'preparation' => '',
'advice' => '',
'video_id' => null,
'consumption_moments' => [],
'preparation_time' => '',
'cooking_time' => '',
'nb_guests' => 0,
'difficulty' => '',
'highlight' => false,
'published' => false,
];
/**
* RecipeResource
*
* @return void
*/
public function __construct(Recipe $recipeModel)
{
$this->recipeModel = $recipeModel;
}
/**
* Transform the resource into an array.
*/
public function toArray(?Request $request = null): array
{
return array_intersect_key(
[
'id' => $this->recipeModel->getKey(),
'slug' => $this->recipeModel->slug,
'title' => $this->recipeModel->title,
'tips' => $this->recipeModel->tips,
'picture' => $this->recipeModel->picture,
'picture_alt' => $this->recipeModel->picture_alt,
'ingredients_list' => $this->recipeModel->ingredients_list,
'preparation' => $this->recipeModel->preparation,
'advice' => $this->recipeModel->advice,
'video_id' => (! is_null($this->recipeModel->video)) ?
(new VideoResource($this->recipeModel->video))->toArray($request)
: null,
'consumption_moments' => $this->recipeModel->consumptionMoments
->map(fn($moment) => (new ConsumptionMomentResource($moment))->toArray($request))
->values()
->all(),
'preparation_time' => $this->recipeModel->preparation_time,
'cooking_time' => $this->recipeModel->cooking_time,
'nb_guests' => $this->recipeModel->nb_guests,
'difficulty' => $this->recipeModel->difficulty->label(),
'highlight' => $this->recipeModel->highlight,
'published' => $this->recipeModel->published,
],
$this->getSkeleton()
);
}
/**
* Returns the resource skeletton
*/
public static function getSkeleton(): array
{
return self::$skeleton;
}
}