php - Format the array output to display friendly names -
php - Format the array output to display friendly names -
i've function returns json response:
public function getusersaction() { $response = array(); $em = $this->getdoctrine()->getmanager(); $entities = $em->getrepository("userbundle:user")->findall(); $roles = array( "role_profile_one" => "facturación y entrega", "role_profile_two" => "envío", "role_admin" => "administrador", "role_user" => "no posee roles asignados" ); $users = array(); foreach ($entities $entity) { $user = array(); $user[] = $entity->getusername(); $user[] = $entity->getemailcanonical(); $user[] = $entity->getroles(); $user[] = $entity->getgroupnames() != null ? $entity->getgroupnames() : "-"; $users[] = $user; } $response[ 'data' ] = $users; homecoming new jsonresponse($response); }
i access through twig template via ajax call, that's work! since getroles()
user
(fosuser) model homecoming db value per example: role_profile_one
, role_admin
, role_user
, how format output display friendly names based on $roles
defined array? tried foreach
loop within foreach ($entities $entity) ...
, set new array nested calls big since apache goes down. help?
trying more clear input/output example
well, have input (what function returns, made ladybug_dump($entities)
output):
array(6) [0]: object(tanane\userbundle\entity\user) >> properties ... # [roles]: array(1) [0]: string (16) "role_profile_one" ... [1]: object(tanane\userbundle\entity\user) >> properties ... # [roles]: array(2) [0]: string (16) "role_profile_two" [1]: string (16) "role_profile_one" ...
when access on twig, @ template, got:
user1 role_profile_one user2 role_profile_two, role_profile_one
but need output instead:
user1 facturación y entrega user2 envío, facturación y entrega
it's more clear now?
if roles going transform specific string add together them user model , build list of roles in 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() $role) { $role = strtoupper($role); $const = sprintf('self::%s', $role); // not add together if $role === role_user if (fos\userbundle\model\userinterface::role_default === $role) { continue; } if (!defined($const)) { throw \exception(sprintf('user not have role constant "%s" set', $role)); } $transformed[] = constant($const); ) // if no roles add together self::role_user if (empty($transformed)) { $transformed[] = self::role_user; } homecoming $transformed; } .... }
this homecoming transformed array of roles (using $user->gettransformedroles()
) ever may want them opposed single utilize case.
alternatively service same kind of transformation set of varying roles , transformations set via config.yml.
update
to utilize service role transformations specified in app/config/config
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 app/config/config.yml
// empty array role_transformations: ~ // or not @ all, defaults empty array // 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 service , inject 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 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 add together interface user object specifying // getroles method or depend on symfony security bundle , // type hint symfony\component\security\core\user\userinterface } homecoming $this->gettransformedroles($user->getroles(); } public function gettransformedroles(array $roles) { $transformedroles = array() foreach ($roles $role) { $role = strtoupper($role); if (null !== $transformedrole = $this->gettransformedrole($role)) { $transformedroles[] = $transformedrole; } } homecoming $transformedroles; } public function gettransformedrole($role) { if (self::role_user === $role) { homecoming null; } if (!array_key_exists($role, $this->roletransformations)) { throw \exception(sprintf( 'role "%s" not found in acme.something.role_transformations', $role) ); } homecoming $this->roletransformations[$role]; } }
this injected service or controller , used like
$transformer = $this->container->get('acme.something.role_transformer'); // or injected via di $roles = $transformer->gettransformedrolesforuser($user); // of users roles $roles = $transformer->gettransformedroles($user->getroles()); // array of roles $role = $transformer->gettransformedrole('role_profile_one'); // single role, or null if role_user
php arrays symfony2
Comments
Post a Comment