diff --git a/lib/ACFComposer/ResolveConfig.php b/lib/ACFComposer/ResolveConfig.php index 70e9d95..5040ac6 100644 --- a/lib/ACFComposer/ResolveConfig.php +++ b/lib/ACFComposer/ResolveConfig.php @@ -5,6 +5,18 @@ use Exception; class ResolveConfig { + public static function forFieldGroup($config) { + $output = self::validateConfig($config, ['name', 'title', 'fields', 'location']); + + $keySuffix = $output['name']; + $output['key'] = "group_{$keySuffix}"; + $output = self::forNestedEntities($output, $keySuffix); + $output['fields'] = array_map(function($field) use ($keySuffix){ + return self::forField($field, $keySuffix); + }, $output['fields']); + return $output; + } + public static function forField($config, $keySuffix = '') { return self::forEntity($config, ['name', 'label', 'type'], $keySuffix); } diff --git a/tests/test-resolveConfigForFieldGroup.php b/tests/test-resolveConfigForFieldGroup.php new file mode 100644 index 0000000..ac7377d --- /dev/null +++ b/tests/test-resolveConfigForFieldGroup.php @@ -0,0 +1,35 @@ + 'someField', + 'label' => 'Some Field', + 'type' => 'someType' + ]; + $locationConfig = [ + 'param' => 'someParam', + 'operator' => 'someOperator', + 'value' => 'someValue' + ]; + $config = [ + 'name' => 'someGroup', + 'title' => 'Some Group', + 'fields' => [$fieldConfig], + 'location' => [ + [$locationConfig] + ] + ]; + $output = ResolveConfig::forFieldGroup($config); + $fieldConfig['key'] = 'field_someGroup_someField'; + $config['key'] = 'group_someGroup'; + $config['fields'] = [$fieldConfig]; + $this->assertEquals($config, $output); + } +}