<?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\CurrencyRepository;
use Gedmo\Mapping\Annotation as Gedmo;
use App\DoctrineExtensions\IsActiveEntity;
use App\DoctrineExtensions\IsDefaultEntity;
use App\EventListener\CurrencySaveListener;
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;
/**
* @ORM\Entity(repositoryClass=CurrencyRepository::class)
* @ORM\Table(name="currency", indexes={
* @ORM\Index(name="name", columns={"name"}),
* @ORM\Index(name="iso_code", columns={"iso_code"}),
* @ORM\Index(name="change_rate", columns={"change_rate"}),
* @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({CurrencySaveListener::class})
*/
class Currency
{
use UserTimestampableEntity;
use UserSoftDeleteableEntity;
use IsActiveEntity;
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=64)
*/
private string $name;
/**
* @ORM\Column(type="string", length=3)
*/
private string $iso_code;
/**
* @ORM\Column(type="decimal", precision=20, scale=12, options={"default" : "1.000000000000"})
*/
private string $change_rate;
/**
* @ORM\OneToMany(targetEntity=Context::class, mappedBy="currency", fetch="EXTRA_LAZY")
*/
private Collection $contexts;
public function __construct()
{
$this->contexts = 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 = $iso_code;
return $this;
}
/**
* @return string
*/
public function getChangeRate(): string
{
return $this->change_rate;
}
/**
* @param string $change_rate
* @return $this
*/
public function setChangeRate(string $change_rate): self
{
$this->change_rate = $change_rate;
if (str_contains($this->change_rate, '.')) {
$array = explode(".", $this->change_rate);
$after = $array[1];
if (strlen($after) > 12) {
$after = substr($after, 0, 12);
}
$this->change_rate = $array[0] . '.' . $after;
}
return $this;
}
/**
* @return Collection|Context[]
*/
public function getContexts(): Collection
{
return $this->contexts;
}
/**
* @param Context $context
* @return $this
*/
public function addContext(Context $context): self
{
if (!$this->contexts->contains($context)) {
$this->contexts[] = $context;
$context->setCurrency($this);
}
return $this;
}
}