src/Entity/CartItem.php line 15

Open in your IDE?
  1. <?php
  2.     namespace App\Entity;
  3.     use App\Repository\CartItemRepository;
  4.     use App\Traits\DateTrait;
  5.     use Doctrine\Common\Collections\ArrayCollection;
  6.     use Doctrine\Common\Collections\Collection;
  7.     use Doctrine\ORM\Mapping as ORM;
  8.     use Symfony\Component\Validator\Constraints as Assert;
  9.     /**
  10.      * @ORM\Entity(repositoryClass=CartItemRepository::class)
  11.      */
  12.     class CartItem
  13.     {
  14.         /**
  15.          * @ORM\Id
  16.          * @ORM\GeneratedValue
  17.          * @ORM\Column(type="integer")
  18.          */
  19.         private $id;
  20.         /**
  21.          * SKU : Identifiant d'unité de stock
  22.          * @ORM\Column(type="string", length=16)
  23.          */
  24.         private $sku;
  25.         /**
  26.          * @ORM\Column(type="integer")
  27.          */
  28.         private $quantity;
  29.         /**
  30.          * @ORM\ManyToOne(targetEntity=Cart::class, inversedBy="items")
  31.          * @ORM\JoinColumn(nullable=false, referencedColumnName="id")
  32.          */
  33.         private $cart;
  34.         /**
  35.          * @ORM\OneToMany(targetEntity=CartItemOption::class, mappedBy="item",cascade={ "remove"})
  36.          */
  37.         private $options;
  38.         /**
  39.          * @ORM\OneToMany(targetEntity=CartItemShipping::class, mappedBy="item",cascade={"persist", "remove"})
  40.          * @ORM\JoinColumn(nullable=true)
  41.          */
  42.         private $shipments;
  43.         /**
  44.          * @ORM\OneToMany(targetEntity=CartItemParticipant::class, mappedBy="cartItem", cascade={"persist","remove"})
  45.          * @ORM\JoinColumn(nullable=true)
  46.          */
  47.         private $participants;
  48.         use DateTrait;
  49.         public function __construct()
  50.         {
  51.             $this->options      = new ArrayCollection();
  52.             $this->shipments    = new ArrayCollection();
  53.             $this->participants = new ArrayCollection();
  54.         }
  55.         /*
  56.          * ============================================================================================
  57.          * =============================== FONCTIONS CUSTOM ===========================================
  58.          * ============================================================================================
  59.          */
  60.         /**
  61.          * @Assert\IsFalse(message = "Les quantités sont incorrectes, merci de rectifier")
  62.          * @return boolean
  63.          */
  64.         public function isQuantityByItemShipping()
  65.         {
  66.             $quantity 0;
  67.             foreach ( $this->getShipments() as $shipment ) {
  68.                 if ( $shipment instanceof CartItemShipping ) {
  69.                     $quantity += $shipment->getQuantity();
  70.                 }
  71.             }
  72.             if ( $quantity <= $this->getQuantity() ) {
  73.                 return FALSE;
  74.             }
  75.             return TRUE;
  76.         }
  77.         /**
  78.          * @return Collection|CartItemShipping[]
  79.          */
  80.         public function getShipments(): Collection
  81.         {
  82.             return $this->shipments;
  83.         }
  84.         public function getQuantity(): ?int
  85.         {
  86.             return $this->quantity;
  87.         }
  88.         /*
  89.          * ============================================================================================
  90.          * ============================== FIN FONCTIONS CUSTOM ========================================
  91.          * ============================================================================================
  92.          */
  93.         public function setQuantityint $quantity ): self
  94.         {
  95.             $this->quantity $quantity;
  96.             return $this;
  97.         }
  98.         public function getInitItemShipping()
  99.         {
  100.             foreach ( $this->getShipments() as $shipment ) {
  101.                 if ( $shipment instanceof CartItemShipping && $shipment->getInit() ) {
  102.                     return $shipment;
  103.                 }
  104.             }
  105.             return NULL;
  106.         }
  107.         public function getItemsINotShipping()
  108.         {
  109.             foreach ( $this->getShipments() as $shipment ) {
  110.                 if ( $shipment instanceof CartItemShipping && NULL === $shipment->getShippingAddress() ) {
  111.                     return TRUE;
  112.                 }
  113.             }
  114.             return FALSE;
  115.         }
  116.         public function getId(): ?int
  117.         {
  118.             return $this->id;
  119.         }
  120.         public function getSku(): ?string
  121.         {
  122.             return $this->sku;
  123.         }
  124.         public function setSkustring $sku ): self
  125.         {
  126.             $this->sku $sku;
  127.             return $this;
  128.         }
  129.         public function getCart(): ?Cart
  130.         {
  131.             return $this->cart;
  132.         }
  133.         public function setCart( ?Cart $cart ): self
  134.         {
  135.             $this->cart $cart;
  136.             return $this;
  137.         }
  138.         /**
  139.          * @return Collection|CartItemOption[]
  140.          */
  141.         public function getOptions(): Collection
  142.         {
  143.             return $this->options;
  144.         }
  145.         public function addOptionCartItemOption $option ): self
  146.         {
  147.             if ( !$this->options->contains$option ) ) {
  148.                 $this->options[] = $option;
  149.                 $option->setItem$this );
  150.             }
  151.             return $this;
  152.         }
  153.         public function removeOptionCartItemOption $option ): self
  154.         {
  155.             if ( $this->options->removeElement$option ) ) {
  156.                 // set the owning side to null (unless already changed)
  157.                 if ( $option->getItem() === $this ) {
  158.                     $option->setItemNULL );
  159.                 }
  160.             }
  161.             return $this;
  162.         }
  163.         public function addQuantity$quantity ): CartItem
  164.         {
  165.             if ( is_null$this->quantity ) ) {
  166.                 $this->quantity 0;
  167.             }
  168.             $this->quantity += $quantity;
  169.             return $this;
  170.         }
  171.         public function editQuantity$quantity ): CartItem
  172.         {
  173.             $this->quantity $quantity;
  174.             return $this;
  175.         }
  176.         public function removeQuantity$quantity ): CartItem
  177.         {
  178.             $this->quantity -= $quantity;
  179.             if ( $this->quantity ) {
  180.                 $this->quantity 0;
  181.             }
  182.             return $this;
  183.         }
  184.         public function addShipmentCartItemShipping $shipment ): self
  185.         {
  186.             if ( !$this->shipments->contains$shipment ) ) {
  187.                 $this->shipments[] = $shipment;
  188.                 $shipment->setItem$this );
  189.             }
  190.             return $this;
  191.         }
  192.         public function removeShipmentCartItemShipping $shipment ): self
  193.         {
  194.             if ( $this->shipments->removeElement$shipment ) ) {
  195.                 // set the owning side to null (unless already changed)
  196.                 if ( $shipment->getItem() === $this ) {
  197.                     $shipment->setItemNULL );
  198.                 }
  199.             }
  200.             return $this;
  201.         }
  202.         /**
  203.          * @return Collection|CartItemParticipant[]
  204.          */
  205.         public function getParticipants(): Collection
  206.         {
  207.             return $this->participants;
  208.         }
  209.         public function addParticipantCartItemParticipant $participant ): self
  210.         {
  211.             if ( !$this->participants->contains$participant ) ) {
  212.                 $this->participants[] = $participant;
  213.                 $participant->setCartItem$this );
  214.             }
  215.             return $this;
  216.         }
  217.         public function removeParticipantCartItemParticipant $participant ): self
  218.         {
  219.             if ( $this->participants->removeElement$participant ) ) {
  220.                 // set the owning side to null (unless already changed)
  221.                 if ( $participant->getCartItem() === $this ) {
  222.                     $participant->setCartItemNULL );
  223.                 }
  224.             }
  225.             return $this;
  226.         }
  227.     }