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

fix(resolveConfig): flatten nested filters #17

Merged
merged 4 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}