From 41c05c345191a0a31c287cb6451ba2e03a525ae3 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 27 Jan 2016 15:57:51 +0100 Subject: [PATCH] Re-add String.php for backwards compatibility --- library/Icinga/Util/String.php | 159 +++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 library/Icinga/Util/String.php diff --git a/library/Icinga/Util/String.php b/library/Icinga/Util/String.php new file mode 100644 index 000000000..0ec29f842 --- /dev/null +++ b/library/Icinga/Util/String.php @@ -0,0 +1,159 @@ + $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. + * + *
+     *  cartesianProduct(
+     *      array(array('foo', 'bar'), array('mumble', 'grumble', null)),
+     *      '_'
+     *  );
+     *     => array('foo_mumble', 'foo_grumble', 'bar_mumble', 'bar_grumble', 'foo', 'bar')
+     * 
+ * + * @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; + } +}