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));
}