2014-02-12 17:01:11 +01:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
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;
|
|
|
|
}
|
2015-02-12 08:56:10 +01:00
|
|
|
|
2015-06-02 17:09:40 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2015-02-12 08:56:10 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2015-03-31 16:29:33 +02:00
|
|
|
|
2015-04-02 16:25:20 +02:00
|
|
|
/**
|
|
|
|
* Check if a string ends with a different string
|
|
|
|
*
|
|
|
|
* @param $haystack The string to search for matches
|
|
|
|
* @param $needle The string to match at the start of the haystack
|
|
|
|
*
|
|
|
|
* @return bool Whether or not needle is at the beginning of haystack
|
|
|
|
*/
|
|
|
|
public static function endsWith($haystack, $needle)
|
|
|
|
{
|
|
|
|
return $needle === '' ||
|
|
|
|
(($temp = strlen($haystack) - strlen($needle)) >= 0 && false !== strpos($haystack, $needle, $temp));
|
|
|
|
}
|
|
|
|
|
2015-03-31 16:29:33 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2014-02-12 17:01:11 +01:00
|
|
|
}
|