<?php
namespace App\Entity;
use App\Annotation\Exportable;
use App\Annotation\ExportableEntity;
use App\Entity\Interfaces\AddressInterface;
use App\Repository\AddressRepository;
use App\Traits\AddressTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ReflectionClass;
/**
* @ORM\Entity(repositoryClass=AddressRepository::class)
*
* @ExportableEntity()
*/
class Address implements AddressInterface
{
const TYPE_BILLING_ADDRESS = 'billing_address';
const TYPE_SHIPPING_ADDRESS = 'shipping_address';
public const ALLOWED_COUNTRIES = [
'AT',
'BE',
'BG',
'HR',
'CY',
'CZ',
'DK',
'EE',
'FI',
'FR',
'DE',
'GR',
'HU',
'IE',
'IT',
'LV',
'LT',
'LU',
'MT',
'NL',
'PL',
'PT',
'RO',
'SK',
'SI',
'ES',
'SE'
];
use AddressTrait;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="addresses")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @ORM\Column(type="string", length=255, options={"default": self::TYPE_SHIPPING_ADDRESS})
*
* @Exportable()
*/
private $addressType = self::TYPE_SHIPPING_ADDRESS;
/**
* @ORM\OneToMany(targetEntity=CartItemShipping::class, mappedBy="shippingAddress")
*/
private $cartItemShippings;
/**
* @ORM\Column(type="boolean", nullable=true)
*
* @Exportable()
*/
private $preferred;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $comment;
/**
* @ORM\Column(type="boolean", nullable=false, options={"default": true})
*/
private bool $isBillingAddress = true;
public function __construct()
{
$this->cartItemShippings = new ArrayCollection();
}
/**
* @return array
*/
public static function getAddressTypes(): array
{
$typesAddress = [];
$reflect = new ReflectionClass(__CLASS__);
foreach ($reflect->getConstants() as $k => $const) {
if (preg_match("/^(TYPE)/", $k)) {
$typesAddress[$const] = str_replace("_", ".", $const);
}
}
return $typesAddress;
}
/*
* ============================================================================================
* =============================== FONCTIONS CUSTOM ===========================================
* ============================================================================================
*/
public function __toString()
{
return $this->getName();
}
/*
* ============================================================================================
* ============================== FIN FONCTIONS CUSTOM ========================================
* ============================================================================================
*/
/**
* @return User|null
*/
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getAddressType(): ?string
{
return $this->addressType;
}
public function setAddressType(?string $addressType): self
{
$this->addressType = $addressType;
return $this;
}
/**
* @return Collection|CartItemShipping[]
*/
public function getCartItemShippings(): Collection
{
return $this->cartItemShippings;
}
public function addCartItemShipping(CartItemShipping $cartItemShipping): self
{
if (!$this->cartItemShippings->contains($cartItemShipping)) {
$this->cartItemShippings[] = $cartItemShipping;
$cartItemShipping->setShippingAddress($this);
}
return $this;
}
public function removeCartItemShipping(CartItemShipping $cartItemShipping): self
{
if ($this->cartItemShippings->removeElement($cartItemShipping)) {
// set the owning side to null (unless already changed)
if ($cartItemShipping->getShippingAddress() === $this) {
$cartItemShipping->setShippingAddress(null);
}
}
return $this;
}
public function getPreferred(): ?bool
{
return $this->preferred;
}
public function setPreferred(?bool $preferred): self
{
$this->preferred = $preferred;
return $this;
}
public function getComment(): ?string
{
return $this->comment;
}
public function setComment(?string $comment): self
{
$this->comment = $comment;
return $this;
}
public function isIsBillingAddress(): ?bool
{
return $this->isBillingAddress;
}
public function setIsBillingAddress(bool $isBillingAddress): self
{
$this->isBillingAddress = $isBillingAddress;
return $this;
}
}