Completed
Push — master ( 77b50b...7ba73a )
by Steve
46s
created

RecursiveTransform::transform()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 1
nop 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
    public function getKeys()
18
    {
19
        return $this->keys;
20
    }
21
22
    /**
23
     * Apply the `transformValue` function to all leafs at the defined keys.
24
     * @param  array $config
25
     * @return array transformed config
26
     */
27
    public function transform($config)
28
    {
29
        array_walk_recursive($config, function(&$value, $key) {
30
            if (in_array($key, $this->getKeys())) {
31
                $value = $this->transformValue($value);
32
            }
33
        });
34
35
        return $config;
36
    }
37
38
    /**
39
     * Impelment this in all discrete classes
40
     * @param  mixed $value input
41
     * @return mixed output value
42
     */
43
    abstract public function transformValue($value);
44
}
45