src/Entity/Customer.php line 28

Open in your IDE?
  1. <?php
  2. /**
  3.  * @license Silk Software House Sp. Z O.O.
  4.  */
  5. declare(strict_types=1);
  6. namespace App\Entity;
  7. use App\Entity\Traits\IdTrait;
  8. use App\Entity\Traits\TimePreUpdateTrait;
  9. use App\Entity\Traits\TimeTrait;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  14. use DateTime;
  15. /**
  16.  * Class Customer.
  17.  *
  18.  * @ORM\Table("customers")
  19.  * @ORM\Entity(repositoryClass="App\Repository\CustomerRepository")
  20.  * @ORM\HasLifecycleCallbacks()
  21.  *
  22.  * @UniqueEntity(fields={"name"})
  23.  */
  24. class Customer
  25. {
  26.     use IdTrait;
  27.     use TimeTrait;
  28.     use TimePreUpdateTrait;
  29.     /**
  30.      * Name.
  31.      *
  32.      * @var string|null
  33.      *
  34.      * @ORM\Column(name="name", type="text", nullable=true, options={"default": "", "comment": "Company name"})
  35.      */
  36.     private ?string $name null;
  37.     /**
  38.      * Country.
  39.      *
  40.      * @var string|null
  41.      *
  42.      * @ORM\Column(name="country", type="text", nullable=true, options={"default": "", "comment": "Company country"})
  43.      */
  44.     private ?string $country null;
  45.     /**
  46.      * Brand.
  47.      *
  48.      * @var string|null
  49.      *
  50.      * @ORM\Column(name="brand", type="text", nullable=true, options={"default": "", "comment": "Company brand"})
  51.      */
  52.     private ?string $brand null;
  53.     /**
  54.      * Devices.
  55.      *
  56.      * @var Collection<int, Device>|null
  57.      *
  58.      * @ORM\OneToMany(targetEntity="App\Entity\Device", mappedBy="customer", cascade={"persist", "remove"})
  59.      */
  60.     private ?Collection $devices null;
  61.     /**
  62.      * Devices.
  63.      *
  64.      * @var Collection<int, User>|null
  65.      *
  66.      * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="customer", cascade={"persist", "remove"})
  67.      */
  68.     private ?Collection $users null;
  69.     /**
  70.      * Account activity ends at.
  71.      *
  72.      * @var DateTime|null
  73.      *
  74.      * @ORM\Column(name="active_to", type="datetime", nullable=true, options={"comment": "Active to."})
  75.      */
  76.     private ?DateTime $activeTo null;
  77.     /**
  78.      * Customer constructor.
  79.      */
  80.     public function __construct()
  81.     {
  82.         $this->devices = new ArrayCollection();
  83.         $this->users = new ArrayCollection();
  84.     }
  85.     /**
  86.      * Get name.
  87.      *
  88.      * @return string|null
  89.      */
  90.     public function getName(): ?string
  91.     {
  92.         return $this->name;
  93.     }
  94.     /**
  95.      * Set name.
  96.      *
  97.      * @param string $name
  98.      *
  99.      * @return Customer
  100.      */
  101.     public function setName(string $name): self
  102.     {
  103.         $this->name $name;
  104.         return $this;
  105.     }
  106.     /**
  107.      * To string.
  108.      *
  109.      * @return string
  110.      */
  111.     public function __toString(): string
  112.     {
  113.         return $this->name ?? '';
  114.     }
  115.     /**
  116.      * Get devices.
  117.      *
  118.      * @return Collection
  119.      */
  120.     public function getDevices(): Collection
  121.     {
  122.         if (!isset($this->devices)) {
  123.             $this->devices = new ArrayCollection();
  124.         }
  125.         return $this->devices;
  126.     }
  127.     /**
  128.      * Add device.
  129.      *
  130.      * @param Device $device
  131.      *
  132.      * @return Customer
  133.      */
  134.     public function addDevice(Device $device): self
  135.     {
  136.         if (!isset($this->devices)) {
  137.             $this->devices = new ArrayCollection();
  138.         }
  139.         if ($this->devices->contains($device)) {
  140.             return $this;
  141.         }
  142.         $this->devices->add($device);
  143.         $device->setCustomer($this);
  144.         return $this;
  145.     }
  146.     /**
  147.      * Remove device.
  148.      *
  149.      * @param Device $device
  150.      *
  151.      * @return Customer
  152.      */
  153.     public function removeDevice(Device $device): self
  154.     {
  155.         if (!isset($this->devices)) {
  156.             $this->devices = new ArrayCollection();
  157.         }
  158.         if (!$this->devices->contains($device)) {
  159.             return $this;
  160.         }
  161.         $this->devices->removeElement($device);
  162.         $device->setCustomer(null);
  163.         return $this;
  164.     }
  165.     /**
  166.      * Get active to.
  167.      *
  168.      * @return DateTime|null
  169.      */
  170.     public function getActiveTo(): ?DateTime
  171.     {
  172.         return $this->activeTo;
  173.     }
  174.     /**
  175.      * Set active to.
  176.      *
  177.      * @param DateTime|null $activeTo
  178.      *
  179.      * @return Customer
  180.      */
  181.     public function setActiveTo(?DateTime $activeTo): self
  182.     {
  183.         $this->activeTo $activeTo;
  184.         return $this;
  185.     }
  186.     /**
  187.      * Get is active.
  188.      *
  189.      * @return bool
  190.      */
  191.     public function getIsActive(): bool
  192.     {
  193.         return !$this->activeTo || $this->activeTo > new DateTime();
  194.     }
  195.     /**
  196.      * Set is active.
  197.      *
  198.      * @param bool $isActive
  199.      *
  200.      * @return Customer
  201.      */
  202.     public function setIsActive(bool $isActive): self
  203.     {
  204.         $this->activeTo $isActive null : new DateTime();
  205.         return $this;
  206.     }
  207.     /**
  208.      * Get users.
  209.      *
  210.      * @return Collection
  211.      */
  212.     public function getUsers(): Collection
  213.     {
  214.         if (!isset($this->users)) {
  215.             $this->users = new ArrayCollection();
  216.         }
  217.         return $this->users;
  218.     }
  219.     /**
  220.      * @return string|null
  221.      */
  222.     public function getCountry(): ?string
  223.     {
  224.         return $this->country;
  225.     }
  226.     /**
  227.      * @param string|null $country
  228.      *
  229.      * @return $this
  230.      */
  231.     public function setCountry(?string $country): self
  232.     {
  233.         $this->country $country;
  234.         return $this;
  235.     }
  236.     /**
  237.      * @return string|null
  238.      */
  239.     public function getBrand(): ?string
  240.     {
  241.         return $this->brand;
  242.     }
  243.     /**
  244.      * @param string|null $brand
  245.      *
  246.      * @return Customer
  247.      */
  248.     public function setBrand(?string $brand): self
  249.     {
  250.         $this->brand $brand;
  251.         return $this;
  252.     }
  253.     /**
  254.      * @param array $brands
  255.      * @return $this
  256.      */
  257.     public function setBrands(array $brands): self
  258.     {
  259.         $this->brand implode(','$brands) ? : null;
  260.         return $this;
  261.     }
  262.     /**
  263.      * @return string[]
  264.      */
  265.     public function getBrands(): array
  266.     {
  267.         $brands explode(','$this->brand ?? '');
  268.         return array_map(static function (string $brand) {
  269.             return trim($brand);
  270.         }, $brands);
  271.     }
  272. }