If your roles are always going to transform to those specific string then you could add them to your user model and build a list of the roles in a getTransformedRoles() method like..
User.php
class User extends BaseUser implements UserInterface
{
const ROLE_PROFILE_ONE = 'Facturación y Entrega';
const ROLE_PROFILE_TWO = 'Envío';
const ROLE_ADMIN = 'Administrador';
const ROLE_USER = 'No posee roles asignados';
...
public function getTransformedRoles()
{
$transformed = array();
foreach ($this->getRoles() as $role) {
$role = strtoupper($role);
$const = sprintf('self::%s', $role);
// Do not add if is $role === ROLE_USER
if (FOS\UserBundle\Model\UserInterface::ROLE_DEFAULT === $role) {
continue;
}
if (!defined($const)) {
throw \Exception(sprintf('User does not have the role constant "%s" set', $role));
}
$transformed[] = constant($const);
)
// If no roles add self::ROLE_USER
if (empty($transformed)) {
$transformed[] = self::ROLE_USER;
}
return $transformed;
}
....
}
This would then return the fully transformed array of roles (using $user->getTransformedRoles()) where ever you may want them as opposed to just the single use case.
Alternatively you could do it with a service that does the same kind of transformation but with a set of varying roles and transformations that you could set via the config.yml.
Update
To use this as a service with your role transformations specified in your app/config/config you could do the following..
Acme/SomethingBundle/DependencyInjection/Configuration
$rootNode
->children()
->arrayNode('role_transformations')
->defaultValue(array())
->useAttributeAsKey('name')
->prototype('scalar')->cannotBeEmpty()->end()
->end()
->end()
->end();
Acme/SomethingBundle/DependencyInjection/AcmeSomethingExtension
$container->setParameter(
'acme.something.role_transformations',
$config['role_transformations']
);
Then in your app/config/config.yml
// For an empty array
role_transformations: ~ // Or not even at all, it defaults to an empty array
// For transformation
role_transformations:
ROLE_PROFILE_ONE: 'Facturación y Entrega'
ROLE_PROFILE_TWO: 'Envío'
ROLE_ADMIN: 'Administrador'
ROLE_USER: 'No posee roles asignados'
Create your service and inject the role_transformations
parameters:
acme.something.role_transformer.class: Acme/SomethingBundle/Transformer/RoleTransformer
services:
acme.something.role_transformer:
class: %acme.something.role_transformer.class%
arguments:
- %acme.something.role_transformations%
Then in your service file (Acme/SomethingBundle/Transformer/RoleTransformer)
class RoleTransformer implements RoleTransformerInterface
{
const ROLE_DEFAULT = 'ROLE_USER';
protected $rolesTransformations;
public function __construct(array $roleTransformations)
{
$this->roleTransformations = $roleTransformations;
}
public function getTransformedRolesForUser($user)
{
if (!method_exists($user, 'getRoles')) {
throw new \Exception('User object has no "getRoles" method');
// Alternatively you could add an interface to you user object specifying
// the getRoles method or depend on the Symfony security bundle and
// type hint Symfony\Component\Security\Core\User\UserInterface
}
return $this->getTransformedRoles($user->getRoles();
}
public function getTransformedRoles(array $roles)
{
$transformedRoles = array()
foreach ($roles as $role) {
$role = strtoupper($role);
if (null !== $transformedRole = $this->getTransformedRole($role)) {
$transformedRoles[] = $transformedRole;
}
}
return $transformedRoles;
}
public function getTransformedRole($role)
{
if (self::ROLE_USER === $role) {
return null;
}
if (!array_key_exists($role, $this->roleTransformations)) {
throw \Exception(sprintf(
'Role "%s" not found in acme.something.role_transformations', $role)
);
}
return $this->roleTransformations[$role];
}
}
This could be then be injected into a service or controller and used like
$transformer = $this->container->get('acme.something.role_transformer');
// Or injected via the DI
$roles = $transformer->getTransformedRolesForUser($user);
// For all of a users roles
$roles = $transformer->getTransformedRoles($user->getRoles());
// For an array of roles
$role = $transformer->getTransformedRole('ROLE_PROFILE_ONE');
// For a single role, or null if ROLE_USER
# [roles]: array(1) [0]: string (16) "ROLE_PROFILE_ONE"