src/Entity/Address.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Annotation\Exportable;
  4. use App\Annotation\ExportableEntity;
  5. use App\Entity\Interfaces\AddressInterface;
  6. use App\Repository\AddressRepository;
  7. use App\Traits\AddressTrait;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use ReflectionClass;
  12. /**
  13. * @ORM\Entity(repositoryClass=AddressRepository::class)
  14. *
  15. * @ExportableEntity()
  16. */
  17. class Address implements AddressInterface
  18. {
  19. const TYPE_BILLING_ADDRESS = 'billing_address';
  20. const TYPE_SHIPPING_ADDRESS = 'shipping_address';
  21. public const ALLOWED_COUNTRIES = [
  22. 'AT',
  23. 'BE',
  24. 'BG',
  25. 'HR',
  26. 'CY',
  27. 'CZ',
  28. 'DK',
  29. 'EE',
  30. 'FI',
  31. 'FR',
  32. 'DE',
  33. 'GR',
  34. 'HU',
  35. 'IE',
  36. 'IT',
  37. 'LV',
  38. 'LT',
  39. 'LU',
  40. 'MT',
  41. 'NL',
  42. 'PL',
  43. 'PT',
  44. 'RO',
  45. 'SK',
  46. 'SI',
  47. 'ES',
  48. 'SE'
  49. ];
  50. use AddressTrait;
  51. /**
  52. * @ORM\ManyToOne(targetEntity=User::class, inversedBy="addresses")
  53. * @ORM\JoinColumn(nullable=false)
  54. */
  55. private $user;
  56. /**
  57. * @ORM\Column(type="string", length=255, options={"default": self::TYPE_SHIPPING_ADDRESS})
  58. *
  59. * @Exportable()
  60. */
  61. private $addressType = self::TYPE_SHIPPING_ADDRESS;
  62. /**
  63. * @ORM\OneToMany(targetEntity=CartItemShipping::class, mappedBy="shippingAddress")
  64. */
  65. private $cartItemShippings;
  66. /**
  67. * @ORM\Column(type="boolean", nullable=true)
  68. *
  69. * @Exportable()
  70. */
  71. private $preferred;
  72. /**
  73. * @ORM\Column(type="text", nullable=true)
  74. */
  75. private $comment;
  76. /**
  77. * @ORM\Column(type="boolean", nullable=false, options={"default": true})
  78. */
  79. private bool $isBillingAddress = true;
  80. public function __construct()
  81. {
  82. $this->cartItemShippings = new ArrayCollection();
  83. }
  84. /**
  85. * @return array
  86. */
  87. public static function getAddressTypes(): array
  88. {
  89. $typesAddress = [];
  90. $reflect = new ReflectionClass(__CLASS__);
  91. foreach ($reflect->getConstants() as $k => $const) {
  92. if (preg_match("/^(TYPE)/", $k)) {
  93. $typesAddress[$const] = str_replace("_", ".", $const);
  94. }
  95. }
  96. return $typesAddress;
  97. }
  98. /*
  99. * ============================================================================================
  100. * =============================== FONCTIONS CUSTOM ===========================================
  101. * ============================================================================================
  102. */
  103. public function __toString()
  104. {
  105. return $this->getName();
  106. }
  107. /*
  108. * ============================================================================================
  109. * ============================== FIN FONCTIONS CUSTOM ========================================
  110. * ============================================================================================
  111. */
  112. /**
  113. * @return User|null
  114. */
  115. public function getUser(): ?User
  116. {
  117. return $this->user;
  118. }
  119. public function setUser(?User $user): self
  120. {
  121. $this->user = $user;
  122. return $this;
  123. }
  124. public function getAddressType(): ?string
  125. {
  126. return $this->addressType;
  127. }
  128. public function setAddressType(?string $addressType): self
  129. {
  130. $this->addressType = $addressType;
  131. return $this;
  132. }
  133. /**
  134. * @return Collection|CartItemShipping[]
  135. */
  136. public function getCartItemShippings(): Collection
  137. {
  138. return $this->cartItemShippings;
  139. }
  140. public function addCartItemShipping(CartItemShipping $cartItemShipping): self
  141. {
  142. if (!$this->cartItemShippings->contains($cartItemShipping)) {
  143. $this->cartItemShippings[] = $cartItemShipping;
  144. $cartItemShipping->setShippingAddress($this);
  145. }
  146. return $this;
  147. }
  148. public function removeCartItemShipping(CartItemShipping $cartItemShipping): self
  149. {
  150. if ($this->cartItemShippings->removeElement($cartItemShipping)) {
  151. // set the owning side to null (unless already changed)
  152. if ($cartItemShipping->getShippingAddress() === $this) {
  153. $cartItemShipping->setShippingAddress(null);
  154. }
  155. }
  156. return $this;
  157. }
  158. public function getPreferred(): ?bool
  159. {
  160. return $this->preferred;
  161. }
  162. public function setPreferred(?bool $preferred): self
  163. {
  164. $this->preferred = $preferred;
  165. return $this;
  166. }
  167. public function getComment(): ?string
  168. {
  169. return $this->comment;
  170. }
  171. public function setComment(?string $comment): self
  172. {
  173. $this->comment = $comment;
  174. return $this;
  175. }
  176. public function isIsBillingAddress(): ?bool
  177. {
  178. return $this->isBillingAddress;
  179. }
  180. public function setIsBillingAddress(bool $isBillingAddress): self
  181. {
  182. $this->isBillingAddress = $isBillingAddress;
  183. return $this;
  184. }
  185. }