<?php
/**
* @license Silk Software House Sp. Z O.O.
*/
declare(strict_types=1);
namespace App\Entity;
use App\Entity\Traits\IdTrait;
use App\Entity\Traits\TimePreUpdateTrait;
use App\Entity\Traits\TimeTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use DateTime;
/**
* Class Customer.
*
* @ORM\Table("customers")
* @ORM\Entity(repositoryClass="App\Repository\CustomerRepository")
* @ORM\HasLifecycleCallbacks()
*
* @UniqueEntity(fields={"name"})
*/
class Customer
{
use IdTrait;
use TimeTrait;
use TimePreUpdateTrait;
/**
* Name.
*
* @var string|null
*
* @ORM\Column(name="name", type="text", nullable=true, options={"default": "", "comment": "Company name"})
*/
private ?string $name = null;
/**
* Country.
*
* @var string|null
*
* @ORM\Column(name="country", type="text", nullable=true, options={"default": "", "comment": "Company country"})
*/
private ?string $country = null;
/**
* Brand.
*
* @var string|null
*
* @ORM\Column(name="brand", type="text", nullable=true, options={"default": "", "comment": "Company brand"})
*/
private ?string $brand = null;
/**
* Devices.
*
* @var Collection<int, Device>|null
*
* @ORM\OneToMany(targetEntity="App\Entity\Device", mappedBy="customer", cascade={"persist", "remove"})
*/
private ?Collection $devices = null;
/**
* Devices.
*
* @var Collection<int, User>|null
*
* @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="customer", cascade={"persist", "remove"})
*/
private ?Collection $users = null;
/**
* Account activity ends at.
*
* @var DateTime|null
*
* @ORM\Column(name="active_to", type="datetime", nullable=true, options={"comment": "Active to."})
*/
private ?DateTime $activeTo = null;
/**
* Customer constructor.
*/
public function __construct()
{
$this->devices = new ArrayCollection();
$this->users = new ArrayCollection();
}
/**
* Get name.
*
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}
/**
* Set name.
*
* @param string $name
*
* @return Customer
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* To string.
*
* @return string
*/
public function __toString(): string
{
return $this->name ?? '';
}
/**
* Get devices.
*
* @return Collection
*/
public function getDevices(): Collection
{
if (!isset($this->devices)) {
$this->devices = new ArrayCollection();
}
return $this->devices;
}
/**
* Add device.
*
* @param Device $device
*
* @return Customer
*/
public function addDevice(Device $device): self
{
if (!isset($this->devices)) {
$this->devices = new ArrayCollection();
}
if ($this->devices->contains($device)) {
return $this;
}
$this->devices->add($device);
$device->setCustomer($this);
return $this;
}
/**
* Remove device.
*
* @param Device $device
*
* @return Customer
*/
public function removeDevice(Device $device): self
{
if (!isset($this->devices)) {
$this->devices = new ArrayCollection();
}
if (!$this->devices->contains($device)) {
return $this;
}
$this->devices->removeElement($device);
$device->setCustomer(null);
return $this;
}
/**
* Get active to.
*
* @return DateTime|null
*/
public function getActiveTo(): ?DateTime
{
return $this->activeTo;
}
/**
* Set active to.
*
* @param DateTime|null $activeTo
*
* @return Customer
*/
public function setActiveTo(?DateTime $activeTo): self
{
$this->activeTo = $activeTo;
return $this;
}
/**
* Get is active.
*
* @return bool
*/
public function getIsActive(): bool
{
return !$this->activeTo || $this->activeTo > new DateTime();
}
/**
* Set is active.
*
* @param bool $isActive
*
* @return Customer
*/
public function setIsActive(bool $isActive): self
{
$this->activeTo = $isActive ? null : new DateTime();
return $this;
}
/**
* Get users.
*
* @return Collection
*/
public function getUsers(): Collection
{
if (!isset($this->users)) {
$this->users = new ArrayCollection();
}
return $this->users;
}
/**
* @return string|null
*/
public function getCountry(): ?string
{
return $this->country;
}
/**
* @param string|null $country
*
* @return $this
*/
public function setCountry(?string $country): self
{
$this->country = $country;
return $this;
}
/**
* @return string|null
*/
public function getBrand(): ?string
{
return $this->brand;
}
/**
* @param string|null $brand
*
* @return Customer
*/
public function setBrand(?string $brand): self
{
$this->brand = $brand;
return $this;
}
/**
* @param array $brands
* @return $this
*/
public function setBrands(array $brands): self
{
$this->brand = implode(',', $brands) ? : null;
return $this;
}
/**
* @return string[]
*/
public function getBrands(): array
{
$brands = explode(',', $this->brand ?? '');
return array_map(static function (string $brand) {
return trim($brand);
}, $brands);
}
}