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

NamespaceFieldKey::transformValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace StoutLogic\AcfBuilder\Transform;
4
5
/**
6
 * Namespace a field key by appending the namespace consisting of 'field'
7
 * and the Builder's name before the defined key.
8
 *
9
 * Ensure all lowercase.
10
 */
11
class NamespaceFieldKey extends RecursiveTransform
12
{
13
    protected $keys = ['key', 'field'];
14
15
    public function transformValue($value)
16
    {
17
        $namespace = 'field_';
18
        $groupName = $this->getBuilder()->getName();
19
20
        if ($groupName) {
21
            // remove field_ or group_ if already at the begining of the key
22
            $value = preg_replace('/^field_|^group_/', '', $value);
23
            $namespace .= str_replace(' ', '_', $groupName).'_';
24
        }
25
        return strtolower($namespace.$value);
26
    }
27
}
28