src/Entity/Product.php line 16

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ProductRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use Symfony\Component\Serializer\Annotation\Groups;
  10. use Symfony\Component\Uid\Uuid;
  11. #[ORM\Entity(repositoryClassProductRepository::class)]
  12. // #[UniqueEntity(['whitelabel', 'name'], message: 'Record with this name already exists.')]
  13. class Product
  14. {
  15.     #[ORM\Id]
  16.     #[ORM\Column(type'uuid'uniquetrue)]
  17.     #[Groups(['BASE'])]
  18.     private ?string $id null;
  19.     #[ORM\Column(length255)]
  20.     private ?string $name null;
  21.     #[ORM\Column(length255nullabletrue)]
  22.     private ?string $nameEn null;
  23.     /**
  24.      * @phpstan-ignore-next-line
  25.      */
  26.     #[ORM\Column(typeTypes::DECIMALprecision10scale'2')]
  27.     private ?float $price null;
  28.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  29.     private ?\DateTimeInterface $createDate null;
  30.     #[ORM\ManyToOne(inversedBy'products')]
  31.     #[ORM\JoinColumn(nullablefalse)]
  32.     private ?Whitelabel $whitelabel null;
  33.     #[ORM\ManyToOne(inversedBy'products')]
  34.     #[ORM\JoinColumn(nullablefalse)]
  35.     private ?User $creatorUser null;
  36.     #[ORM\ManyToMany(targetEntityComponent::class, inversedBy'products')]
  37.     private Collection $components;
  38.     #[ORM\ManyToMany(targetEntityMenu::class, mappedBy'products')]
  39.     private Collection $menus;
  40.     #[ORM\Column(nullabletrue)]
  41.     private ?int $weight null;
  42.     #[ORM\ManyToOne(inversedBy'products')]
  43.     #[ORM\JoinColumn(nullabletrue)]
  44.     private ?ProductCategory $category null;
  45.     #[ORM\ManyToMany(targetEntityAllergen::class, inversedBy'products')]
  46.     private Collection $allergens;
  47.     public function __construct()
  48.     {
  49.         $this->id Uuid::v4();
  50.         $this->components = new ArrayCollection();
  51.         $this->menus = new ArrayCollection();
  52.         $this->allergens = new ArrayCollection();
  53.     }
  54.     public function __toString()
  55.     {
  56.         $name $this->getName();
  57.         if (!empty($this->getCategory())) {
  58.             $name $name.' - '.$this->getCategory()->getName();
  59.         }
  60.         $name $name.' ('.$this->getPrice().'лв.)';
  61.         return $name;
  62.     }
  63.     public function getId(): ?string
  64.     {
  65.         return $this->id;
  66.     }
  67.     public function getName(): ?string
  68.     {
  69.         return $this->name;
  70.     }
  71.     public function setName(string $name): self
  72.     {
  73.         $this->name $name;
  74.         return $this;
  75.     }
  76.     public function getNameEn(): ?string
  77.     {
  78.         return $this->nameEn;
  79.     }
  80.     public function setNameEn(?string $nameEn): self
  81.     {
  82.         $this->nameEn $nameEn;
  83.         return $this;
  84.     }
  85.     public function getPrice(): ?float
  86.     {
  87.         return $this->price;
  88.     }
  89.     public function getFormattedPrice()
  90.     {
  91.         return $this->price.' USD';
  92.     }
  93.     public function setPrice(float $price): self
  94.     {
  95.         $this->price $price;
  96.         return $this;
  97.     }
  98.     public function getCreateDate(): ?\DateTimeInterface
  99.     {
  100.         return $this->createDate;
  101.     }
  102.     public function setCreateDate(\DateTimeInterface $createDate): self
  103.     {
  104.         $this->createDate $createDate;
  105.         return $this;
  106.     }
  107.     public function getWhitelabel(): ?Whitelabel
  108.     {
  109.         return $this->whitelabel;
  110.     }
  111.     public function setWhitelabel(?Whitelabel $whitelabel): self
  112.     {
  113.         $this->whitelabel $whitelabel;
  114.         return $this;
  115.     }
  116.     public function getCreatorUser(): ?User
  117.     {
  118.         return $this->creatorUser;
  119.     }
  120.     public function setCreatorUser(?User $creatorUser): self
  121.     {
  122.         $this->creatorUser $creatorUser;
  123.         return $this;
  124.     }
  125.     /**
  126.      * @return Collection<int, Component>
  127.      */
  128.     public function getComponents(): Collection
  129.     {
  130.         return $this->components;
  131.     }
  132.     public function addComponent(Component $component): self
  133.     {
  134.         if (!$this->components->contains($component)) {
  135.             $this->components->add($component);
  136.         }
  137.         return $this;
  138.     }
  139.     public function removeComponent(Component $component): self
  140.     {
  141.         $this->components->removeElement($component);
  142.         return $this;
  143.     }
  144.     /**
  145.      * @return Collection<int, Menu>
  146.      */
  147.     public function getMenus(): Collection
  148.     {
  149.         return $this->menus;
  150.     }
  151.     public function addMenu(Menu $menu): self
  152.     {
  153.         if (!$this->menus->contains($menu)) {
  154.             $this->menus->add($menu);
  155.             $menu->addProduct($this);
  156.         }
  157.         return $this;
  158.     }
  159.     public function removeMenu(Menu $menu): self
  160.     {
  161.         if ($this->menus->removeElement($menu)) {
  162.             $menu->removeProduct($this);
  163.         }
  164.         return $this;
  165.     }
  166.     public function getWeight(): ?int
  167.     {
  168.         return $this->weight;
  169.     }
  170.     public function setWeight(?int $weight): self
  171.     {
  172.         $this->weight $weight;
  173.         return $this;
  174.     }
  175.     public function getCategory(): ?ProductCategory
  176.     {
  177.         return $this->category;
  178.     }
  179.     public function setCategory(?ProductCategory $category): self
  180.     {
  181.         $this->category $category;
  182.         return $this;
  183.     }
  184.     /**
  185.      * @return Collection<int, Allergen>
  186.      */
  187.     public function getAllergens(): Collection
  188.     {
  189.         return $this->allergens;
  190.     }
  191.     public function addAllergen(Allergen $allergen): self
  192.     {
  193.         if (!$this->allergens->contains($allergen)) {
  194.             $this->allergens->add($allergen);
  195.         }
  196.         return $this;
  197.     }
  198.     public function removeAllergen(Allergen $allergen): self
  199.     {
  200.         $this->components->removeElement($allergen);
  201.         return $this;
  202.     }
  203. }