Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a helper for anonymous reference of object's key-values #337

Open
ppKrauss opened this issue Jun 14, 2018 · 0 comments
Open

Add a helper for anonymous reference of object's key-values #337

ppKrauss opened this issue Jun 14, 2018 · 0 comments

Comments

@ppKrauss
Copy link

ppKrauss commented Jun 14, 2018

Solving the problem of "no anonymous reference for objects"... Example:

$abc = ['foo','bar','baz'];
echo $m->render(  "\n\t 1: <ul>{{#.}}<li>{{.}}</li>{{/.}}</ul>\n",  $abc  );

works fine with anonymous reference for array. But when using a "dictionary" like associative array,

$abc = ['a'=>'foo','b'=>'bar','c'=>'baz'];

it is impossible with Mustache. A workaround is to use a function that transforms the $abc dictionary into keys and vals,

$abc_ugly = [  ['_key'=>'a', '_val'=>'foo'], 
          ['_key'=>'b', '_val'=>'bar'],  ['_key'=>'c', '_val'=>'baz']  ];
echo $m->render(
  "\n\t 2: <ul>{{#.}}<li>{{_key}}={{_val}}</li>{{/.}}</ul>\n",
  $abc_ugly  // ideal will be objs2list($abc) instead $abc_ugly
);

Another problem is to transform not the full root-dictionary, but only the object pointed by a path, example:

echo $m->render(
  "\n\t 3: xx={{xx}}  <ul>{{#yy}}<li>{{_key}}={{_val}}</li>{{/yy}}</ul>\n",
  objs2list(  ['xx'=>123,'yy'=>$abc],  ['yy']  )
);

and, ideal, is to add it as a internal method, to call in standard way $m->objs2list(). So, this is the suggestion.

Suggested helper

/**
 * Transforns objects into array of "_key" and "_val" objects.
 * The object can be pointed by a $path (array of reference-labels). 
 */
function objs2list($obj,$path=NULL) {
  $l = [];
  $flag = ($path && count($path));
  foreach(($flag? arrayPath($obj, $path): $obj) as $k=>$v) 
     $l[] = ['_key'=>$k,'_val'=>$v];
  if ($flag) {
    arrayPath($obj, $path, $l); return $obj;
  } else return $l;
}


/**
 * set/return a nested array value
 *
 * @param array $array the array to modify
 * @param array $path  the path to the value
 * @param mixed $value (optional) value to set
 *
 * @return mixed previous value
 * @see https://stackoverflow.com/a/36042293/287948
 */
function arrayPath(&$array, $path = array(), &$value = null)
{
    $args = func_get_args();
    $ref = &$array;
    foreach ($path as $key) {
        if (!is_array($ref)) {
            $ref = array();
        }
        $ref = &$ref[$key];
    }
    $prev = $ref;
    if (array_key_exists(2, $args)) {
        // value param was passed -> we're setting
        $ref = $value;  // set the value
    }
    return $prev;
}
@ppKrauss ppKrauss changed the title Add a helper to convert dictionary into array of dictionary of key-value Add a helper for anonymous reference of object's key-values Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant