src/Entity/RefillSetting.php line 20

  1. <?php
  2. namespace App\Entity;
  3. use App\Trait\EntityTrait;
  4. use App\Repository\RefillSettingRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use ApiPlatform\Action\NotFoundAction;
  9. use ApiPlatform\Metadata\Get;
  10. use ApiPlatform\Metadata\GetCollection;
  11. use ApiPlatform\Metadata\ApiResource;
  12. use Symfony\Component\Serializer\Annotation\MaxDepth;
  13. #[ApiResource(
  14.     normalizationContext: ['enable_max_depth'=>true],
  15. )]
  16. #[ORM\Entity(repositoryClassRefillSettingRepository::class)]
  17. class RefillSetting
  18. {
  19.     use EntityTrait;
  20.     #[ORM\Id]
  21.     #[ORM\GeneratedValue]
  22.     #[ORM\Column]
  23.     private ?int $id null;
  24.     #[MaxDepth(1)]
  25.     #[ORM\OneToOne(inversedBy'refillSetting'cascade: ['persist''remove'])]
  26.     private ?User $user null;
  27.     #[MaxDepth(1)]
  28.     #[ORM\OneToOne(inversedBy'refillSetting'cascade: ['persist''remove'])]
  29.     private ?Channel $channel null;
  30.     /**
  31.      * Number of months. The refill is valid for the current month and the next (periodMonths - 1) months
  32.      * The date is calculated in the controller (currently only RefillSettingController)
  33.      */
  34.     #[ORM\Column]
  35.     private ?int $periodMonths null;
  36.     #[MaxDepth(1)]
  37.     #[ORM\OneToMany(mappedBy'refillSetting'targetEntityRefillSettingEntity::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  38.     private Collection $refillSettingEntities;
  39.     public function __construct()
  40.     {
  41.         $this->refillSettingEntities = new ArrayCollection();
  42.     }
  43.     public function getId(): ?int
  44.     {
  45.         return $this->id;
  46.     }
  47.     public function getUser(): ?User
  48.     {
  49.         return $this->user;
  50.     }
  51.     public function setUser(?User $user): self
  52.     {
  53.         $this->user $user;
  54.         return $this;
  55.     }
  56.     public function getChannel(): ?Channel
  57.     {
  58.         return $this->channel;
  59.     }
  60.     public function setChannel(?Channel $channel): self
  61.     {
  62.         $this->channel $channel;
  63.         return $this;
  64.     }
  65.     public function getPeriod(): ?int
  66.     {
  67.         return $this->periodMonths;
  68.     }
  69.     public function setPeriod(int $periodMonths): self
  70.     {
  71.         $this->periodMonths $periodMonths;
  72.         return $this;
  73.     }
  74.     /**
  75.      * @return Collection<int, RefillSettingEntity>
  76.      */
  77.     public function getRefillSettingEntities(): Collection
  78.     {
  79.         return $this->refillSettingEntities;
  80.     }
  81.     public function addRefillSettingEntity(RefillSettingEntity $refillSettingEntity): self
  82.     {
  83.         if (!$this->refillSettingEntities->contains($refillSettingEntity)) {
  84.             $this->refillSettingEntities->add($refillSettingEntity);
  85.             $refillSettingEntity->setRefillSetting($this);
  86.         }
  87.         return $this;
  88.     }
  89.     public function removeRefillSettingEntity(RefillSettingEntity $refillSettingEntity): self
  90.     {
  91.         if ($this->refillSettingEntities->removeElement($refillSettingEntity)) {
  92.             // set the owning side to null (unless already changed)
  93.             if ($refillSettingEntity->getRefillSetting() === $this) {
  94.                 $refillSettingEntity->setRefillSetting(null);
  95.             }
  96.         }
  97.         return $this;
  98.     }
  99.     public function __toString()
  100.     {
  101.         $userOrChannel 'ERROR';   // Shows if user and channel are null
  102.         if ($this->user)    // User's toString prints the username
  103.             $userOrChannel $this->user;
  104.         else if ($this->channel)
  105.             $userOrChannel $this->channel;
  106.         
  107.         return $userOrChannel.' '.$this->periodMonths.' months';
  108.     }
  109. }