Skip to content

Commit

Permalink
feat(ResolveConfig): generate key for fields and layouts, including n…
Browse files Browse the repository at this point in the history
…ested
  • Loading branch information
domtra committed Nov 8, 2016
1 parent f32ea4c commit cc670a6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
26 changes: 18 additions & 8 deletions lib/ACFComposer/ResolveConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,36 @@
use Exception;

class ResolveConfig {
public static function forField($config) {
return self::forEntity($config, ['name', 'label', 'type']);
public static function forField($config, $keySuffix = '') {
return self::forEntity($config, ['name', 'label', 'type'], $keySuffix);
}

public static function forLayout($config) {
return self::forEntity($config, ['name', 'label']);
public static function forLayout($config, $keySuffix = '') {
return self::forEntity($config, ['name', 'label'], $keySuffix);
}

protected static function forEntity($config, $requiredAttributes) {
protected static function forEntity($config, $requiredAttributes, $parentKeySuffix = '') {
if(is_string($config)) {
$config = apply_filters($config, null);
}
$output = self::validateConfig($config, $requiredAttributes);
$output = self::forNestedEntities($output);

$keySuffix = empty($parentKeySuffix) ? $output['name'] : "{$parentKeySuffix}_{$output['name']}";
$output['key'] = "field_{$keySuffix}";
$output = self::forNestedEntities($output, $keySuffix);
return $output;
}

protected static function forNestedEntities($config) {
protected static function forNestedEntities($config, $parentKeySuffix) {
if(array_key_exists('sub_fields', $config)) {
$config['sub_fields'] = array_map('self::forField', $config['sub_fields']);
$config['sub_fields'] = array_map(function($field) use ($parentKeySuffix){
return self::forField($field, $parentKeySuffix);
}, $config['sub_fields']);
}
if(array_key_exists('layouts', $config)) {
$config['layouts'] = array_map(function($layout) use ($parentKeySuffix){
return self::forLayout($layout, $parentKeySuffix);
}, $config['layouts']);
}
return $config;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/test-resolveConfigForField.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function testForFieldWithValidConfig() {
'type' => 'someType'
];
$output = ResolveConfig::forField($config);
$config['key'] = 'field_someField';
$this->assertEquals($config, $output);
}

Expand Down Expand Up @@ -66,6 +67,7 @@ function testForFieldGetConfigFromFilter() {
->once()
->andReturn($someField);
$output = ResolveConfig::forField($config);
$someField['key'] = "field_someField";
$this->assertEquals($someField, $output);
}

Expand All @@ -82,6 +84,7 @@ function testForFieldWithValidSubField() {
'sub_fields' => [$subFieldConfig]
];
$output = ResolveConfig::forField($config);
$subFieldConfig['key'] = 'field_someField_subField';
$this->assertEquals($subFieldConfig, $output['sub_fields'][0]);
}

Expand All @@ -99,4 +102,20 @@ function testForFieldFailWithInvalidSubField() {
$this->expectException(Exception::class);
ResolveConfig::forField($config);
}

function testForFieldWithValidLayout() {
$layoutConfig = [
'name' => 'someLayout',
'label' => 'Some Layout'
];
$config = [
'name' => 'someField',
'label' => 'Some Field',
'type' => 'someType',
'layouts' => [$layoutConfig]
];
$output = ResolveConfig::forField($config);
$layoutConfig['key'] = 'field_someField_someLayout';
$this->assertEquals($layoutConfig, $output['layouts'][0]);
}
}
3 changes: 3 additions & 0 deletions tests/test-resolveConfigForLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function testForLayoutWithValidConfig() {
'label' => 'Some Layout'
];
$output = ResolveConfig::forLayout($config);
$config['key'] = 'field_someLayout';
$this->assertEquals($config, $output);
}

Expand Down Expand Up @@ -52,6 +53,7 @@ function testForLayoutGetConfigFromFilter() {
->once()
->andReturn($someLayout);
$output = ResolveConfig::forLayout($config);
$someLayout['key'] = 'field_someLayout';
$this->assertEquals($someLayout, $output);
}
function testForLayoutWithValidSubField() {
Expand All @@ -66,6 +68,7 @@ function testForLayoutWithValidSubField() {
'sub_fields' => [$subFieldConfig]
];
$output = ResolveConfig::forLayout($config);
$subFieldConfig['key'] = 'field_someLayout_subField';
$this->assertEquals($subFieldConfig, $output['sub_fields'][0]);
}

Expand Down

0 comments on commit cc670a6

Please sign in to comment.