Skip to content

Commit

Permalink
fix(resolveConfig): flatten nested filters (#17)
Browse files Browse the repository at this point in the history
* test(forFieldGroup): add test for nested filters

* fix(resolveConfig): flatten nested filters

* refactor(resolveConfig): extract pushSingleOrMultiple

Co-authored-by: Dominik Tränklein <dominik@bleech.de>
  • Loading branch information
jGRUBBS and domtra authored Sep 11, 2020
1 parent 600b1ae commit da63e97
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lib/ACFComposer/ResolveConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ public static function forFieldGroup($config)
$output['key'] = "group_{$keySuffix}";
$output['fields'] = array_reduce($config['fields'], function ($carry, $fieldConfig) use ($keySuffix) {
$fields = self::forField($fieldConfig, [$keySuffix]);
if (!self::isAssoc($fields)) {
foreach ($fields as $field) {
array_push($carry, $field);
}
} else {
array_push($carry, $fields);
}
self::pushSingleOrMultiple($carry, $fields);
return $carry;
}, []);
$output['location'] = array_map('self::mapLocation', $output['location']);
Expand Down Expand Up @@ -255,4 +249,24 @@ protected static function isAssoc(array $arr)
}
return array_keys($arr) !== range(0, count($arr) - 1);
}

/**
* Adds a single or multiple elements to an array.
*
* @param array &$arr Array to add to.
* @param array $fields Single or multiple associative arrays to add to $arr.
*
* @return boolean
*/
protected static function pushSingleOrMultiple(array &$carry, array $fields)
{
if (!self::isAssoc($fields)) {
foreach ($fields as $field) {
self::pushSingleOrMultiple($carry, $field);
}
} else {
array_push($carry, $fields);
}
return $carry;
}
}
60 changes: 60 additions & 0 deletions tests/test-resolveConfigForFieldGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,64 @@ public function testForFieldGroupFailsWithInvalidLocation()
$this->expectException(Exception::class);
ResolveConfig::forFieldGroup($config);
}

public function testForFieldGroupWithNestedFilters()
{
$subFieldFilter = 'ACFComposer/Layout/subFieldLayout/SubFields';
$subFieldConfig = [
[
'name' => 'nestedSubField',
'label' => 'Nested Sub Field',
'type' => 'text'
]
];
$someLayoutFilter = 'ACFComposer/Layout/someLayout';
$someLayoutConfig = [
[
'name' => 'SubField',
'label' => 'Sub Field',
'type' => 'text'
],
$subFieldFilter
];
$masterLayout = [
'name' => 'masterLayout',
'title' => 'Master Layout',
'fields' => [
$someLayoutFilter
],
'location' => [
[
[
'param' => 'someParam',
'operator' => 'someOperator',
'value' => 'someValue'
]
]
]
];
Filters::expectApplied($subFieldFilter)
->once()
->andReturn($subFieldConfig);
Filters::expectApplied($someLayoutFilter)
->once()
->andReturn($someLayoutConfig);
$output = ResolveConfig::forFieldGroup($masterLayout);
$resolved_subfields = $output['fields'];
$expectedResult = [
[
'name' => 'SubField',
'label' => 'Sub Field',
'type' => 'text',
'key' => 'field_masterLayout_SubField'
],
[
'name' => 'nestedSubField',
'label' => 'Nested Sub Field',
'type' => 'text',
'key' => 'field_masterLayout_nestedSubField'
]
];
$this->assertEquals($expectedResult, $resolved_subfields);
}
}

0 comments on commit da63e97

Please sign in to comment.