ZfcRbac\Service\RoleService::convertRoles PHP Method

convertRoles() protected method

Convert the roles (potentially strings) to concrete RoleInterface objects using role provider
protected convertRoles ( array | Traversabl\Traversable $roles ) : Rbac\Role\RoleInterface[]
$roles array | Traversabl\Traversable
return Rbac\Role\RoleInterface[]
    protected function convertRoles($roles)
    {
        if ($roles instanceof Traversable) {
            $roles = iterator_to_array($roles);
        }
        $collectedRoles = [];
        $toCollect = [];
        foreach ((array) $roles as $role) {
            // If it's already a RoleInterface, nothing to do as a RoleInterface contains everything already
            if ($role instanceof RoleInterface) {
                $collectedRoles[] = $role;
                continue;
            }
            // Otherwise, it's a string and hence we need to collect it
            $toCollect[] = (string) $role;
        }
        // Nothing to collect, we don't even need to hit the (potentially) costly role provider
        if (empty($toCollect)) {
            return $collectedRoles;
        }
        return array_merge($collectedRoles, $this->roleProvider->getRoles($toCollect));
    }