/usr/share/php/Icinga/Util/StringHelper.php is in php-icinga 2.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | <?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Util;
/**
* Common string functions
*/
class StringHelper
{
/**
* Split string into an array and trim spaces
*
* @param string $value
* @param string $delimiter
*
* @return array
*/
public static function trimSplit($value, $delimiter = ',')
{
return array_map('trim', explode($delimiter, $value));
}
/**
* Uppercase the first character of each word in a string
*
* Converts 'first_name' to 'FirstName' for example.
*
* @param string $name
* @param string $separator Word separator
*
* @return string
*/
public static function cname($name, $separator = '_')
{
return str_replace(' ', '', ucwords(str_replace($separator, ' ', strtolower($name))));
}
/**
* 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;
}
/**
* Add ellipsis in the center of a string when a string is longer than max length
*
* @param string $string
* @param int $maxLength
* @param string $ellipsis
*
* @return string
*/
public static function ellipsisCenter($string, $maxLength, $ellipsis = '...')
{
$start = ceil($maxLength / 2.0);
$end = floor($maxLength / 2.0);
if (strlen($string) > $maxLength) {
return substr($string, 0, $start - strlen($ellipsis)) . $ellipsis . substr($string, - $end);
}
return $string;
}
/**
* Find and return all similar strings in $possibilites matching $string with the given minimum $similarity
*
* @param string $string
* @param array $possibilities
* @param float $similarity
*
* @return array
*/
public static function findSimilar($string, array $possibilities, $similarity = 0.33)
{
if (empty($string)) {
return array();
}
$matches = array();
foreach ($possibilities as $possibility) {
$distance = levenshtein($string, $possibility);
if ($distance / strlen($string) <= $similarity) {
$matches[] = $possibility;
}
}
return $matches;
}
/**
* Test whether the given string ends with the given suffix
*
* @param string $string The string to test
* @param string $suffix The suffix the string must end with
*
* @return bool
*/
public static function endsWith($string, $suffix)
{
$stringSuffix = substr($string, -strlen($suffix));
return $stringSuffix !== false ? $stringSuffix === $suffix : false;
}
/**
* Generates an array of strings that constitutes the cartesian product of all passed sets, with all
* string combinations concatenated using the passed join-operator.
*
* <pre>
* cartesianProduct(
* array(array('foo', 'bar'), array('mumble', 'grumble', null)),
* '_'
* );
* => array('foo_mumble', 'foo_grumble', 'bar_mumble', 'bar_grumble', 'foo', 'bar')
* </pre>
*
* @param array $sets An array of arrays containing all sets for which the cartesian
* product should be calculated.
* @param string $glue The glue used to join the strings, defaults to ''.
*
* @returns array The cartesian product in one array of strings.
*/
public static function cartesianProduct(array $sets, $glue = '')
{
$product = null;
foreach ($sets as $set) {
if (! isset($product)) {
$product = $set;
} else {
$newProduct = array();
foreach ($product as $strA) {
foreach ($set as $strB) {
if ($strB === null) {
$newProduct []= $strA;
} else {
$newProduct []= $strA . $glue . $strB;
}
}
}
$product = $newProduct;
}
}
return $product;
}
}
|