src/Entity/RefillSetting.php line 20
<?php
namespace App\Entity;
use App\Trait\EntityTrait;
use App\Repository\RefillSettingRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Action\NotFoundAction;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\ApiResource;
use Symfony\Component\Serializer\Annotation\MaxDepth;
#[ApiResource(
normalizationContext: ['enable_max_depth'=>true],
)]
#[ORM\Entity(repositoryClass: RefillSettingRepository::class)]
class RefillSetting
{
use EntityTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[MaxDepth(1)]
#[ORM\OneToOne(inversedBy: 'refillSetting', cascade: ['persist', 'remove'])]
private ?User $user = null;
#[MaxDepth(1)]
#[ORM\OneToOne(inversedBy: 'refillSetting', cascade: ['persist', 'remove'])]
private ?Channel $channel = null;
/**
* Number of months. The refill is valid for the current month and the next (periodMonths - 1) months
* The date is calculated in the controller (currently only RefillSettingController)
*/
#[ORM\Column]
private ?int $periodMonths = null;
#[MaxDepth(1)]
#[ORM\OneToMany(mappedBy: 'refillSetting', targetEntity: RefillSettingEntity::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $refillSettingEntities;
public function __construct()
{
$this->refillSettingEntities = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getChannel(): ?Channel
{
return $this->channel;
}
public function setChannel(?Channel $channel): self
{
$this->channel = $channel;
return $this;
}
public function getPeriod(): ?int
{
return $this->periodMonths;
}
public function setPeriod(int $periodMonths): self
{
$this->periodMonths = $periodMonths;
return $this;
}
/**
* @return Collection<int, RefillSettingEntity>
*/
public function getRefillSettingEntities(): Collection
{
return $this->refillSettingEntities;
}
public function addRefillSettingEntity(RefillSettingEntity $refillSettingEntity): self
{
if (!$this->refillSettingEntities->contains($refillSettingEntity)) {
$this->refillSettingEntities->add($refillSettingEntity);
$refillSettingEntity->setRefillSetting($this);
}
return $this;
}
public function removeRefillSettingEntity(RefillSettingEntity $refillSettingEntity): self
{
if ($this->refillSettingEntities->removeElement($refillSettingEntity)) {
// set the owning side to null (unless already changed)
if ($refillSettingEntity->getRefillSetting() === $this) {
$refillSettingEntity->setRefillSetting(null);
}
}
return $this;
}
public function __toString()
{
$userOrChannel = 'ERROR'; // Shows if user and channel are null
if ($this->user) // User's toString prints the username
$userOrChannel = $this->user;
else if ($this->channel)
$userOrChannel = $this->channel;
return $userOrChannel.' '.$this->periodMonths.' months';
}
}