src/Entity/Recipe/Recipe.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Recipe;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  8. use App\Controller\Api\Recipe\GetNumberOfRecipes;
  9. use App\Entity\MediaObject\MediaRecipe;
  10. use App\Filter\RecipeIngredientFilter;
  11. use App\Filter\RecipeTypeFilter;
  12. use App\Repository\Recipe\RecipeRepository;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Gedmo\Timestampable\Traits\TimestampableEntity;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. use Symfony\Component\Validator\Constraints as Assert;
  19. /**
  20.  *
  21.  * @ORM\Entity(repositoryClass=RecipeRepository::class)
  22.  * @ApiResource(
  23.  *     attributes={
  24.  *          "pagination_client_enabled"=true,
  25.  *          "pagination_client_items_per_page"=true,
  26.  *          "order"={"createdAt": "DESC"},
  27.  *          "normalization_context"={"groups"={
  28.  *              "Recipe:io",
  29.  *          }},
  30.  *          "denormalization_context"={"groups"={
  31.  *              "Recipe:io",
  32.  *          }}
  33.  *      },
  34.  *     collectionOperations={
  35.  *          "public-get-recipes"={
  36.  *              "path"="/public/recipes",
  37.  *              "method"="GET",
  38.  *          },
  39.  *          "public-get-number-recipes"={
  40.  *              "path"="/public/recipes-number",
  41.  *              "method"="GET",
  42.  *              "controller"=GetNumberOfRecipes::class,
  43.  *          },
  44.  *     },
  45.  *     itemOperations={
  46.  *          "public-get-one-recipe"={
  47.  *              "path"="/public/recipe/{id}",
  48.  *              "method"="GET",
  49.  *          },
  50.  *     },
  51.  * )
  52.  * @ApiFilter(RecipeIngredientFilter::class, properties={
  53.  *     "recipeIngredients.ingredient.name",
  54.  * })
  55.  * @ApiFilter(RecipeTypeFilter::class, properties={
  56.  *     "recipeType.name",
  57.  * })
  58.  * @ApiFilter(SearchFilter::class, properties={
  59.  *     "name": "partial",
  60.  *     "recipeType.name": "exact",
  61.  * })
  62.  * @ApiFilter(OrderFilter::class, properties={
  63.  *     "position"
  64.  * })
  65.  */
  66. class Recipe
  67. {
  68.     use TimestampableEntity;
  69.     /**
  70.      * @ORM\Id
  71.      * @ORM\GeneratedValue
  72.      * @ORM\Column(type="integer")
  73.      * @Groups({
  74.      *     "Recipe:io",
  75.      * })
  76.      */
  77.     private $id;
  78.     /**
  79.      * @ORM\Column(type="string", length=255)
  80.      * @Groups({
  81.      *     "Recipe:io",
  82.      * })
  83.      */
  84.     private string $name;
  85.     /**
  86.      * @ORM\Column(type="boolean")
  87.      * @Groups({
  88.      *     "Recipe:io",
  89.      * })
  90.      * @ApiFilter(BooleanFilter::class)
  91.      */
  92.     private bool $promote false;
  93.     /**
  94.      * @ORM\Column(type="integer")
  95.      * @Groups({
  96.      *     "Recipe:io",
  97.      * })
  98.      */
  99.     private int $position 0;
  100.     /**
  101.      * @ORM\ManyToMany(targetEntity=RecipeType::class, inversedBy="recipes")
  102.      * @ORM\JoinTable(name="recipe_recipe_type")
  103.      * @Groups({
  104.      *     "Recipe:io",
  105.      * })
  106.      */
  107.     private Collection $recipeType;
  108.     /**
  109.      * @ORM\Column(type="string", length=255, nullable=true)
  110.      * @Groups({
  111.      *     "Recipe:io",
  112.      * })
  113.      */
  114.     private ?string $description;
  115.     /**
  116.      * @ORM\Column(type="boolean")
  117.      * @Groups({
  118.      *     "Recipe:io",
  119.      * })
  120.      * @ApiFilter(BooleanFilter::class)
  121.      */
  122.     private bool $online;
  123.     /**
  124.      * @ORM\Column(type="datetime")
  125.      * @Groups({
  126.      *     "Recipe:io",
  127.      * })
  128.      */
  129.     private \DateTimeInterface $publicationDate;
  130.     /**
  131.      * @ORM\Column(type="time")
  132.      * @Groups({
  133.      *     "Recipe:io",
  134.      * })
  135.      */
  136.     private \DateTimeInterface $preparationTime;
  137.     /**
  138.      * @ORM\Column(type="time")
  139.      * @Groups({
  140.      *     "Recipe:io",
  141.      * })
  142.      */
  143.     private \DateTimeInterface $cookingTime;
  144.     /**
  145.      * @ORM\OneToOne(targetEntity=MediaRecipe::class, cascade={"persist", "remove"})
  146.      * @Groups({
  147.      *     "Recipe:io",
  148.      * })
  149.      */
  150.     private MediaRecipe $image;
  151.     /**
  152.      * @ORM\Column(type="integer")
  153.      * @Groups({
  154.      *     "Recipe:io",
  155.      * })
  156.      */
  157.     private int $portion;
  158.     /**
  159.      * @ORM\OneToMany(targetEntity=RecipeIngredient::class, mappedBy="recipe", cascade={"persist"}, orphanRemoval=true)
  160.      * @Assert\Count(min="1", minMessage="Une recette doit contenir au moins 1 ingrédient")
  161.      * @Groups({
  162.      *     "Recipe:io",
  163.      * })
  164.      */
  165.     private Collection $recipeIngredients;
  166.     /**
  167.      * @ORM\Column(type="json")
  168.      * @Assert\Count(min="1", minMessage="Une recette doit contenir au moins 1 étape")
  169.      * @Assert\All({
  170.      *      @Assert\NotNull(message="Une étape ne peux pas être vide")
  171.      * })
  172.      * @Groups({
  173.      *     "Recipe:io",
  174.      * })
  175.      */
  176.     private array $steps = [];
  177.     /**
  178.      * @ORM\Column(type="string", length=255, nullable=true)
  179.      * @Groups({
  180.      *     "Recipe:io",
  181.      * })
  182.      */
  183.     private ?string $introduction;
  184.     /**
  185.      * @ORM\Column(type="string", length=500, nullable=true)
  186.      * @Groups({
  187.      *     "Recipe:io",
  188.      * })
  189.      */
  190.     private ?string $tricks;
  191.     public function __construct()
  192.     {
  193.         $this->recipeType = new ArrayCollection();
  194.         $this->recipeIngredients = new ArrayCollection();
  195.     }
  196.     public function getId(): ?int
  197.     {
  198.         return $this->id;
  199.     }
  200.     public function getName(): ?string
  201.     {
  202.         return $this->name;
  203.     }
  204.     public function setName(string $name): self
  205.     {
  206.         $this->name $name;
  207.         return $this;
  208.     }
  209.     public function getPromote(): ?bool
  210.     {
  211.         return $this->promote;
  212.     }
  213.     public function setPromote(bool $promote): self
  214.     {
  215.         $this->promote $promote;
  216.         return $this;
  217.     }
  218.     /**
  219.      * @return Collection<int, RecipeType>
  220.      */
  221.     public function getRecipeType(): Collection
  222.     {
  223.         return $this->recipeType;
  224.     }
  225.     public function addRecipeType(RecipeType $recipeType): self
  226.     {
  227.         if (!$this->recipeType->contains($recipeType)) {
  228.             $this->recipeType[] = $recipeType;
  229.         }
  230.         return $this;
  231.     }
  232.     public function removeRecipeType(RecipeType $recipeType): self
  233.     {
  234.         $this->recipeType->removeElement($recipeType);
  235.         return $this;
  236.     }
  237.     public function getDescription(): ?string
  238.     {
  239.         return $this->description;
  240.     }
  241.     public function setDescription(?string $description): self
  242.     {
  243.         $this->description $description;
  244.         return $this;
  245.     }
  246.     public function getOnline(): ?bool
  247.     {
  248.         return $this->online;
  249.     }
  250.     public function setOnline(bool $online): self
  251.     {
  252.         $this->online $online;
  253.         return $this;
  254.     }
  255.     public function getPublicationDate(): ?\DateTimeInterface
  256.     {
  257.         return $this->publicationDate;
  258.     }
  259.     public function setPublicationDate(\DateTimeInterface $publicationDate): self
  260.     {
  261.         $this->publicationDate $publicationDate;
  262.         return $this;
  263.     }
  264.     public function getPreparationTime(): ?\DateTimeInterface
  265.     {
  266.         return $this->preparationTime;
  267.     }
  268.     public function setPreparationTime(\DateTimeInterface $preparationTime): self
  269.     {
  270.         $this->preparationTime $preparationTime;
  271.         return $this;
  272.     }
  273.     public function getCookingTime(): ?\DateTimeInterface
  274.     {
  275.         return $this->cookingTime;
  276.     }
  277.     public function setCookingTime(\DateTimeInterface $cookingTime): self
  278.     {
  279.         $this->cookingTime $cookingTime;
  280.         return $this;
  281.     }
  282.     public function getPortion(): ?int
  283.     {
  284.         return $this->portion;
  285.     }
  286.     public function setPortion(int $portion): self
  287.     {
  288.         $this->portion $portion;
  289.         return $this;
  290.     }
  291.     public function getImage(): MediaRecipe
  292.     {
  293.         return $this->image;
  294.     }
  295.     public function setImage(MediaRecipe $image): Recipe
  296.     {
  297.         $this->image $image;
  298.         return $this;
  299.     }
  300.     /**
  301.      * @return Collection<int, RecipeIngredient>
  302.      */
  303.     public function getRecipeIngredients(): Collection
  304.     {
  305.         return $this->recipeIngredients;
  306.     }
  307.     public function addRecipeIngredient(RecipeIngredient $recipeIngredient): self
  308.     {
  309.         if (!$this->recipeIngredients->contains($recipeIngredient)) {
  310.             $this->recipeIngredients[] = $recipeIngredient;
  311.             $recipeIngredient->setRecipe($this);
  312.         }
  313.         return $this;
  314.     }
  315.     public function removeRecipeIngredient(RecipeIngredient $recipeIngredient): self
  316.     {
  317.         if ($this->recipeIngredients->removeElement($recipeIngredient)) {
  318.             // set the owning side to null (unless already changed)
  319.             if ($recipeIngredient->getRecipe() === $this) {
  320.                 $recipeIngredient->setRecipe(null);
  321.             }
  322.         }
  323.         return $this;
  324.     }
  325.     public function getSteps(): ?array
  326.     {
  327.         return $this->steps;
  328.     }
  329.     public function setSteps(array $steps): self
  330.     {
  331.         $this->steps $steps;
  332.         return $this;
  333.     }
  334.     public function getIntroduction(): ?string
  335.     {
  336.         return $this->introduction;
  337.     }
  338.     public function setIntroduction(?string $introduction): self
  339.     {
  340.         $this->introduction $introduction;
  341.         return $this;
  342.     }
  343.     public function getTricks(): ?string
  344.     {
  345.         return $this->tricks;
  346.     }
  347.     public function setTricks(?string $tricks): self
  348.     {
  349.         $this->tricks $tricks;
  350.         return $this;
  351.     }
  352.     public function getPosition(): int
  353.     {
  354.         return $this->position;
  355.     }
  356.     public function setPosition(int $position): Recipe
  357.     {
  358.         $this->position $position;
  359.         return $this;
  360.     }
  361. }