<?php
/**
* @author Leo BANNHOLTZER (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace App\Entity;
use Symfony\Component\Uid\Uuid;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\ContextRepository;
use Gedmo\Mapping\Annotation as Gedmo;
use App\EventListener\ContextSaveListener;
use App\DoctrineExtensions\IsDefaultEntity;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Symfony\Component\Validator\Constraints as Assert;
use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
/**
* @ORM\Entity(repositoryClass=ContextRepository::class)
* @ORM\Table(name="context", indexes={
* @ORM\Index(name="name", columns={"name"}),
* @ORM\Index(name="postcode", columns={"postcode"}),
* @ORM\Index(name="vat_number", columns={"vat_number"}),
* @ORM\Index(name="email", columns={"email"}),
* @ORM\Index(name="is_default", columns={"is_default"}),
* @ORM\Index(name="deleted_at", columns={"deleted_at"}),
* @ORM\Index(name="created_at", columns={"created_at"}),
* @ORM\Index(name="updated_at", columns={"updated_at"}),
* @ORM\Index(name="position", columns={"position"})
* })
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
* @ORM\EntityListeners({ContextSaveListener::class})
*/
class Context
{
use UserTimestampableEntity;
use UserSoftDeleteableEntity;
use IsDefaultEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
private ?Uuid $id = null;
/**
* @ORM\Column(type="string", length=128)
*/
private string $name;
/**
* @ORM\Column(type="string", length=128)
*/
private string $companyName;
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private ?string $address;
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private ?string $address2;
/**
* @ORM\Column(type="string", length=32, nullable=true)
*/
private ?string $postcode;
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private ?string $city;
/**
* @ORM\Column(type="string", length=32, nullable=true)
*/
private ?string $phone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Email
*/
private ?string $email;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $website;
/**
* @ORM\Column(type="string", length=32, nullable=true)
*/
private ?string $vat_number;
/**
* @ORM\Column(type="string", length=16, nullable=true)
*/
private ?string $siren;
/**
* @ORM\Column(type="string", length=16, nullable=true)
*/
private ?string $siret;
/**
* @ORM\Column(type="string", length=8, nullable=true)
*/
private ?string $ape;
/**
* @ORM\ManyToOne(targetEntity=Currency::class, inversedBy="contexts")
* @ORM\JoinColumn(nullable=false)
*/
private Currency $currency;
/**
* @ORM\ManyToOne(targetEntity=Country::class, inversedBy="contexts")
*/
private ?Country $country;
/**
* @ORM\ManyToOne(targetEntity=CountryState::class, inversedBy="contexts")
*/
private ?CountryState $country_state;
/**
* @ORM\OneToMany(targetEntity=Configuration::class, mappedBy="context", fetch="EXTRA_LAZY")
*/
private Collection $configurations;
/**
* @ORM\OneToMany(targetEntity=UserContext::class, mappedBy="context", fetch="EXTRA_LAZY")
*/
private Collection $users;
/**
* @ORM\OneToMany(targetEntity=MailServer::class, mappedBy="context", fetch="EXTRA_LAZY")
*/
private Collection $mail_servers;
/**
* @ORM\OneToMany(targetEntity=Establishment::class, mappedBy="context")
*/
private ?Collection $establishments = null;
/**
* @ORM\Column(type="boolean", options={"default" : "0"})
*/
private bool $isFranchise = false;
/**
* @ORM\Column(type="integer")
*/
private ?int $position = null;
public function __construct()
{
$this->configurations = new ArrayCollection();
$this->users = new ArrayCollection();
$this->mail_servers = new ArrayCollection();
$this->establishments = new ArrayCollection();
}
/**
* @return Uuid|null
*/
public function getId(): ?Uuid
{
return $this->id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
* @return $this
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return string
*/
public function getCompanyName(): string
{
return $this->companyName;
}
/**
* @param string $companyName
* @return $this
*/
public function setCompanyName(string $companyName): self
{
$this->companyName = $companyName;
return $this;
}
/**
* @return string|null
*/
public function getAddress(): ?string
{
return $this->address;
}
/**
* @param string|null $address
* @return $this
*/
public function setAddress(?string $address): self
{
$this->address = $address;
return $this;
}
/**
* @return string|null
*/
public function getAddress2(): ?string
{
return $this->address2;
}
/**
* @param string|null $address2
* @return $this
*/
public function setAddress2(?string $address2): self
{
$this->address2 = $address2;
return $this;
}
/**
* @return string|null
*/
public function getPostcode(): ?string
{
return $this->postcode;
}
/**
* @param string|null $postcode
* @return $this
*/
public function setPostcode(?string $postcode): self
{
$this->postcode = $postcode;
return $this;
}
/**
* @return string|null
*/
public function getCity(): ?string
{
return $this->city;
}
/**
* @param string|null $city
* @return $this
*/
public function setCity(?string $city): self
{
$this->city = $city;
return $this;
}
/**
* @return string|null
*/
public function getPhone(): ?string
{
return $this->phone;
}
/**
* @param string|null $phone
* @return $this
*/
public function setPhone(?string $phone): self
{
$this->phone = preg_replace('/[^0-9]/', '', $phone);
return $this;
}
/**
* @return string|null
*/
public function getEmail(): ?string
{
return $this->email;
}
/**
* @param string|null $email
* @return $this
*/
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
/**
* @return string|null
*/
public function getWebsite(): ?string
{
return $this->website;
}
/**
* @param string|null $website
* @return $this
*/
public function setWebsite(?string $website): self
{
$this->website = $website;
return $this;
}
/**
* @return string|null
*/
public function getVatNumber(): ?string
{
return $this->vat_number;
}
/**
* @param string|null $vat_number
* @return $this
*/
public function setVatNumber(?string $vat_number): self
{
$this->vat_number = $vat_number;
return $this;
}
/**
* @return string|null
*/
public function getSiren(): ?string
{
return $this->siren;
}
/**
* @param string|null $siren
* @return $this
*/
public function setSiren(?string $siren): self
{
$this->siren = $siren;
return $this;
}
/**
* @return string|null
*/
public function getSiret(): ?string
{
return $this->siret;
}
/**
* @param string|null $siret
* @return $this
*/
public function setSiret(?string $siret): self
{
$this->siret = $siret;
return $this;
}
/**
* @return string|null
*/
public function getApe(): ?string
{
return $this->ape;
}
/**
* @param string|null $ape
* @return $this
*/
public function setApe(?string $ape): self
{
$this->ape = $ape;
return $this;
}
/**
* @return Currency
*/
public function getCurrency(): Currency
{
return $this->currency;
}
/**
* @param Currency $currency
* @return $this
*/
public function setCurrency(Currency $currency): self
{
$this->currency = $currency;
return $this;
}
/**
* @return Country|null
*/
public function getCountry(): ?Country
{
return $this->country;
}
/**
* @param Country|null $country
* @return $this
*/
public function setCountry(?Country $country): self
{
$this->country = $country;
return $this;
}
/**
* @return CountryState|null
*/
public function getCountryState(): ?CountryState
{
return $this->country_state;
}
/**
* @param CountryState|null $country_state
* @return $this
*/
public function setCountryState(?CountryState $country_state): self
{
$this->country_state = $country_state;
return $this;
}
/**
* @return Collection|Configuration[]
*/
public function getConfigurations(): Collection
{
return $this->configurations;
}
/**
* @param Configuration $configuration
* @return $this
*/
public function addConfiguration(Configuration $configuration): self
{
if (!$this->configurations->contains($configuration)) {
$this->configurations[] = $configuration;
$configuration->setContext($this);
}
return $this;
}
/**
* @param Configuration $configuration
* @return $this
*/
public function removeConfiguration(Configuration $configuration): self
{
if ($this->configurations->removeElement($configuration)) {
if ($configuration->getContext() === $this) {
$configuration->setContext(null);
}
}
return $this;
}
/**
* @return Collection|UserContext[]
*/
public function getUsers(): Collection
{
return $this->users;
}
/**
* @param UserContext $user
* @return $this
*/
public function addUser(UserContext $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setContext($this);
}
return $this;
}
/**
* @param UserContext $user
* @return $this
*/
public function removeUser(UserContext $user): self
{
if ($this->users->removeElement($user)) {
if ($user->getContext() === $this) {
$user->setContext(null);
}
}
return $this;
}
/**
* @return Collection|MailServer[]
*/
public function getMailServers(): Collection
{
return $this->mail_servers;
}
/**
* @return MailServer|null
*/
public function getMailServer(): ?MailServer
{
return !$this->getMailServers()->isEmpty() ? $this->getMailServers()->first() : null;
}
/**
* @param User|null $user
* @return Collection|Establishment[]
*/
public function getEstablishments(?User $user = null): ?Collection
{
$establishments = $this->establishments;
if (!$user) {
return $establishments;
}
return $establishments->filter(function (Establishment $establishment) use ($user) {
return $establishment->getUsersEstablishments()->isEmpty()
|| !$establishment->getUsersEstablishments()->filter(function (UsersEstablishments $userE) use ($user) {
return $userE->getUser()->getId() === $user->getId();
})->isEmpty()
;
});
}
public function addEstablishment(Establishment $establishment): self
{
if (!$this->establishments->contains($establishment)) {
$this->establishments[] = $establishment;
$establishment->setContext($this);
}
return $this;
}
public function removeEstablishment(Establishment $establishment): self
{
if ($this->establishments->removeElement($establishment)) {
// set the owning side to null (unless already changed)
if ($establishment->getContext() === $this) {
$establishment->setContext(null);
}
}
return $this;
}
/**
* @return bool|null
*/
public function getIsFranchise(): ?bool
{
return $this->isFranchise;
}
/**
* @param bool $isFranchise
* @return $this
*/
public function setIsFranchise(bool $isFranchise): self
{
$this->isFranchise = $isFranchise;
return $this;
}
/**
* @return int|null
*/
public function getPosition(): ?int
{
return $this->position;
}
/**
* @param int|null $position
* @return self
*/
public function setPosition(?int $position): self
{
$this->position = $position;
return $this;
}
}