src/Entity/Recipe/Ingredient.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Recipe;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use App\Entity\Operation\Operation;
  7. use App\Repository\Recipe\IngredientRepository;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Gedmo\Timestampable\Traits\TimestampableEntity;
  12. use Symfony\Component\Serializer\Annotation\Groups;
  13. /**
  14.  * @ORM\Entity(repositoryClass=IngredientRepository::class)
  15.  * @ApiResource(
  16.  *     attributes={
  17.  *          "normalization_context"={"groups"={
  18.  *              "Recipe:io",
  19.  *          }},
  20.  *          "denormalization_context"={"groups"={
  21.  *              "Recipe:io",
  22.  *          }}
  23.  *      },
  24.  *     collectionOperations={
  25.  *          "public-get-ingredients"={
  26.  *              "path"="/public/ingredients",
  27.  *              "method"="GET",
  28.  *          },
  29.  *     },
  30.  * ),
  31.  */
  32. class Ingredient
  33. {
  34.     use TimestampableEntity;
  35.     /**
  36.      * @ORM\Id
  37.      * @ORM\GeneratedValue
  38.      * @ORM\Column(type="integer")
  39.      * @Groups({
  40.      *     "Recipe:io",
  41.      * })
  42.      */
  43.     private $id;
  44.     /**
  45.      * @ORM\Column(type="string", length=255)
  46.      * @Groups({
  47.      *     "Recipe:io",
  48.      * })
  49.      */
  50.     private $name;
  51.     /**
  52.      * @ORM\Column(type="boolean")
  53.      * @Groups({
  54.      *     "Recipe:io",
  55.      * })
  56.      * @ApiFilter(BooleanFilter::class)
  57.      */
  58.     private $brand;
  59.     /**
  60.      * @ORM\OneToMany(targetEntity=RecipeIngredient::class, mappedBy="ingredient")
  61.      */
  62.     private $recipeIngredients;
  63.     /**
  64.      * @ORM\ManyToMany(targetEntity=Operation::class, inversedBy="ingredients")
  65.      * @Groups({
  66.      *     "Recipe:io",
  67.      * })
  68.      */
  69.     private $offers;
  70.     public function __construct()
  71.     {
  72.         $this->recipeIngredients = new ArrayCollection();
  73.         $this->offers = new ArrayCollection();
  74.     }
  75.     public function getId(): ?int
  76.     {
  77.         return $this->id;
  78.     }
  79.     public function getName(): ?string
  80.     {
  81.         return $this->name;
  82.     }
  83.     public function setName(string $name): self
  84.     {
  85.         $this->name $name;
  86.         return $this;
  87.     }
  88.     public function getBrand(): ?bool
  89.     {
  90.         return $this->brand;
  91.     }
  92.     public function setBrand(bool $brand): self
  93.     {
  94.         $this->brand $brand;
  95.         return $this;
  96.     }
  97.     /**
  98.      * @return Collection<int, RecipeIngredient>
  99.      */
  100.     public function getRecipeIngredients(): Collection
  101.     {
  102.         return $this->recipeIngredients;
  103.     }
  104.     public function addRecipeIngredient(RecipeIngredient $recipeIngredient): self
  105.     {
  106.         if (!$this->recipeIngredients->contains($recipeIngredient)) {
  107.             $this->recipeIngredients[] = $recipeIngredient;
  108.             $recipeIngredient->setIngredient($this);
  109.         }
  110.         return $this;
  111.     }
  112.     public function removeRecipeIngredient(RecipeIngredient $recipeIngredient): self
  113.     {
  114.         if ($this->recipeIngredients->removeElement($recipeIngredient)) {
  115.             // set the owning side to null (unless already changed)
  116.             if ($recipeIngredient->getIngredient() === $this) {
  117.                 $recipeIngredient->setIngredient(null);
  118.             }
  119.         }
  120.         return $this;
  121.     }
  122.     /**
  123.      * @return Collection<int, Operation>
  124.      */
  125.     public function getOffers(): Collection
  126.     {
  127.         return $this->offers;
  128.     }
  129.     public function addOffer(Operation $offer): self
  130.     {
  131.         if (!$this->offers->contains($offer)) {
  132.             $this->offers[] = $offer;
  133.         }
  134.         return $this;
  135.     }
  136.     public function removeOffer(Operation $offer): self
  137.     {
  138.         $this->offers->removeElement($offer);
  139.         return $this;
  140.     }
  141. }