src/OnlinePaymentBundle/Entity/PaymentMethod.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\OnlinePaymentBundle\Entity;
  3. use App\MediaBundle\Entity\Image;
  4. use App\OnlinePaymentBundle\Entity\Translation\PaymentMethodTranslation;
  5. use App\OnlinePaymentBundle\Enum\PaymentMethodEnum;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use PN\LocaleBundle\Model\LocaleTrait;
  10. use PN\LocaleBundle\Model\Translatable;
  11. use PN\ServiceBundle\Interfaces\DateTimeInterface;
  12. use PN\ServiceBundle\Model\DateTimeTrait;
  13. use PN\ServiceBundle\Model\VirtualDeleteTrait;
  14. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. /**
  17. * @ORM\HasLifecycleCallbacks
  18. * @ORM\Table("payment_method")
  19. * @UniqueEntity("type")
  20. * @ORM\Entity(repositoryClass="App\OnlinePaymentBundle\Repository\PaymentMethodRepository")
  21. */
  22. class PaymentMethod implements Translatable, DateTimeInterface
  23. {
  24. use VirtualDeleteTrait,
  25. DateTimeTrait,
  26. LocaleTrait;
  27. /**
  28. * @ORM\Column(name="id", type="integer")
  29. * @ORM\Id
  30. * @ORM\GeneratedValue(strategy="AUTO")
  31. */
  32. private ?int $id = null;
  33. /**
  34. * @Assert\NotBlank()
  35. * @ORM\Column(name="title", type="string", length=45)
  36. */
  37. private ?string $title = null;
  38. /**
  39. * @ORM\OneToOne(targetEntity="App\MediaBundle\Entity\Image", cascade={"persist", "remove" })
  40. */
  41. private ?Image $image = null;
  42. /**
  43. * @ORM\Column(name="note", type="text", nullable=true)
  44. */
  45. private ?string $note = null;
  46. /**
  47. * @Assert\NotBlank()
  48. * @ORM\Column(name="type", type="string", enumType=PaymentMethodEnum::class, length=255, unique=true, nullable=false)
  49. */
  50. private ?PaymentMethodEnum $type = null;
  51. /**
  52. * @ORM\Column(name="active", type="boolean", nullable=false)
  53. */
  54. private bool $active = false;
  55. /**
  56. * @ORM\Column(name="fees", type="float", nullable=true)
  57. */
  58. private float $fees = 0;
  59. /**
  60. * @ORM\OneToMany(targetEntity="App\OnlinePaymentBundle\Entity\Translation\PaymentMethodTranslation", mappedBy="translatable", cascade={"ALL"}, orphanRemoval=true)
  61. */
  62. private Collection $translations;
  63. private ?float $currentFee = null;
  64. public function getName(): ?string
  65. {
  66. return $this->getTitle();
  67. }
  68. public function setName(string $name): self
  69. {
  70. return $this->setTitle($name);
  71. }
  72. public function getCurrentFees(): ?float
  73. {
  74. if ($this->currentFee === null) {
  75. $this->setCurrentFees($this->getFees());
  76. }
  77. return $this->currentFee;
  78. }
  79. public function setCurrentFees($currentFee): PaymentMethod
  80. {
  81. $this->currentFee = $currentFee;
  82. return $this;
  83. }
  84. public function getTypeName(): ?string
  85. {
  86. if ($this->getType() instanceof PaymentMethodEnum) {
  87. return $this->getType()->name();
  88. }
  89. return null;
  90. }
  91. public function getType(): ?PaymentMethodEnum
  92. {
  93. return $this->type;
  94. }
  95. public function getObj(): array
  96. {
  97. return [
  98. "id" => (int)$this->getId(),
  99. "name" => (string)$this->getTitle(),
  100. "type" => (string)$this->getType()->name,
  101. "fees" => (float)$this->getCurrentFees(),
  102. ];
  103. }
  104. public function __construct()
  105. {
  106. $this->translations = new ArrayCollection();
  107. }
  108. function __toString()
  109. {
  110. return $this->getTitle();
  111. }
  112. public function getTitle(): ?string
  113. {
  114. return !$this->currentTranslation ? $this->title : $this->currentTranslation->getTitle();
  115. }
  116. public function getId(): ?int
  117. {
  118. return $this->id;
  119. }
  120. public function setTitle(string $title): self
  121. {
  122. $this->title = $title;
  123. return $this;
  124. }
  125. public function getNote(): ?string
  126. {
  127. return $this->note;
  128. }
  129. public function setNote(?string $note): self
  130. {
  131. $this->note = $note;
  132. return $this;
  133. }
  134. public function setType(string $type): self
  135. {
  136. $this->type = $type;
  137. return $this;
  138. }
  139. public function isActive(): ?bool
  140. {
  141. return $this->active;
  142. }
  143. public function setActive(bool $active): self
  144. {
  145. $this->active = $active;
  146. return $this;
  147. }
  148. public function getFees(): ?float
  149. {
  150. return $this->fees;
  151. }
  152. public function setFees(?float $fees): self
  153. {
  154. $this->fees = $fees;
  155. return $this;
  156. }
  157. /**
  158. * @return Collection<int, PaymentMethodTranslation>
  159. */
  160. public function getTranslations(): Collection
  161. {
  162. return $this->translations;
  163. }
  164. public function addTranslation(PaymentMethodTranslation $translation): self
  165. {
  166. if (!$this->translations->contains($translation)) {
  167. $this->translations->add($translation);
  168. $translation->setTranslatable($this);
  169. }
  170. return $this;
  171. }
  172. public function removeTranslation(PaymentMethodTranslation $translation): self
  173. {
  174. if ($this->translations->removeElement($translation)) {
  175. // set the owning side to null (unless already changed)
  176. if ($translation->getTranslatable() === $this) {
  177. $translation->setTranslatable(null);
  178. }
  179. }
  180. return $this;
  181. }
  182. public function getImage(): ?Image
  183. {
  184. return $this->image;
  185. }
  186. public function setImage(?Image $image): self
  187. {
  188. $this->image = $image;
  189. return $this;
  190. }
  191. }