DefaultRoleEvent.php in Organic groups 8
File
src/Event/DefaultRoleEvent.php
View source
<?php
declare (strict_types=1);
namespace Drupal\og\Event;
use Drupal\og\Entity\OgRole;
use Symfony\Component\EventDispatcher\Event;
class DefaultRoleEvent extends Event implements DefaultRoleEventInterface {
protected $roles = [];
public function getRole($name) {
if (!isset($this->roles[$name])) {
throw new \InvalidArgumentException("The '{$name}' role does not exist.'");
}
return $this->roles[$name];
}
public function getRoles() {
return $this->roles;
}
public function addRole(OgRole $role) {
$this
->validate($role);
if (array_key_exists($role
->getName(), $this->roles)) {
throw new \InvalidArgumentException("The '{$role->getName()}' role already exists.");
}
$this->roles[$role
->getName()] = $role;
}
public function addRoles(array $roles) {
foreach ($roles as $role) {
$this
->addRole($role);
}
}
public function setRole(OgRole $role) {
$this
->validate($role);
$this
->deleteRole($role
->getName());
$this
->addRole($role);
}
public function setRoles(array $roles) {
foreach ($roles as $properties) {
$this
->setRole($properties);
}
}
public function deleteRole($name) {
unset($this->roles[$name]);
}
public function hasRole($name) {
return isset($this->roles[$name]);
}
public function offsetGet($key) {
return $this
->getRole($key);
}
public function offsetSet($key, $role) {
$this
->validate($role);
if ($role
->getName() !== $key) {
throw new \InvalidArgumentException('The key and the "name" property of the role should be identical.');
}
$this
->setRole($role);
}
public function offsetUnset($key) {
$this
->deleteRole($key);
}
public function offsetExists($key) {
return $this
->hasRole($key);
}
public function getIterator() {
return new \ArrayIterator($this->roles);
}
protected function validate(OgRole $role) {
if (empty($role
->getName())) {
throw new \InvalidArgumentException('Role name is required.');
}
}
}