Skip to content

Commit

Permalink
automatically handle keyed arrays
Browse files Browse the repository at this point in the history
or even, mixed arrays
  • Loading branch information
AD7six committed Nov 14, 2014
1 parent 4d2e5d2 commit c06be86
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Template/Bake/Element/array_property.ctp
Expand Up @@ -7,4 +7,4 @@ use Cake\Utility\Inflector;
*
* @var array
*/
public $<%= $name %> = [<%= $this->Bake->stringifyList($value, ['keys' => true]) %>];
public $<%= $name %> = [<%= $this->Bake->stringifyList($value) %>];
15 changes: 5 additions & 10 deletions src/View/Helper/BakeHelper.php
Expand Up @@ -55,21 +55,16 @@ public function arrayProperty($name, $value, $options = []) {
*/
public function stringifyList($list, $options = []) {
$options += [
'keys' => false,
'indent' => 2,
'callback' => function ($v) {
return "'$v'";
},
'indent' => 2
];

if (!$list) {
return '';
}

$wrapped = array_map($options['callback'], $list);

if (!empty($option['keys'])) {
foreach($wrapped as $k => &$v) {
foreach($list as $k => &$v) {
$v = "'$v'";
if (!is_numeric($k)) {
$v = "'$k' => $v";
}
}
Expand All @@ -82,7 +77,7 @@ public function stringifyList($list, $options = []) {
$end = "\n" . str_repeat("\t", $options['indent'] - 1);
}

return $start . implode($join, $wrapped) . $end;
return $start . implode($join, $list) . $end;
}

public function aliasExtractor($modelObj, $assoc) {
Expand Down

0 comments on commit c06be86

Please sign in to comment.