* @ORM\OneToMany( * targetEntity="Chill\MainBundle\Entity\GroupCenter", * mappedBy="permissionsGroup" * ) */ private Collection $groupCenters; /** * @var int * * @ORM\Id * @ORM\Column(name="id", type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private ?int $id = null; /** * @var string * * @ORM\Column(type="string", length=255, nullable=false, options={"default": ""}) */ private string $name = ''; /** * @var Collection * @ORM\ManyToMany( * targetEntity="Chill\MainBundle\Entity\RoleScope", * inversedBy="permissionsGroups", * cascade={ "persist" }) * @ORM\Cache(usage="NONSTRICT_READ_WRITE") */ private Collection $roleScopes; /** * PermissionsGroup constructor. */ public function __construct() { $this->roleScopes = new \Doctrine\Common\Collections\ArrayCollection(); $this->groupCenters = new \Doctrine\Common\Collections\ArrayCollection(); } public function addRoleScope(RoleScope $roleScope) { $this->roleScopes->add($roleScope); } /** * @return string[] */ public function getFlags() { return $this->flags; } /** * @return int */ public function getId() { return $this->id; } /** * @return string */ public function getName() { return $this->name; } /** * @return ArrayCollection|Collection */ public function getRoleScopes(): \Doctrine\Common\Collections\ArrayCollection|\Doctrine\Common\Collections\Collection { return $this->roleScopes; } /** * Test that a role scope is associated only once * with the permission group. */ public function isRoleScopePresentOnce(ExecutionContextInterface $context) { $roleScopesId = array_map( static fn (RoleScope $roleScope) => $roleScope->getId(), $this->getRoleScopes()->toArray() ); $countedIds = array_count_values($roleScopesId); foreach ($countedIds as $id => $nb) { if (1 < $nb) { $context->buildViolation('A permission is already present ' . 'for the same role and scope') ->addViolation(); } } } /** * @throws RuntimeException if the roleScope could not be removed. */ public function removeRoleScope(RoleScope $roleScope) { $result = $this->roleScopes->removeElement($roleScope); if (false === $result) { throw new RuntimeException(sprintf("The roleScope '%s' could not be removed, " . 'aborting.', spl_object_hash($roleScope))); } } /** * @return $this */ public function setFlags(array $flags) { $this->flags = $flags; return $this; } /** * @param $name * * @return $this */ public function setName($name) { $this->name = $name; return $this; } }