<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\RoleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: RoleRepository::class)]
#[ApiResource(
normalizationContext: ['groups' => ['role:read']],
denormalizationContext: ['groups' => ['role:write']],
order: ['name' => 'ASC'],
)]
class Role
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['role:read', 'role:write', 'user:read'])]
private ?int $id = null;
#[Groups(['role:read', 'role:write', 'user:read'])]
#[ORM\Column(length: 255)]
private ?string $name = null;
#[Groups(['role:read', 'role:write', 'user:read'])]
#[ORM\Column(length: 255)]
private ?string $label = null;
#[Groups(['role:read', 'role:write', 'user:read'])]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description = null;
/**
* @var Collection<int, UserRole>
*/
#[ORM\OneToMany(mappedBy: 'role', targetEntity: UserRole::class, cascade:['persist', 'remove'])]
private Collection $userRoles;
/**
* @var Collection<int, Permission>
*/
#[ORM\OneToMany(mappedBy: 'role', targetEntity: Permission::class, cascade:['remove'])]
private Collection $permissions;
#[Groups(['role:read', 'role:write', 'user:read'])]
#[ORM\Column(nullable: true)]
private ?bool $loginAdminPanel = null;
/**
* @var Collection<int, Menu>
*/
#[Groups(['role:read', 'role:write', 'user:read'])]
#[ORM\ManyToMany(targetEntity: Menu::class, mappedBy: 'role')]
private Collection $menus;
public function __construct()
{
$this->userRoles = new ArrayCollection();
$this->permissions = new ArrayCollection();
$this->menus = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): static
{
$this->label = $label;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
/**
* @return Collection<int, UserRole>
*/
public function getUserRoles(): Collection
{
return $this->userRoles;
}
public function addUserRole(UserRole $userRole): static
{
if (!$this->userRoles->contains($userRole)) {
$this->userRoles->add($userRole);
$userRole->setRole($this);
}
return $this;
}
public function removeUserRole(UserRole $userRole): static
{
if ($this->userRoles->removeElement($userRole)) {
// set the owning side to null (unless already changed)
if ($userRole->getRole() === $this) {
$userRole->setRole(null);
}
}
return $this;
}
/**
* @return Collection<int, Permission>
*/
public function getPermissions(): Collection
{
return $this->permissions;
}
public function addPermission(Permission $permission): static
{
if (!$this->permissions->contains($permission)) {
$this->permissions->add($permission);
$permission->setRole($this);
}
return $this;
}
public function removePermission(Permission $permission): static
{
if ($this->permissions->removeElement($permission)) {
// set the owning side to null (unless already changed)
if ($permission->getRole() === $this) {
$permission->setRole(null);
}
}
return $this;
}
public function isLoginAdminPanel(): ?bool
{
return $this->loginAdminPanel;
}
public function setLoginAdminPanel(?bool $loginAdminPanel): static
{
$this->loginAdminPanel = $loginAdminPanel;
return $this;
}
/**
* @return Collection<int, Menu>
*/
public function getMenus(): Collection
{
return $this->menus;
}
public function addMenu(Menu $menu): static
{
if (!$this->menus->contains($menu)) {
$this->menus->add($menu);
$menu->addRole($this);
}
return $this;
}
public function removeMenu(Menu $menu): static
{
if ($this->menus->removeElement($menu)) {
$menu->removeRole($this);
}
return $this;
}
}