Completed
Branch develop (6833e5)
by Steve
04:32
created

RecursiveTransform   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 57
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeys() 0 4 1
A transform() 0 14 3
A shouldRecurse() 0 4 1
transformValue() 0 1 ?
1
<?php
2
3
namespace StoutLogic\AcfBuilder\Transform;
4
5
/**
6
 * Transform applies to all leafs in array, at specific keys
7
 * using array_walk_recursive
8
 */
9
abstract class RecursiveTransform extends Transform
10
{
11
    /**
12
     * Define a list of array keys `transformValue` should apply to.
13
     * @var array
14
     */
15
    protected $keys = [];
16
17
    /**
18
     * @return array
19
     */
20
    public function getKeys()
21
    {
22
        return $this->keys;
23
    }
24
25
    /**
26
     * Apply the `transformValue` function to all values in multidementional
27
     * associative array where the key matches one of the keys defined
28
     * on the RecursiveTransform.
29
     * @param  array $config
30
     * @return array transformed config
31
     */
32
    public function transform($config)
33
    {
34
        array_walk($config, function (&$value, $key) {
35
            if (in_array($key, $this->getKeys(), true)) {
36
                $value = $this->transformValue($value);
37
            } else {
38
                if ($this->shouldRecurse($value, $key)) {
39
                    $value = $this->transform($value);
40
                }
41
            }
42
        });
43
44
        return $config;
45
    }
46
47
    /**
48
     * Based upon the value or key, determine if the transform function
49
     * should recurse.
50
     * @param $value
51
     * @param string $key
52
     * @return bool
53
     */
54
    protected function shouldRecurse($value, $key)
55
    {
56
        return is_array($value);
57
    }
58
59
    /**
60
     * Impelment this in all discrete classes
61
     * @param  mixed $value input
62
     * @return mixed output value
63
     */
64
    abstract public function transformValue($value);
65
}
66