From fa76f6f48a94d5d3047ddb1a825300215edc11d9 Mon Sep 17 00:00:00 2001 From: fahernandez Date: Mon, 30 Jan 2017 15:31:28 -0600 Subject: [PATCH] HULI-2083 moving functionality from HString to Kstring --- library/Kima/Util/KString.php | 52 +++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/library/Kima/Util/KString.php b/library/Kima/Util/KString.php index bc8485a..830819c 100755 --- a/library/Kima/Util/KString.php +++ b/library/Kima/Util/KString.php @@ -11,15 +11,15 @@ */ class KString { - /** * Transforms camel case to underscore * * @param string + * @param mixed $string * * @return string */ - public static function camel_case_to_underscore($string) + public function camel_case_to_underscore($string) { return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', (string) $string)); } @@ -31,7 +31,7 @@ public static function camel_case_to_underscore($string) * * @return string */ - public static function to_slug($string) + public function to_slug($string) { // convert non ascii characters to its equivalent setlocale(LC_CTYPE, 'en_US.utf8'); @@ -46,4 +46,50 @@ public static function to_slug($string) // return lowercase string return strtolower($string); } + + /** + * Builds a comma separated list with an special separator for the last item + * + * @param array $data + * @param string $last_item_separator a separator to use with grammatical meaning + * + * @return string + */ + public function build_comma_list($data, $last_item_separator = ' and ') + { + $count = count($data); + $list = []; + + if ($count > 1) { + for ($i = 0; $i < $count; $i++) { + array_unshift($list, array_pop($data)); + if ($i === 0) { + array_unshift($list, $last_item_separator); + } elseif (isset($data[0])) { + array_unshift($list, ', '); + } + } + } else { + $list = $data; + } + + return implode($list); + } + + /** + * Get a random string from a determinate size + * + * @param int $size + * + * @return string random string of the value size + */ + public function rand($size) + { + // Convert the number to negative + // The idea is to get the rand string backwards so the uniqueness is better + $size *= -1; + + // Adding additional entropy + return substr(uniqid('', true), $size); + } }