Passed
Branch develop (612b6d)
by Steve
04:05
created

ConditionalField   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 8
c 1
b 1
f 0
lcom 0
cbo 3
dl 0
loc 39
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getBuilder() 0 4 1
A transformValue() 0 4 1
A transformConfig() 0 8 2
A shouldTransformValue() 0 4 3
1
<?php
2
3
namespace StoutLogic\AcfBuilder\Transform;
4
5
/**
6
 * Replace the field name in the 'field' key, with the key of the
7
 * actual field as defined by the Builder.
8
 */
9
class ConditionalField extends RecursiveTransform
10
{
11
    protected $keys = ['field'];
12
13
    /**
14
     * @param \StoutLogic\AcfBuilder\FieldsBuilder $builder
15
     */
16
    public function __construct(\StoutLogic\AcfBuilder\FieldsBuilder $builder)
17
    {
18
        parent::__construct($builder);
19
    }
20
21
    /**
22
     * @return \StoutLogic\AcfBuilder\FieldsBuilder
23
     */
24
    public function getBuilder()
25
    {
26
        return parent::getBuilder();
27
    }
28
29
    public function transformValue($value)
30
    {
31
        return $this->getBuilder()->getField($value)->getKey();
32
    }
33
34
    public function transformConfig($config)
35
    {
36
        if ($this->getBuilder()->getField($config['field'])->hasCustomKey()) {
37
            $config['_has_custom_key'] = true;
38
        }
39
40
        return $config;
41
    }
42
43
    public function shouldTransformValue($key, $config)
44
    {
45
        return parent::shouldTransformValue($key, $config) && !(array_key_exists('_has_custom_key', $config) && $config['_has_custom_key'] === true);
46
    }
47
}
48