<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\RoleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: RoleRepository::class)]
#[UniqueEntity(fields:'label', message:'Le nom existe déjà')]
#[ApiResource(
normalizationContext:['groups'=>['role:get']]
)]
class Role
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['role:get','admin:read'])]
private $id;
#[ORM\Column(type: 'string', length: 80)]
#[Groups(['role:get','admin:read'])]
private $label;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups(['role:get','admin:read'])]
private $description;
#[ORM\ManyToMany(targetEntity: Permission::class)]
#[Groups(['role:get','admin:read'])]
private $permissions;
public function __construct()
{
$this->permissions = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return Collection<int, Permission>
*/
public function getPermissions(): Collection
{
return $this->permissions;
}
public function addPermission(Permission $permission): self
{
if (!$this->permissions->contains($permission)) {
$this->permissions[] = $permission;
}
return $this;
}
public function removePermission(Permission $permission): self
{
$this->permissions->removeElement($permission);
return $this;
}
}