Test Failed
Push — master ( c207a3...da2a21 )
by Steve
09:23 queued 12s
created

ConditionalField::shouldTransformValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace StoutLogic\AcfBuilder\Transform;
4
5
use StoutLogic\AcfBuilder\FieldsBuilder;
6
7
/**
8
 * Replace the field name in the 'field' key, with the key of the
9
 * actual field as defined by the Builder.
10
 */
11
class ConditionalField extends RecursiveTransform
12
{
13
    protected $keys = ['field'];
14
15
    /**
16
     * @param \StoutLogic\AcfBuilder\FieldsBuilder $builder
17
     */
18
    public function __construct(\StoutLogic\AcfBuilder\FieldsBuilder $builder)
19
    {
20
        parent::__construct($builder);
21
    }
22
23
    /**
24
     * @return \StoutLogic\AcfBuilder\FieldsBuilder
25
     */
26
    public function getBuilder()
27
    {
28
        return parent::getBuilder();
29
    }
30
31
    public function transformValue($value)
32
    {
33
        if ($this->getBuilder()->fieldExists($value)){
34
            return $this->getBuilder()->getField($value)->getKey();
35
        }
36
37
       return $value;
38
    }
39
40
    public function transformConfig($config)
41
    {
42
        if ($this->getBuilder()->fieldExists($config['field']) && $this->getBuilder()->getField($config['field'])->hasCustomKey()) {
43
            $config['_has_custom_key'] = true;
44
        } else if (!$this->getBuilder()->fieldExists($config['field'])) {
45
            $config['_field_does_not_exist'] = $config['field'];
46
        }
47
48
        return $config;
49
    }
50
51
    public function shouldTransformValue($key, $config)
52
    {
53
        return parent::shouldTransformValue($key, $config) && !(array_key_exists('_has_custom_key', $config) && $config['_has_custom_key'] === true);
54
    }
55
}
56