69 lines
2.3 KiB
PHP
69 lines
2.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Dtos;
|
||
|
|
|
||
|
|
class RecipeNutritionDto
|
||
|
|
{
|
||
|
|
public ?int $id = null;
|
||
|
|
public ?int $calories = null;
|
||
|
|
public ?float $protein = null;
|
||
|
|
public ?float $carbohydrates = null;
|
||
|
|
public ?float $fat = null;
|
||
|
|
public ?float $saturatedFat = null;
|
||
|
|
public ?float $fiber = null;
|
||
|
|
public ?float $sugar = null;
|
||
|
|
public ?float $sodium = null;
|
||
|
|
public ?float $cholesterol = null;
|
||
|
|
public ?int $createdAt = null;
|
||
|
|
public ?int $updatedAt = null;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create DTO from array
|
||
|
|
*
|
||
|
|
* @param array $data
|
||
|
|
* @return static
|
||
|
|
*/
|
||
|
|
public static function fromArray(array $data): static
|
||
|
|
{
|
||
|
|
$dto = new static();
|
||
|
|
|
||
|
|
$dto->id = $data['id'] ?? null;
|
||
|
|
$dto->calories = isset($data['calories']) ? (int)$data['calories'] : null;
|
||
|
|
$dto->protein = isset($data['protein']) ? (float)$data['protein'] : null;
|
||
|
|
$dto->carbohydrates = isset($data['carbohydrates']) ? (float)$data['carbohydrates'] : null;
|
||
|
|
$dto->fat = isset($data['fat']) ? (float)$data['fat'] : null;
|
||
|
|
$dto->saturatedFat = isset($data['saturatedFat']) ? (float)$data['saturatedFat'] : null;
|
||
|
|
$dto->fiber = isset($data['fiber']) ? (float)$data['fiber'] : null;
|
||
|
|
$dto->sugar = isset($data['sugar']) ? (float)$data['sugar'] : null;
|
||
|
|
$dto->sodium = isset($data['sodium']) ? (float)$data['sodium'] : null;
|
||
|
|
$dto->cholesterol = isset($data['cholesterol']) ? (float)$data['cholesterol'] : null;
|
||
|
|
$dto->createdAt = isset($data['createdAt']) ? (int)$data['createdAt'] : null;
|
||
|
|
$dto->updatedAt = isset($data['updatedAt']) ? (int)$data['updatedAt'] : null;
|
||
|
|
|
||
|
|
return $dto;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Convert DTO to array
|
||
|
|
*
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function toArray(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'id' => $this->id,
|
||
|
|
'calories' => $this->calories,
|
||
|
|
'protein' => $this->protein,
|
||
|
|
'carbohydrates' => $this->carbohydrates,
|
||
|
|
'fat' => $this->fat,
|
||
|
|
'saturatedFat' => $this->saturatedFat,
|
||
|
|
'fiber' => $this->fiber,
|
||
|
|
'sugar' => $this->sugar,
|
||
|
|
'sodium' => $this->sodium,
|
||
|
|
'cholesterol' => $this->cholesterol,
|
||
|
|
'createdAt' => $this->createdAt,
|
||
|
|
'updatedAt' => $this->updatedAt,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|