2014-02-12 17:01:11 +01:00
|
|
|
<?php
|
2015-02-03 16:27:59 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | http://www.gnu.org/licenses/gpl-2.0.txt */
|
2014-02-12 17:01:11 +01:00
|
|
|
|
|
|
|
namespace Icinga\Util;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Common string helper
|
|
|
|
*/
|
|
|
|
class String
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Split string into an array and trim spaces
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @param string $delimiter
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function trimSplit($value, $delimiter = ',')
|
|
|
|
{
|
2014-03-03 18:16:07 +01:00
|
|
|
return array_map('trim', explode($delimiter, $value));
|
2014-02-12 17:01:11 +01:00
|
|
|
}
|
2014-09-30 22:34:58 +02:00
|
|
|
|
|
|
|
/**
|
2014-12-18 17:23:54 +01:00
|
|
|
* Uppercase the first character of each word in a string
|
2014-09-30 22:34:58 +02:00
|
|
|
*
|
|
|
|
* Converts 'first_name' to 'firstName' for example.
|
|
|
|
*
|
|
|
|
* @param string $name
|
2014-12-18 17:23:54 +01:00
|
|
|
* @param string $separator Word separator
|
2014-09-30 22:34:58 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-12-18 17:23:54 +01:00
|
|
|
public static function cname($name, $separator = '_')
|
2014-09-30 22:34:58 +02:00
|
|
|
{
|
2014-12-18 17:23:54 +01:00
|
|
|
return str_replace(' ', '', ucwords(str_replace($separator, ' ', strtolower($name))));
|
2014-09-30 22:34:58 +02:00
|
|
|
}
|
2015-01-19 14:20:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add ellipsis when a string is longer than max length
|
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
* @param int $maxLength
|
|
|
|
* @param string $ellipsis
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function ellipsis($string, $maxLength, $ellipsis = '...')
|
|
|
|
{
|
|
|
|
if (strlen($string) > $maxLength) {
|
|
|
|
return substr($string, 0, $maxLength - strlen($ellipsis)) . $ellipsis;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
2014-02-12 17:01:11 +01:00
|
|
|
}
|