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

Commit

Permalink
Added support for kebab case ACF field names.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendan Corazzin authored and Brendan Corazzin committed May 13, 2019
1 parent 6b1e779 commit a1e7df8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Module/Acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Sober\Controller\Module;

use Sober\Controller\Utils;

class Acf
{
// Config
Expand Down Expand Up @@ -32,6 +34,21 @@ private function setReturnFilter()
: false);
}

/**
* Recursively iterates over array and adds a new snake cased keys=>value for all kebab cased keys
*
* Return void
*/
private function recursiveSnakeCase(&$data) {
foreach ($data as $key => $val) {
if (is_array($val)) {
$this->recursiveSnakeCase($val);
} else {
$data[Utils::convertKebabCaseToSnakeCase($key)] = $val;
}
}
}

/**
* Set Data Return Format
*
Expand Down Expand Up @@ -88,6 +105,8 @@ public function setData($acf)
$this->data[$item] = get_field($item, $query);
}
}

$this->recursiveSnakeCase($this->data);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@ public static function convertToKebabCase($str)
{
return strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $str));
}

/**
* Convert To Snake Case
*
* Converts kebab case to snake case for data variables
* @return string
*/
public static function convertKebabCaseToSnakeCase($str)
{
return strtolower(str_replace('-', '_', $str));
}
}

0 comments on commit a1e7df8

Please sign in to comment.