From 9adc12ea3758edd8fb4b44611b9559d039c3ac90 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Fri, 18 Jul 2014 11:26:46 +0200 Subject: [PATCH] Added function to help convert object and array properties after getting them from the database. --- h5p.classes.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/h5p.classes.php b/h5p.classes.php index 292cf2d..ea447f7 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -1755,6 +1755,31 @@ class H5PCore { return $upgrades; } + + /** + * Converts all the properties of the given object or array from + * snake_case to camelCase. Useful after fetching data from the database. + * + * Note that some databases does not support camelCase. + * + * @param mixed $arr input + * @param boolean $obj return object + * @return mixed object or array + */ + public static function snakeToCamel($arr, $obj = false) { + $newArr = array(); + + foreach ($arr as $key => $val) { + $next = -1; + while (($next = strpos($key, '_', $next + 1)) !== FALSE) { + $key = substr_replace($key, strtoupper($key{$next + 1}), $next, 2); + } + + $newArr[$key] = $val; + } + + return $obj ? (object) $newArr : $newArr; + } } /**