<?php
namespace App\Entity\Eg\Admin;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'eg_admin_group')]
#[ORM\UniqueConstraint(name: 'uniq_42c78f2b5e237e06', columns: ['name'])]
class UserGroup implements GroupInterface
{
public const WORKERS_GROUP_NAMES = [
'GROUP_CITY_ADMINISTRATOR',
'GROUP_CONTROLLER'
];
/**
* @var int
*/
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
#[ORM\Column(type: 'integer')]
private $id;
/**
* @var string
*/
#[ORM\Column(name: 'name', type: 'string', length: 180, nullable: false)]
private $name;
/**
* @var array
*/
#[ORM\Column(name: 'roles', type: 'array', nullable: false)]
private $roles;
/**
* @var string
*/
#[ORM\Column(name: 'color', type: 'string', length: 7, nullable: true)]
private $color;
/**
* @var Collection
*/
#[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'groups')]
private $users;
/**
* Constructor
*/
public function __construct(?string $name = null)
{
$this->setName($name);
$this->users = new ArrayCollection();
$this->roles = [];
}
public function __toString()
{
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;
}
public function getRoles(): ?array
{
return $this->roles;
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function hasRole(string $role): bool
{
return in_array(strtoupper($role), $this->getRoles(), true);
}
public function addRole(string $role): self
{
if (!$this->hasRole($role)) {
$this->roles[] = strtoupper($role);
}
return $this;
}
public function removeRole(string $role): GroupInterface
{
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
unset($this->roles[$key]);
$this->roles = array_values($this->roles);
}
return $this;
}
public function getColor(): ?string
{
return $this->color;
}
public function setColor(string $color): self
{
$this->color = $color;
return $this;
}
/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->addGroup($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
$user->removeGroup($this);
}
return $this;
}
}