From c38b9a15036d292ce3005d1d471021e549f845dd Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 29 Mar 2020 18:05:48 +0000 Subject: [PATCH] [COMMON] Added toCamelCase and toSnakeCase functions --- src/Util/Common.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Util/Common.php b/src/Util/Common.php index 49aeef9272..cb0408020e 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -107,4 +107,28 @@ abstract class Common return self::endsWith($haystack, $needle); }); } + + /** + * Call $func with only abs($count) arguments, taken either from the + * left or right depending on the sign + */ + public static function arity(callable $func, int $count): callable + { + return function (...$args) use ($func, $count) { + if ($count > 0) { + return call_user_func_array($func, F\take_left($args, $count)); + } + return call_user_func_array($func, F\take_right($args, -$count)); + }; + } + + public static function toSnakeCase(string $str): string + { + return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $str)); + } + + public static function toCamelCase(string $str): string + { + return implode('', F\map(preg_split('/[\b_]/', $str), self::arity('ucfirst', 1))); + } }