From da63e9706b8a6de464b0022ae57f0ffada7c5525 Mon Sep 17 00:00:00 2001 From: Justin Grubbs Date: Fri, 11 Sep 2020 04:37:12 -0400 Subject: [PATCH] fix(resolveConfig): flatten nested filters (#17) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test(forFieldGroup): add test for nested filters * fix(resolveConfig): flatten nested filters * refactor(resolveConfig): extract pushSingleOrMultiple Co-authored-by: Dominik Tränklein --- lib/ACFComposer/ResolveConfig.php | 28 ++++++++--- tests/test-resolveConfigForFieldGroup.php | 60 +++++++++++++++++++++++ 2 files changed, 81 insertions(+), 7 deletions(-) diff --git a/lib/ACFComposer/ResolveConfig.php b/lib/ACFComposer/ResolveConfig.php index 544d725..6b18101 100644 --- a/lib/ACFComposer/ResolveConfig.php +++ b/lib/ACFComposer/ResolveConfig.php @@ -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']); @@ -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; + } } diff --git a/tests/test-resolveConfigForFieldGroup.php b/tests/test-resolveConfigForFieldGroup.php index 3851a01..c0d3c15 100644 --- a/tests/test-resolveConfigForFieldGroup.php +++ b/tests/test-resolveConfigForFieldGroup.php @@ -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); + } }