|
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', 'collapsed']; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param \StoutLogic\AcfBuilder\NamedBuilder $builder |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(\StoutLogic\AcfBuilder\NamedBuilder $builder) |
|
19
|
|
|
{ |
|
20
|
|
|
parent::__construct($builder); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return \StoutLogic\AcfBuilder\NamedBuilder |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getBuilder() |
|
27
|
|
|
{ |
|
28
|
|
|
return parent::getBuilder(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function transform($config) |
|
32
|
|
|
{ |
|
33
|
|
|
$config = parent::transform($config); |
|
34
|
|
|
|
|
35
|
|
|
$config = $this->secondTransformPass($config, $config); |
|
36
|
|
|
|
|
37
|
|
|
return $config; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $value Key |
|
42
|
|
|
* @return string Namedspaced key |
|
43
|
|
|
*/ |
|
44
|
|
|
public function transformValue($value) |
|
45
|
|
|
{ |
|
46
|
|
|
$namespace = 'field_'; |
|
47
|
|
|
$groupName = $this->getBuilder()->getName(); |
|
48
|
|
|
|
|
49
|
|
|
if ($groupName) { |
|
50
|
|
|
// remove field_ or group_ if already at the begining of the key |
|
51
|
|
|
$value = preg_replace('/^field_|^group_/', '', $value); |
|
52
|
|
|
$namespace .= str_replace(' ', '_', $groupName).'_'; |
|
53
|
|
|
} |
|
54
|
|
|
return strtolower($namespace.$value); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function shouldTransformValue($key, $config) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($key === 'field' && array_key_exists('_field_does_not_exist', $config)) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return parent::shouldTransformValue($key, $config) && !$this->hasCustomKey($key, $config) && !$this->hasCustomCollapsedKey($key, $config); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param $config |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
private function hasCustomKey($key, $config) |
|
71
|
|
|
{ |
|
72
|
|
|
return ($key !== 'collapsed' && array_key_exists('_has_custom_key', $config) && $config['_has_custom_key'] === true); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param $config |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
private function hasCustomCollapsedKey($key, $config) |
|
80
|
|
|
{ |
|
81
|
|
|
return ($key === 'collapsed' && array_key_exists('_has_custom_collapsed_key', $config) && $config['_has_custom_collapsed_key'] === true); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function secondTransformPass(array $config, array $parentConfig) |
|
85
|
|
|
{ |
|
86
|
|
|
foreach ($config as $key => &$value) { |
|
87
|
|
|
if (array_key_exists('_field_does_not_exist', $config)) { |
|
88
|
|
|
foreach($parentConfig as $parentField) { |
|
89
|
|
|
if (is_array($parentField) && |
|
90
|
|
|
array_key_exists('name', $parentField) && |
|
91
|
|
|
$parentField['name'] === $config['_field_does_not_exist']) { |
|
92
|
|
|
$config['field'] = $parentField['key']; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if ($this->shouldRecurse($value, $key)) { |
|
98
|
|
|
$value = $this->secondTransformPass($value, $parentConfig); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $config; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|