Added function to help convert object and array properties after getting them from the database.

namespaces
Frode Petterson 2014-07-18 11:26:46 +02:00
parent c9d66ec3e9
commit 9adc12ea37
1 changed files with 25 additions and 0 deletions

View File

@ -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;
}
}
/**