2022-09-13 13:13:11 +00:00
|
|
|
<?php
|
|
|
|
// XString.php
|
|
|
|
// Created: 2022-02-03
|
2023-07-05 01:28:33 +00:00
|
|
|
// Updated: 2023-07-05
|
2022-09-13 13:13:11 +00:00
|
|
|
|
|
|
|
namespace Index;
|
|
|
|
|
2023-01-05 02:33:37 +00:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
2022-09-13 13:13:11 +00:00
|
|
|
/**
|
|
|
|
* Provides various helper methods for strings.
|
|
|
|
*/
|
|
|
|
final class XString {
|
2023-01-05 02:33:37 +00:00
|
|
|
/**
|
|
|
|
* Default character set for the random method.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const RANDOM_CHARS = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789';
|
|
|
|
|
2022-09-13 13:13:11 +00:00
|
|
|
public static function toBool(string $value): bool {
|
|
|
|
return boolval($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function toInt(string $value, int $base = 10): int {
|
2023-01-01 19:00:00 +00:00
|
|
|
return $base === 10 ? (int)$value : intval($value, $base);
|
2022-09-13 13:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function toFloat(string $value): float {
|
2023-01-01 19:00:00 +00:00
|
|
|
return (float)$value;
|
2022-09-13 13:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function escape(
|
|
|
|
string $value,
|
|
|
|
int $flags = ENT_COMPAT | ENT_HTML5,
|
|
|
|
?string $encoding = null,
|
|
|
|
bool $doubleEncoding = true
|
|
|
|
): string {
|
|
|
|
return htmlspecialchars($value, $flags, $encoding, $doubleEncoding);
|
|
|
|
}
|
|
|
|
|
2023-07-05 01:28:33 +00:00
|
|
|
/**
|
|
|
|
* Counts unique characters in a string.
|
|
|
|
*
|
|
|
|
* @param string String to count unique characters of.
|
|
|
|
* @return int Unique character count.
|
|
|
|
*/
|
|
|
|
public static function countUnique(string $string): int {
|
|
|
|
$string = str_split($string);
|
|
|
|
$chars = [];
|
|
|
|
|
|
|
|
foreach($string as $char)
|
|
|
|
if(!in_array($char, $chars, true))
|
|
|
|
$chars[] = $char;
|
|
|
|
|
|
|
|
return count($chars);
|
|
|
|
}
|
|
|
|
|
2022-09-13 13:13:11 +00:00
|
|
|
/**
|
|
|
|
* Check if a string is null or empty.
|
|
|
|
*
|
|
|
|
* @param IString|string|null $string String ot check for emptiness.
|
|
|
|
* @return bool true if the string is empty, false if not.
|
|
|
|
*/
|
|
|
|
public static function nullOrEmpty(IString|string|null $string): bool {
|
|
|
|
if($string === null)
|
|
|
|
return true;
|
|
|
|
if($string instanceof IString)
|
|
|
|
return $string->isEmpty();
|
|
|
|
return empty($string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a string is null or whitespace.
|
|
|
|
*
|
|
|
|
* @param IString|string|null $string String to check for whitespace.
|
|
|
|
* @return bool true if the string is whitespace, false if not.
|
|
|
|
*/
|
|
|
|
public static function nullOrWhitespace(IString|string|null $string): bool {
|
|
|
|
if($string === null)
|
|
|
|
return true;
|
|
|
|
return empty(trim((string)$string));
|
|
|
|
}
|
2023-01-05 02:33:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a random string of user specified length.
|
|
|
|
*
|
|
|
|
* @param int $length Desired length of the string.
|
|
|
|
* @param string $chars Set of characters to pick from. Default set contains the alphabet in upper- and lowercase and numbers 0 thru 9.
|
|
|
|
* @return string The generated string.
|
|
|
|
*/
|
|
|
|
public static function random(int $length, string $chars = self::RANDOM_CHARS): string {
|
|
|
|
if($length < 1)
|
|
|
|
throw new InvalidArgumentException('$length must be at least 1.');
|
|
|
|
if($chars === '')
|
|
|
|
throw new InvalidArgumentException('$chars may not be empty.');
|
|
|
|
|
|
|
|
$string = '';
|
|
|
|
$count = strlen($chars) - 1;
|
|
|
|
|
|
|
|
while($length-- > 0)
|
|
|
|
$string .= $chars[random_int(0, $count)];
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
2022-09-13 13:13:11 +00:00
|
|
|
}
|