src/Entity/Eg/Admin/UserGroup.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Eg\Admin;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. #[ORM\Entity]
  7. #[ORM\Table(name: 'eg_admin_group')]
  8. #[ORM\UniqueConstraint(name: 'uniq_42c78f2b5e237e06', columns: ['name'])]
  9. class UserGroup implements GroupInterface
  10. {
  11.     public const WORKERS_GROUP_NAMES = [
  12.         'GROUP_CITY_ADMINISTRATOR',
  13.         'GROUP_CONTROLLER'
  14.     ];
  15.     /**
  16.      * @var int
  17.      */
  18.     #[ORM\Id]
  19.     #[ORM\GeneratedValue(strategy: 'IDENTITY')]
  20.     #[ORM\Column(type: 'integer')]
  21.     private $id;
  22.     /**
  23.      * @var string
  24.      */
  25.     #[ORM\Column(name: 'name', type: 'string', length: 180, nullable: false)]
  26.     private $name;
  27.     /**
  28.      * @var array
  29.      */
  30.     #[ORM\Column(name: 'roles', type: 'array', nullable: false)]
  31.     private $roles;
  32.     /**
  33.      * @var string
  34.      */
  35.     #[ORM\Column(name: 'color', type: 'string', length: 7, nullable: true)]
  36.     private $color;
  37.     /**
  38.      * @var Collection
  39.      */
  40.     #[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'groups')]
  41.     private $users;
  42.     /**
  43.      * Constructor
  44.      */
  45.     public function __construct(?string $name = null)
  46.     {
  47.         $this->setName($name);
  48.         $this->users = new ArrayCollection();
  49.         $this->roles = [];
  50.     }
  51.     public function __toString()
  52.     {
  53.         return $this->getName();
  54.     }
  55.     public function getId(): int
  56.     {
  57.         return $this->id;
  58.     }
  59.     public function getName(): ?string
  60.     {
  61.         return $this->name;
  62.     }
  63.     public function setName(string $name): self
  64.     {
  65.         $this->name = $name;
  66.         return $this;
  67.     }
  68.     public function getRoles(): ?array
  69.     {
  70.         return $this->roles;
  71.     }
  72.     public function setRoles(array $roles): self
  73.     {
  74.         $this->roles = $roles;
  75.         return $this;
  76.     }
  77.     public function hasRole(string $role): bool
  78.     {
  79.         return in_array(strtoupper($role), $this->getRoles(), true);
  80.     }
  81.     public function addRole(string $role): self
  82.     {
  83.         if (!$this->hasRole($role)) {
  84.             $this->roles[] = strtoupper($role);
  85.         }
  86.         return $this;
  87.     }
  88.     public function removeRole(string $role): GroupInterface
  89.     {
  90.         if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
  91.             unset($this->roles[$key]);
  92.             $this->roles = array_values($this->roles);
  93.         }
  94.         return $this;
  95.     }
  96.     public function getColor(): ?string
  97.     {
  98.         return $this->color;
  99.     }
  100.     public function setColor(string $color): self
  101.     {
  102.         $this->color = $color;
  103.         return $this;
  104.     }
  105.     /**
  106.      * @return Collection<int, User>
  107.      */
  108.     public function getUsers(): Collection
  109.     {
  110.         return $this->users;
  111.     }
  112.     public function addUser(User $user): self
  113.     {
  114.         if (!$this->users->contains($user)) {
  115.             $this->users[] = $user;
  116.             $user->addGroup($this);
  117.         }
  118.         return $this;
  119.     }
  120.     public function removeUser(User $user): self
  121.     {
  122.         if ($this->users->removeElement($user)) {
  123.             $user->removeGroup($this);
  124.         }
  125.         return $this;
  126.     }
  127. }