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

ConditionalField::shouldTransformValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 3
eloc 2
nc 3
nop 2
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