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

Commit b75b453

Browse files
author
Darren Jacoby
authored
Merge pull request #126 from bckcmo/master
PR related to issue #122
2 parents 6b1e779 + e87be4f commit b75b453

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Module/Acf.php

Lines changed: 22 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,24 @@ private function setReturnFilter()
3234
: false);
3335
}
3436

37+
/**
38+
* Iterates over array and adds a new snake cased key, with orignial value, for each kebab cased key
39+
*
40+
* Return void
41+
*/
42+
private function recursiveSnakeCase(&$data) {
43+
if(!is_array($data))
44+
return;
45+
46+
foreach ($data as $key => $val) {
47+
if (is_array($val)) {
48+
$this->recursiveSnakeCase($val);
49+
} else {
50+
$data[Utils::convertKebabCaseToSnakeCase($key)] = $val;
51+
}
52+
}
53+
}
54+
3555
/**
3656
* Set Data Return Format
3757
*
@@ -88,6 +108,8 @@ public function setData($acf)
88108
$this->data[$item] = get_field($item, $query);
89109
}
90110
}
111+
112+
$this->recursiveSnakeCase($this->data);
91113
}
92114

93115
/**

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)