Prepares the role collection

@param array $roles

@return array
432851
public function prepareRoleCollection (array $roles)
    {
        $roleCollection = [];
        $roleHelper = new RoleNameHelper();

        foreach ($roles as $role => $configuration)
        {
            try
            {
                // normalize role name
                $roleName = $roleHelper->normalizeRoleName($role);

                // check for duplicate role definitions
                if (array_key_exists($roleName, $roleCollection))
                {
                    $addition = ($roleName !== $role) ? " (normalized from “{$role}”)" : "";
                    throw new DuplicateRoleDefinitionException("The role “{$roleName}”{$addition} has multiple definitions. Keep in mind that all role names are normalized to a common “ROLE_…” format.");
                }

                $roleCollection[$roleName] = $configuration;
            }
            catch (\InvalidArgumentException $e)
            {
                throw new EmptyRoleNameException("Role name could not be normalized: {$e->getMessage()}.", 0, $e);
            }
        }

        return $this->prepareIncludedRoles($roleCollection);
    }
1211799
public function eachRole(array $roles, callable $callback)
    {
        $roles = array_flatten($roles);

        foreach ($roles as $role) {
            if ($role = $this->prepareRole($role)) {
                $callback($role);    
            }
        }
    }
1038821
public function setRoles($roles)
    {
        $widget = $this->widget;
        $newRoles = [];
        foreach ($this->availableRoles as $role) {
            $newRoles[$role] = in_array($role, $roles);
        }
        $widget->setOption('roles', $newRoles);
    }
1292153
protected function convert_array_format($roles, $search) {
        if (empty($roles)) {
            $roles = array();
        }
        $groupedusers = array();
        foreach ($roles as $role) {
            if ($search) {
                $a = new stdClass;
                $a->role = $role->name;
                $a->search = $search;
                $groupname = get_string('matchingsearchandrole', '', $a);
            } else {
                $groupname = $role->name;
            }
            $groupedusers[$groupname] = $role->users;
            foreach ($groupedusers[$groupname] as &$user) {
                unset($user->roles);
                $user->fullname = fullname($user);
                if (!empty($user->component)) {
                    $user->infobelow = get_string('addedby', 'group',
                        get_string('pluginname', $user->component));
                }
            }
        }
        return $groupedusers;
    }
481385
protected function isOne(array $roles)
    {
        foreach ($roles as $role) {
            if ($this->hasRole($role)) {
                return true;
            }
        }

        return false;
    }
1241251
public static function formatRoles(array $roles, $force = false): array
    {
        if ($force || version_compare(Kernel::VERSION, '4.3', '<')) {
            $roles = array_map(static function ($role) {
                return !$role instanceof Role ? new Role((string) $role) : $role;
            }, $roles);
        }

        return $roles;
    }
865808
public function translateToRoleIds($roles)
    {
        if (!is_array($roles)) {
            if ($roles) {
                $roles = explode(",", $roles);
            } else {
                $roles = array();
            }
        }

        $lookup = array_flip($this->_roleTranslations);

        foreach ($roles as $key => $role) {
            if (isset($lookup[$role])) {
                $roles[$key] = $lookup[$role];
            }
        }

        return $roles;
    }
1219179
private function getSpecifiedEnvironmentRoles(array $roles)
    {
        $environmentRoles = [];
        $project = $this->getSelectedProject();
        $environments = $this->api()->getEnvironments($project);
        foreach ($roles as $role) {
            if (strpos($role, ':') === false) {
                continue;
            }
            list($id, $role) = explode(':', $role, 2);
            $role = $this->validateEnvironmentRole($role);
            // Match environment IDs by wildcard.
            if (strpos($id, '%') !== false) {
                $pattern = '/^' . str_replace('%', '.*', preg_quote($id)) . '$/';
                $matched = preg_grep($pattern, array_keys($environments));
                if (empty($matched)) {
                    throw new InvalidArgumentException('No environment IDs match: ' . $id);
                }
                foreach ($matched as $environmentId) {
                    $environmentRoles[$environmentId] = $role;
                }
                continue;
            }
            if (!$this->api()->getEnvironment($id, $project)) {
                throw new InvalidArgumentException('Environment not found: ' . $id);
            }
            $environmentRoles[$id] = $role;
        }

        return $environmentRoles;
    }
303776
public function roleLoadPermissions($roles)
    {
        if (!$roles) {
            return false;
        }
        $multi = is_array($roles);
        if (!$multi) {
            $roles = [$roles];
        }
        $permissions_ids = [];

        foreach ($roles as $role) {
            if (!is_array($role->_permission_ids)) {
                $role->_permission_ids = explode(',', $role->_permission_ids);
            }
            $permissions_ids = array_merge($permissions_ids, $role->_permission_ids);
        }
        $perms = $this->permissionFetchById($permissions_ids);
        foreach ($perms as $perm) {
            foreach ($roles as $role) {
                if (in_array($perm->permission_id, $role->_permission_ids)) {
                    $role->addPermission($perm);
                    continue;
                }
            }
        }
        return true;
    }
168049
public function whereRoleIn(array $roles)
    {
        if (0 < $count = count($roles)) {
            $this->leftJoinRights = true;
            $eol = PHP_EOL;
            $clause = "{$eol}({$eol}";

            foreach ($roles as $i => $role) {
                $role = $roles[$i] instanceof RoleInterface ? $roles[$i]->getRole() : $roles[$i];
                $clause .= $i > 0 ? '    OR ' : '    ';
                $clause .= "rightRole.name = :role_{$i}{$eol}";
                $this->parameters[":role_{$i}"] = $role;
            }

            $this->addWhereClause($clause . ')');
        }

        return $this;
    }
1105980
public function getUsersWithRole(Role $role): array
    {
        $query = $this->getRepository()->createQueryBuilder('u')
            ->select('u')
            ->where(':role MEMBER OF u.roles')
            ->setParameter('role', $role)
            ->getQuery();

        return $query->getResult() ?? [];
    }
876229
public function getGroups($role = 'all')
	{
		static $groups;

		if (!isset($groups))
		{
			$groups = array(
				'applicants' => array(),
				'invitees'   => array(),
				'members'    => array(),
				'managers'   => array(),
				'all'        => array()
			);
			$groups['all'] = Helper::getGroups($this->get('uidNumber'), 'all', 1);

			if ($groups['all'])
			{
				foreach ($groups['all'] as $item)
				{
					if ($item->registered)
					{
						if (!$item->regconfirmed)
						{
							$groups['applicants'][] = $item;
						}
						else
						{
							if ($item->manager)
							{
								$groups['managers'][] = $item;
							}
							else
							{
								$groups['members'][] = $item;
							}
						}
					}
					else
					{
						$groups['invitees'][] = $item;
					}
				}
			}
		}

		if ($role)
		{
			return (isset($groups[$role])) ? $groups[$role] : false;
		}

		return $groups;
	}
963210
protected function checkRoles(array $roles)
    {
        $checkedRoles = array();
        foreach ($roles as $role) {
            if (true === is_string($role) && false === in_array($role, $checkedRoles)) {
                $checkedRoles[] = (string)$role;
            }
        }
        return $checkedRoles;
    }
1282436
private static function buildOrganizationRoles(array $roles, OrganizationInterface $org)
    {
        $orgName = $org->getName();

        if ($org instanceof RoleableInterface) {
            $existingRoles = [];

            foreach ($roles as $role) {
                $existingRoles[] = $role;
            }

            foreach ($org->getRoles() as $orgRole) {
                $roleName = RoleUtil::formatName($orgRole);

                if (!\in_array($roleName, $existingRoles, true)) {
                    $roles[] = $roleName.'__'.$orgName;
                    $existingRoles[] = $roleName;
                }
            }
        }

        return $roles;
    }
956915
public function switchRoleContext($roles)
    {
        if (is_string($roles)) {
            $roles = [$roles];
        }
        if (is_array($roles)) {
            $roleBucket = new RoleBucket($this);
            foreach ($roles as $role) {
                $roleBucket->add($role);
            }
        } else {
            $roleBucket = $roles;
        }
        $context = new RoleSetContext($roleBucket);
        $this->registerContext($context);
        $this->currentContext = $context->getId();
        return $context;
    }
966964
public function hasRoles($roles)
    {
        if (! is_null($this->getUser())) {
            return $this->getUser()->hasRoles($roles);
        }

        return false;
    }
977645
protected function get_roles( $roles ) {
		if ( empty($roles) ) {
			// @codeCoverageIgnoreStart
			return null;
			// @codeCoverageIgnoreEnd
		}

		$wp_roles = wp_roles();
		$names    = $wp_roles->get_names();

		$values = array();

		foreach ( $roles as $role ) {
			$name = $role;
			if ( isset($names[ $role ]) ) {
				$name = translate_user_role($names[ $role ]);
			}
			$values[ $role ] = $name;
		}

		return $values;
	}