Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit a1e7df8

Browse files
Brendan CorazzinBrendan Corazzin
authored andcommitted
Added support for kebab case ACF field names.
1 parent 6b1e779 commit a1e7df8

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Module/Acf.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sober\Controller\Module;
44

5+
use Sober\Controller\Utils;
6+
57
class Acf
68
{
79
// Config
@@ -32,6 +34,21 @@ private function setReturnFilter()
3234
: false);
3335
}
3436

37+
/**
38+
* Recursively iterates over array and adds a new snake cased keys=>value for all kebab cased keys
39+
*
40+
* Return void
41+
*/
42+
private function recursiveSnakeCase(&$data) {
43+
foreach ($data as $key => $val) {
44+
if (is_array($val)) {
45+
$this->recursiveSnakeCase($val);
46+
} else {
47+
$data[Utils::convertKebabCaseToSnakeCase($key)] = $val;
48+
}
49+
}
50+
}
51+
3552
/**
3653
* Set Data Return Format
3754
*
@@ -88,6 +105,8 @@ public function setData($acf)
88105
$this->data[$item] = get_field($item, $query);
89106
}
90107
}
108+
109+
$this->recursiveSnakeCase($this->data);
91110
}
92111

93112
/**

src/Utils.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,15 @@ public static function convertToKebabCase($str)
6969
{
7070
return strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $str));
7171
}
72+
73+
/**
74+
* Convert To Snake Case
75+
*
76+
* Converts kebab case to snake case for data variables
77+
* @return string
78+
*/
79+
public static function convertKebabCaseToSnakeCase($str)
80+
{
81+
return strtolower(str_replace('-', '_', $str));
82+
}
7283
}

0 commit comments

Comments
 (0)