<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\UserTagRepository;
use ApiPlatform\Core\Annotation\ApiFilter;
use App\Entity\Traits\TimestampableEntity;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Interfaces\ElasticUpdateInterface;
use App\Entity\Interfaces\ClientMappedInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ApiResource(
* attributes={"security"="is_granted('ROLE_ADMIN')"},
* normalizationContext={"groups"={"UserTag:Read"}, "skip_null_values"=false},
* denormalizationContext={"groups"={"UserTag:Write"}},
* collectionOperations={
* "post",
* "get"={"security"="is_granted('IS_AUTHENTICATED_FULLY')"},
* "get_for_public_page"={
* "route_name"="api_pub_user_tags_get_for_public_page_collection",
* "method"="GET",
* "security"="is_granted('PUBLIC_ACCESS')",
* "normalization_context"={"groups"={"UserTag:PRead"}, "skip_null_values"=false}
* }
* },
* itemOperations={
* "get"={"security"="is_granted('IS_CLIENT_OWNER', object)"},
* "put"={"security"="is_granted('IS_CLIENT_OWNER', object)"},
* "patch"={"security"="is_granted('IS_CLIENT_OWNER', object)"},
* "delete"={"security"="is_granted('IS_CLIENT_OWNER', object)"}
* }
* )
* @ApiFilter(SearchFilter::class, properties={"name": "partial", "client.id": "exact"})
* @ApiFilter(OrderFilter::class, properties={"id": "ASC", "name"})
* @ORM\Entity(repositoryClass=UserTagRepository::class)
* @ORM\Table(name="user_tag",indexes={@ORM\Index(name="user_tag_name_idx", columns={"name"})})
* @UniqueEntity(
* fields={"client", "name"},
* errorPath="name"
* )
*/
class UserTag implements ClientMappedInterface, ElasticUpdateInterface
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"UserTag:Read", "User:Read", "User:Me", "UserTag:PRead"})
*/
private $id;
/**
* @ORM\Column(type="string", length=64)
* @Groups({"User:Exp", "UserTag:Read", "UserTag:Write", "User:Read", "User:Write", "User:Me", "User:Attendee-List", "User:Change-Profile", "User:PRead", "UserTag:PRead"})
*
* @Assert\NotBlank(message="validation.userTag:name.notBlank")
* @Assert\Length(max=64, maxMessage="validation.userTag:name.max")
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity=User::class, inversedBy="userTags")
* @Groups({"UserTag:Write"})
*/
private $users;
/**
* @ORM\ManyToOne(targetEntity=Client::class)
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"UserTag:Read", "UserTag:Write", "User:Write", "User:Change-Profile"})
*/
private $client;
public function __construct()
{
$this->users = new ArrayCollection();
}
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|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
}
return $this;
}
public function removeUser(User $user): self
{
$this->users->removeElement($user);
return $this;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(?Client $client): self
{
$this->client = $client;
return $this;
}
public function getQueueInfo(): array
{
return [
'type' => 'UserTag',
'id' => $this->getId()
];
}
}