<?php
namespace App\Entity\Recipe;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Controller\Api\Recipe\GetNumberOfRecipes;
use App\Entity\MediaObject\MediaRecipe;
use App\Filter\RecipeIngredientFilter;
use App\Filter\RecipeTypeFilter;
use App\Repository\Recipe\RecipeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
*
* @ORM\Entity(repositoryClass=RecipeRepository::class)
* @ApiResource(
* attributes={
* "pagination_client_enabled"=true,
* "pagination_client_items_per_page"=true,
* "order"={"createdAt": "DESC"},
* "normalization_context"={"groups"={
* "Recipe:io",
* }},
* "denormalization_context"={"groups"={
* "Recipe:io",
* }}
* },
* collectionOperations={
* "public-get-recipes"={
* "path"="/public/recipes",
* "method"="GET",
* },
* "public-get-number-recipes"={
* "path"="/public/recipes-number",
* "method"="GET",
* "controller"=GetNumberOfRecipes::class,
* },
* },
* itemOperations={
* "public-get-one-recipe"={
* "path"="/public/recipe/{id}",
* "method"="GET",
* },
* },
* )
* @ApiFilter(RecipeIngredientFilter::class, properties={
* "recipeIngredients.ingredient.name",
* })
* @ApiFilter(RecipeTypeFilter::class, properties={
* "recipeType.name",
* })
* @ApiFilter(SearchFilter::class, properties={
* "name": "partial",
* "recipeType.name": "exact",
* })
* @ApiFilter(OrderFilter::class, properties={
* "position"
* })
*/
class Recipe
{
use TimestampableEntity;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({
* "Recipe:io",
* })
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({
* "Recipe:io",
* })
*/
private string $name;
/**
* @ORM\Column(type="boolean")
* @Groups({
* "Recipe:io",
* })
* @ApiFilter(BooleanFilter::class)
*/
private bool $promote = false;
/**
* @ORM\Column(type="integer")
* @Groups({
* "Recipe:io",
* })
*/
private int $position = 0;
/**
* @ORM\ManyToMany(targetEntity=RecipeType::class, inversedBy="recipes")
* @ORM\JoinTable(name="recipe_recipe_type")
* @Groups({
* "Recipe:io",
* })
*/
private Collection $recipeType;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({
* "Recipe:io",
* })
*/
private ?string $description;
/**
* @ORM\Column(type="boolean")
* @Groups({
* "Recipe:io",
* })
* @ApiFilter(BooleanFilter::class)
*/
private bool $online;
/**
* @ORM\Column(type="datetime")
* @Groups({
* "Recipe:io",
* })
*/
private \DateTimeInterface $publicationDate;
/**
* @ORM\Column(type="time")
* @Groups({
* "Recipe:io",
* })
*/
private \DateTimeInterface $preparationTime;
/**
* @ORM\Column(type="time")
* @Groups({
* "Recipe:io",
* })
*/
private \DateTimeInterface $cookingTime;
/**
* @ORM\OneToOne(targetEntity=MediaRecipe::class, cascade={"persist", "remove"})
* @Groups({
* "Recipe:io",
* })
*/
private MediaRecipe $image;
/**
* @ORM\Column(type="integer")
* @Groups({
* "Recipe:io",
* })
*/
private int $portion;
/**
* @ORM\OneToMany(targetEntity=RecipeIngredient::class, mappedBy="recipe", cascade={"persist"}, orphanRemoval=true)
* @Assert\Count(min="1", minMessage="Une recette doit contenir au moins 1 ingrédient")
* @Groups({
* "Recipe:io",
* })
*/
private Collection $recipeIngredients;
/**
* @ORM\Column(type="json")
* @Assert\Count(min="1", minMessage="Une recette doit contenir au moins 1 étape")
* @Assert\All({
* @Assert\NotNull(message="Une étape ne peux pas être vide")
* })
* @Groups({
* "Recipe:io",
* })
*/
private array $steps = [];
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({
* "Recipe:io",
* })
*/
private ?string $introduction;
/**
* @ORM\Column(type="string", length=500, nullable=true)
* @Groups({
* "Recipe:io",
* })
*/
private ?string $tricks;
public function __construct()
{
$this->recipeType = new ArrayCollection();
$this->recipeIngredients = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getPromote(): ?bool
{
return $this->promote;
}
public function setPromote(bool $promote): self
{
$this->promote = $promote;
return $this;
}
/**
* @return Collection<int, RecipeType>
*/
public function getRecipeType(): Collection
{
return $this->recipeType;
}
public function addRecipeType(RecipeType $recipeType): self
{
if (!$this->recipeType->contains($recipeType)) {
$this->recipeType[] = $recipeType;
}
return $this;
}
public function removeRecipeType(RecipeType $recipeType): self
{
$this->recipeType->removeElement($recipeType);
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getOnline(): ?bool
{
return $this->online;
}
public function setOnline(bool $online): self
{
$this->online = $online;
return $this;
}
public function getPublicationDate(): ?\DateTimeInterface
{
return $this->publicationDate;
}
public function setPublicationDate(\DateTimeInterface $publicationDate): self
{
$this->publicationDate = $publicationDate;
return $this;
}
public function getPreparationTime(): ?\DateTimeInterface
{
return $this->preparationTime;
}
public function setPreparationTime(\DateTimeInterface $preparationTime): self
{
$this->preparationTime = $preparationTime;
return $this;
}
public function getCookingTime(): ?\DateTimeInterface
{
return $this->cookingTime;
}
public function setCookingTime(\DateTimeInterface $cookingTime): self
{
$this->cookingTime = $cookingTime;
return $this;
}
public function getPortion(): ?int
{
return $this->portion;
}
public function setPortion(int $portion): self
{
$this->portion = $portion;
return $this;
}
public function getImage(): MediaRecipe
{
return $this->image;
}
public function setImage(MediaRecipe $image): Recipe
{
$this->image = $image;
return $this;
}
/**
* @return Collection<int, RecipeIngredient>
*/
public function getRecipeIngredients(): Collection
{
return $this->recipeIngredients;
}
public function addRecipeIngredient(RecipeIngredient $recipeIngredient): self
{
if (!$this->recipeIngredients->contains($recipeIngredient)) {
$this->recipeIngredients[] = $recipeIngredient;
$recipeIngredient->setRecipe($this);
}
return $this;
}
public function removeRecipeIngredient(RecipeIngredient $recipeIngredient): self
{
if ($this->recipeIngredients->removeElement($recipeIngredient)) {
// set the owning side to null (unless already changed)
if ($recipeIngredient->getRecipe() === $this) {
$recipeIngredient->setRecipe(null);
}
}
return $this;
}
public function getSteps(): ?array
{
return $this->steps;
}
public function setSteps(array $steps): self
{
$this->steps = $steps;
return $this;
}
public function getIntroduction(): ?string
{
return $this->introduction;
}
public function setIntroduction(?string $introduction): self
{
$this->introduction = $introduction;
return $this;
}
public function getTricks(): ?string
{
return $this->tricks;
}
public function setTricks(?string $tricks): self
{
$this->tricks = $tricks;
return $this;
}
public function getPosition(): int
{
return $this->position;
}
public function setPosition(int $position): Recipe
{
$this->position = $position;
return $this;
}
}