<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\TokenRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass=TokenRepository::class)
* @ORM\HasLifecycleCallbacks()
*/
#[ApiResource(
attributes: ['security' => "is_granted('ROLE_USER')"],
normalizationContext: ['groups' => ['cstoken:read']],
denormalizationContext: ['groups' => ['cstoken:write']],
collectionOperations: [
'get',
'post' => [
'security' => "is_granted('ROLE_ADMIN')",
],
],
itemOperations: [
'get',
'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
],
)]
class Token
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=128)
* @Groups({"cstoken:read", "cstoken:write"})
*/
private $name;
/**
* @ORM\OneToMany(targetEntity=SurveyCompanyToken::class, mappedBy="token", orphanRemoval=true)
* @Groups({"cstoken:read", "cstoken:write"})
*/
private $surveyCompanyTokens;
public function __construct()
{
$this->surveyCompanyTokens = new ArrayCollection();
}
public function __toString(): string
{
return $this->getName();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|SurveyCompanyToken[]
*/
public function getCompanyToken(): Collection
{
return $this->companyToken;
}
public function addCompanyToken(SurveyCompanyToken $companyToken): self
{
if (!$this->companyToken->contains($companyToken)) {
$this->companyToken[] = $companyToken;
$companyToken->setToken($this);
}
return $this;
}
public function removeCompanyToken(SurveyCompanyToken $companyToken): self
{
if ($this->companyToken->removeElement($companyToken)) {
// set the owning side to null (unless already changed)
if ($companyToken->getToken() === $this) {
$companyToken->setToken(null);
}
}
return $this;
}
}