Skip to content

Commit

Permalink
HULI-2083 moving functionality from HString to Kstring
Browse files Browse the repository at this point in the history
  • Loading branch information
fahernandez committed Jan 30, 2017
1 parent 7bb0d7a commit fa76f6f
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions library/Kima/Util/KString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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');
Expand All @@ -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);
}
}

0 comments on commit fa76f6f

Please sign in to comment.