<?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\LanguageRepository;
use Gedmo\Mapping\Annotation as Gedmo;
use App\DoctrineExtensions\IsActiveEntity;
use App\DoctrineExtensions\IsDefaultEntity;
use App\EventListener\LanguageSaveListener;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass=LanguageRepository::class)
* @ORM\Table(name="language", indexes={
* @ORM\Index(name="name", columns={"name"}),
* @ORM\Index(name="iso_code", columns={"iso_code"}),
* @ORM\Index(name="locale", columns={"locale"}),
* @ORM\Index(name="is_default", columns={"is_default"}),
* @ORM\Index(name="is_active", columns={"is_active"}),
* @ORM\Index(name="deleted_at", columns={"deleted_at"}),
* @ORM\Index(name="created_at", columns={"created_at"}),
* @ORM\Index(name="updated_at", columns={"updated_at"})
* })
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
* @ORM\EntityListeners({LanguageSaveListener::class})
*/
class Language
{
use UserTimestampableEntity;
use UserSoftDeleteableEntity;
use IsActiveEntity;
use IsDefaultEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
* @Groups({"get", "write"})
*/
private ?Uuid $id = null;
/**
* @ORM\Column(type="string", length=32)
* @Groups({"get"})
*/
private string $name;
/**
* @ORM\Column(type="string", length=3)
* @Groups({"get"})
*/
private string $iso_code;
/**
* @ORM\Column(type="string", length=5)
* @Groups({"get"})
*/
private string $locale;
/**
* @ORM\OneToMany(targetEntity=User::class, mappedBy="language", fetch="EXTRA_LAZY")
*/
private Collection $users;
public function __construct()
{
$this->users = 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 getIsoCode(): string
{
return $this->iso_code;
}
/**
* @param string $iso_code
* @return $this
*/
public function setIsoCode(string $iso_code): self
{
$this->iso_code = strtoupper($iso_code);
return $this;
}
/**
* @return string
*/
public function getLocale(): ?string
{
return $this->locale;
}
/**
* @param string $locale
* @return $this
*/
public function setLocale(string $locale): self
{
$this->locale = strtolower($locale);
return $this;
}
/**
* @return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
/**
* @param User $user
* @return $this
*/
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setLanguage($this);
}
return $this;
}
/**
* @param User $user
* @return $this
*/
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
if ($user->getLanguage() === $this) {
$user->setLanguage(null);
}
}
return $this;
}
}