Skip to content

Commit

Permalink
feat(ResolveConfig): resolve sub_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
domtra committed Nov 8, 2016
1 parent 1ac6870 commit f32ea4c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/ACFComposer/ResolveConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@

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

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

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

protected static function forNestedEntities($config) {
if(array_key_exists('sub_fields', $config)) {
$config['sub_fields'] = array_map('self::forField', $config['sub_fields']);
}
return $config;
}

protected static function validateConfig($config, $requiredAttributes = []) {
Expand Down
31 changes: 31 additions & 0 deletions tests/test-resolveConfigForField.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,35 @@ function testForFieldGetConfigFromFilter() {
$output = ResolveConfig::forField($config);
$this->assertEquals($someField, $output);
}

function testForFieldWithValidSubField() {
$subFieldConfig = [
'name' => 'subField',
'label' => 'Sub Field',
'type' => 'someType'
];
$config = [
'name' => 'someField',
'label' => 'Some Field',
'type' => 'someType',
'sub_fields' => [$subFieldConfig]
];
$output = ResolveConfig::forField($config);
$this->assertEquals($subFieldConfig, $output['sub_fields'][0]);
}

function testForFieldFailWithInvalidSubField() {
$subFieldConfig = [
'name' => 'subField',
'label' => 'Sub Field'
];
$config = [
'name' => 'someField',
'label' => 'Some Field',
'type' => 'someType',
'sub_fields' => [$subFieldConfig]
];
$this->expectException(Exception::class);
ResolveConfig::forField($config);
}
}
28 changes: 28 additions & 0 deletions tests/test-resolveConfigForLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,33 @@ function testForLayoutGetConfigFromFilter() {
$output = ResolveConfig::forLayout($config);
$this->assertEquals($someLayout, $output);
}
function testForLayoutWithValidSubField() {
$subFieldConfig = [
'name' => 'subField',
'label' => 'Sub Field',
'type' => 'someType'
];
$config = [
'name' => 'someLayout',
'label' => 'Some Layout',
'sub_fields' => [$subFieldConfig]
];
$output = ResolveConfig::forLayout($config);
$this->assertEquals($subFieldConfig, $output['sub_fields'][0]);
}

function testForLayoutFailWithInvalidSubField() {
$subFieldConfig = [
'name' => 'subField',
'label' => 'Sub Field'
];
$config = [
'name' => 'someLayout',
'label' => 'Some Layout',
'sub_fields' => [$subFieldConfig]
];
$this->expectException(Exception::class);
ResolveConfig::forLayout($config);
}

}

0 comments on commit f32ea4c

Please sign in to comment.