| 1 |
<?php |
| 2 |
|
| 3 |
namespace Icinga\Module\Toplevelview\Util; |
| 4 |
|
| 5 |
/** |
| 6 |
* Str is a small helper class for working with strings |
| 7 |
*/ |
| 8 |
class Str |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Str::limit method truncates the given string to the specified length. |
| 12 |
* Used in cases where CSS text-overflow cannot be used. |
| 13 |
* |
| 14 |
* @param string $str |
| 15 |
* @param int $len |
| 16 |
* @param string $end |
| 17 |
* |
| 18 |
* @return string |
| 19 |
*/ |
| 20 |
public static function limit($str, $len = 25, $end = '...'): string |
| 21 |
{ |
| 22 |
if (empty($str)) { |
| 23 |
return ''; |
| 24 |
} |
| 25 |
|
| 26 |
// If the string is smaller or equal to the limit we simply return it |
| 27 |
if (mb_strwidth($str, 'UTF-8') <= $len) { |
| 28 |
return $str; |
| 29 |
} |
| 30 |
|
| 31 |
// If the string is longer than the limit we truncate it |
| 32 |
// and add the given end to it. |
| 33 |
return mb_strimwidth($str, 0, $len, '', 'UTF-8') . $end; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Transforms the title badge title "warning_unhandled" to "Warning Unhandled" |
| 38 |
* |
| 39 |
* @param string $identifier |
| 40 |
* |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
public static function prettyTitle($identifier): string |
| 44 |
{ |
| 45 |
$s = ''; |
| 46 |
foreach (explode('_', $identifier) as $p) { |
| 47 |
$s .= ' ' . ucfirst($p); |
| 48 |
} |
| 49 |
return trim($s); |
| 50 |
} |
| 51 |
} |