src/Entity/Product/Product.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Product;
  3. use App\Validator\Constraints as AppAssert;
  4. use App\Entity\Customer;
  5. use App\Entity\Supplier;
  6. use DateTime;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\ORM\Mapping\{Column,
  9.     DiscriminatorColumn,
  10.     DiscriminatorMap,
  11.     Entity,
  12.     GeneratedValue,
  13.     HasLifecycleCallbacks,
  14.     Id,
  15.     InheritanceType,
  16.     JoinColumn,
  17.     ManyToMany,
  18.     ManyToOne,
  19.     PrePersist,
  20.     PreUpdate,
  21.     Table};
  22. use Doctrine\ORM\Mapping as ORM;
  23. use Exception;
  24. use JMS\Serializer\Annotation\Groups;
  25. use Symfony\Component\Validator\Constraints\{NotBlank};
  26. use Symfony\Component\Validator\Constraints as Assert;
  27. /**
  28.  * Class Product
  29.  *
  30.  * @package App\Entity
  31.  * @Table(name="malys_product")
  32.  * @InheritanceType("JOINED")
  33.  * @DiscriminatorColumn(name="type", type="string")
  34.  * @AppAssert\Product()
  35.  * @DiscriminatorMap({"guest" = "GuestProduct", "confirmed" = "ConfirmedProduct"})
  36.  * @Entity()
  37.  * @HasLifecycleCallbacks()
  38.  */
  39. abstract class Product
  40. {
  41.     public const TYPES = [
  42.         self::TYPE_CONFIRMED,
  43.         self::TYPE_GUEST,
  44.     ];
  45.     public const TYPE_CONFIRMED 'confirmed';
  46.     public const TYPE_GUEST 'guest';
  47.     /**
  48.      * @var integer
  49.      *
  50.      * @Column(name="id", type="integer")
  51.      * @Id
  52.      * @GeneratedValue(strategy="AUTO")
  53.      * @Groups({"api"})
  54.      */
  55.     protected $id;
  56.     /**
  57.      * @var string
  58.      *
  59.      * @Column(name="code", type="string", length=255, nullable=true)
  60.      * @Assert\NotBlank(message="Indiquez le code du produit", groups={"invoiceEdit"})
  61.      */
  62.     protected $code;
  63.     /**
  64.      * @var Supplier
  65.      *
  66.      * @ManyToOne(targetEntity="App\Entity\Supplier", inversedBy="products")
  67.      * @JoinColumn(name="supplier_id", referencedColumnName="id")
  68.      * @NotBlank()
  69.      */
  70.     protected $supplier;
  71.     /**
  72.      * @var string
  73.      *
  74.      * @Column(name="name", type="string", length=255, nullable=false)
  75.      * @NotBlank(message="Indiquez le nom du produit")
  76.      * @Groups({"api"})
  77.      */
  78.     protected $name;
  79.     /**
  80.      * @var boolean
  81.      *
  82.      * @Column(name="enabled", type="boolean", )
  83.      * @Groups({"api"})
  84.      */
  85.     protected $enabled true;
  86.     /**
  87.      * @var DateTime
  88.      *
  89.      * @Column(name="createdAt", type="datetime", nullable=false)
  90.      * @Groups({"api"})
  91.      */
  92.     protected $createdAt;
  93.     /**
  94.      * @var DateTime
  95.      *
  96.      * @Column(name="updatedAt", type="datetime", nullable=true)
  97.      * @Groups({"api"})
  98.      */
  99.     protected $updatedAt;
  100.     /**
  101.      * @ManyToMany(targetEntity="App\Entity\Customer", inversedBy="favoritesProducts", fetch="LAZY")
  102.      * @ORM\JoinTable(name="malys_customers_favorites_products")
  103.      */
  104.     protected $customers;
  105.     public function __construct()
  106.     {
  107.         $this->customers = new ArrayCollection();
  108.     }
  109.     abstract public function getType();
  110.     /**
  111.      * @return int
  112.      */
  113.     public function getId()
  114.     {
  115.         return $this->id;
  116.     }
  117.     /**
  118.      * @param int $id
  119.      */
  120.     public function setId($id)
  121.     {
  122.         $this->id $id;
  123.     }
  124.     /**
  125.      * @return string
  126.      */
  127.     public function getCode()
  128.     {
  129.         return $this->code;
  130.     }
  131.     /**
  132.      * @param string $code
  133.      */
  134.     public function setCode($code)
  135.     {
  136.         $this->code $code;
  137.     }
  138.     /**
  139.      * @return string
  140.      */
  141.     public function getName()
  142.     {
  143.         return $this->name;
  144.     }
  145.     /**
  146.      * @param string $name
  147.      */
  148.     public function setName($name)
  149.     {
  150.         $this->name $name;
  151.     }
  152.     /**
  153.      * @return mixed
  154.      */
  155.     public function getSupplier()
  156.     {
  157.         return $this->supplier;
  158.     }
  159.     /**
  160.      * @param mixed $supplier
  161.      */
  162.     public function setSupplier($supplier)
  163.     {
  164.         $this->supplier $supplier;
  165.     }
  166.     /**
  167.      * @return bool
  168.      */
  169.     public function isEnabled()
  170.     {
  171.         return $this->enabled;
  172.     }
  173.     /**
  174.      * @param bool $enabled
  175.      */
  176.     public function setEnabled($enabled)
  177.     {
  178.         $this->enabled $enabled;
  179.     }
  180.     /**
  181.      * @return DateTime
  182.      */
  183.     public function getCreatedAt()
  184.     {
  185.         return $this->createdAt;
  186.     }
  187.     /**
  188.      * @return Product
  189.      * @throws Exception
  190.      * @PrePersist
  191.      */
  192.     public function setCreatedAt()
  193.     {
  194.         $this->createdAt = new DateTime();
  195.         $this->updatedAt = new DateTime();
  196.         return $this;
  197.     }
  198.     /**
  199.      * @return DateTime
  200.      */
  201.     public function getUpdatedAt()
  202.     {
  203.         return $this->updatedAt;
  204.     }
  205.     /**
  206.      * @return ArrayCollection
  207.      */
  208.     public function getCustomers()
  209.     {
  210.         return $this->customers;
  211.     }
  212.     /**
  213.      * @param ArrayCollection $customers
  214.      */
  215.     public function setCustomers($customers)
  216.     {
  217.         $this->customers $customers;
  218.     }
  219.     /**
  220.      * @param Customer $customer
  221.      */
  222.     public function addCustomer($customer)
  223.     {
  224.         if (!$this->customers->contains($customer)) {
  225.             $this->customers->add($customer);
  226.         }
  227.     }
  228.     /**
  229.      * @param Customer $customer
  230.      */
  231.     public function removeCustomer($customer)
  232.     {
  233.         if ($this->customers->contains($customer)) {
  234.             $this->customers->removeElement($customer);
  235.             $customer->removeFavoriteProduct($this);
  236.         }
  237.     }
  238.     public function addOrRemoveInCustomerFav(Customer $customer)
  239.     {
  240.         if ($this->customers->contains($customer)) {
  241.             $this->customers->removeElement($customer);
  242.             return false;
  243.         } else {
  244.             $this->customers->add($customer);
  245.             return true;
  246.         }
  247.     }
  248.     /**
  249.      * Set updatedAt
  250.      *
  251.      * @return Product
  252.      * @throws Exception
  253.      * @PreUpdate
  254.      */
  255.     public function setUpdatedAt()
  256.     {
  257.         if ($this->autoUpdateDate === false) {
  258.             return $this;
  259.         }
  260.         $this->updatedAt = new DateTime();
  261.         return $this;
  262.     }
  263.     public function __call($name$arguments)
  264.     {
  265.         if (method_exists(self::class, $name)) {
  266.             return $this->$name;
  267.         }
  268.         return null;
  269.     }
  270.     public function __get($name)
  271.     {
  272.         $methods = [
  273.             'get' $name,
  274.             'get' ucfirst($name),
  275.             'is' $name,
  276.             'is' ucfirst($name)
  277.         ];
  278.         foreach ($methods as $method) {
  279.             if (method_exists(self::class, $method)) {
  280.                 return $this->$method;
  281.             }
  282.         }
  283.         return null;
  284.     }
  285. }