From 5c7046559cf34b70cb7c4308b45128aedbf904f2 Mon Sep 17 00:00:00 2001 From: Justin Grubbs Date: Wed, 6 Feb 2019 15:38:45 -0500 Subject: [PATCH 1/3] test(forFieldGroup): add test for nested filters --- tests/test-resolveConfigForFieldGroup.php | 60 +++++++++++++++++++++++ 1 file changed, 60 insertions(+) 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); + } } From 908d34676b95e8a86a560c4f98bf2dc99601c66f Mon Sep 17 00:00:00 2001 From: Justin Grubbs Date: Wed, 6 Feb 2019 15:42:04 -0500 Subject: [PATCH 2/3] fix(resolveConfig): flatten nested filters --- lib/ACFComposer/ResolveConfig.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/ACFComposer/ResolveConfig.php b/lib/ACFComposer/ResolveConfig.php index 544d725..b7cb77a 100644 --- a/lib/ACFComposer/ResolveConfig.php +++ b/lib/ACFComposer/ResolveConfig.php @@ -22,7 +22,13 @@ public static function forFieldGroup($config) $fields = self::forField($fieldConfig, [$keySuffix]); if (!self::isAssoc($fields)) { foreach ($fields as $field) { - array_push($carry, $field); + if (!self::isAssoc($field)) { + foreach ($field as $sub_field) { + array_push($carry, $sub_field); + } + } else { + array_push($carry, $field); + } } } else { array_push($carry, $fields); From 5a6b80f213816f7fe54a339658a1e321b4477384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Tr=C3=A4nklein?= Date: Thu, 10 Sep 2020 12:54:51 +0200 Subject: [PATCH 3/3] refactor(resolveConfig): extract pushSingleOrMultiple (cherry picked from commit 4f56904a4927429741be66141c059d20e43b2513) --- lib/ACFComposer/ResolveConfig.php | 34 +++++++++++++++++++------------ 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/ACFComposer/ResolveConfig.php b/lib/ACFComposer/ResolveConfig.php index b7cb77a..6b18101 100644 --- a/lib/ACFComposer/ResolveConfig.php +++ b/lib/ACFComposer/ResolveConfig.php @@ -20,19 +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) { - if (!self::isAssoc($field)) { - foreach ($field as $sub_field) { - array_push($carry, $sub_field); - } - } else { - array_push($carry, $field); - } - } - } else { - array_push($carry, $fields); - } + self::pushSingleOrMultiple($carry, $fields); return $carry; }, []); $output['location'] = array_map('self::mapLocation', $output['location']); @@ -261,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; + } }