Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
60.00% |
3 / 5 |
CRAP | |
88.89% |
16 / 18 |
StringUtil | |
0.00% |
0 / 1 |
|
60.00% |
3 / 5 |
13.23 | |
88.89% |
16 / 18 |
startsWith | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
endsWith | |
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
|||
trimToNull | |
0.00% |
0 / 1 |
7.05 | |
90.00% |
9 / 10 |
|||
getEndAfterLast | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
getStartBeforeLast | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
<?php | |
declare(strict_types = 1); | |
namespace Siesta\Util; | |
/** | |
* @author Gregor Müller | |
*/ | |
class StringUtil | |
{ | |
/** | |
* @param string $haystack | |
* @param string $needle | |
* | |
* @return bool | |
*/ | |
public static function startsWith($haystack, $needle) | |
{ | |
return !strncmp($haystack, $needle, strlen($needle)); | |
} | |
/** | |
* @param $haystack | |
* @param $needle | |
* | |
* @return bool | |
*/ | |
public static function endsWith($haystack, $needle) | |
{ | |
return $needle === "" || substr($haystack, -strlen($needle)) === $needle; | |
} | |
/** | |
* @param $value | |
* @param int $maxLength | |
* | |
* @return bool|int|null|string | |
*/ | |
public static function trimToNull($value, $maxLength = null) | |
{ | |
if ($value === null) { | |
return null; | |
} | |
// preserve 0 | |
if ($value === 0 || $value === "0") { | |
return "0"; | |
} | |
// trim it | |
$value = trim($value); | |
if ($value === "") { | |
return null; | |
} | |
if ($maxLength === 0 || $maxLength === null) { | |
return $value; | |
} | |
return substr($value, 0, $maxLength); | |
} | |
/** | |
* @param string $haystack | |
* @param string $needle | |
* | |
* @return string | |
*/ | |
public static function getEndAfterLast(string $haystack, string $needle) : string | |
{ | |
$lastOccurence = strrchr($haystack, $needle); | |
if ($lastOccurence === false) { | |
return $haystack; | |
} | |
return ltrim($lastOccurence, $needle); | |
} | |
/** | |
* @param string $haystack | |
* @param string $needle | |
* | |
* @return string | |
*/ | |
public static function getStartBeforeLast($haystack, $needle) | |
{ | |
$end = self::getEndAfterLast($haystack, $needle); | |
return str_replace($needle . $end, "", $haystack); | |
} | |
} |