Cleaned up \FederationLib\Utilities

This commit is contained in:
Netkas 2023-06-23 04:05:49 -04:00
parent ba9cbf6a60
commit 3301e01065
No known key found for this signature in database
GPG key ID: 5DAF58535614062B

View file

@ -31,16 +31,24 @@
public static function parseBoolean($input): bool public static function parseBoolean($input): bool
{ {
if(is_null($input)) if(is_null($input))
{
return false; return false;
}
if(is_bool($input)) if(is_bool($input))
{
return $input; return $input;
}
if(is_numeric($input)) if(is_numeric($input))
{
return (bool) $input; return (bool) $input;
}
if(is_string($input)) if(is_string($input))
{
$input = trim($input); $input = trim($input);
}
switch(strtolower($input)) switch(strtolower($input))
{ {
@ -69,12 +77,16 @@
public static function promptInput(?string $prompt=null, ?string $default_value=null): ?string public static function promptInput(?string $prompt=null, ?string $default_value=null): ?string
{ {
if($prompt) if($prompt)
{
print($prompt . ' '); print($prompt . ' ');
}
$input = trim(fgets(STDIN)); $input = trim(fgets(STDIN));
if(!$input && $default_value) if(!$input && $default_value)
{
$input = $default_value; $input = $default_value;
}
return $input; return $input;
} }
@ -89,12 +101,16 @@
public static function promptYesNo(?string $prompt=null, bool $default_value=false): bool public static function promptYesNo(?string $prompt=null, bool $default_value=false): bool
{ {
if($prompt) if($prompt)
{
print($prompt . ' '); print($prompt . ' ');
}
$input = trim(fgets(STDIN)); $input = trim(fgets(STDIN));
if(!$input && $default_value) if(!$input && $default_value)
{
$input = $default_value; $input = $default_value;
}
return self::parseBoolean($input); return self::parseBoolean($input);
} }
@ -107,7 +123,7 @@
* @param int $body_width * @param int $body_width
* @return void * @return void
*/ */
public static function printData($banner_text, $data, int $body_width = 70) public static function printData($banner_text, $data, int $body_width = 70): void
{ {
// Padding and wrap for the longest key // Padding and wrap for the longest key
$max_key_len = max(array_map('strlen', array_keys($data))); $max_key_len = max(array_map('strlen', array_keys($data)));
@ -122,18 +138,22 @@
echo str_repeat("*", $banner_width) . PHP_EOL; echo str_repeat("*", $banner_width) . PHP_EOL;
// Print data // Print data
foreach ($data as $key => $value) { foreach ($data as $key => $value)
{
// Split value into lines if it's too long // Split value into lines if it's too long
$lines = str_split($value, $padding); $lines = str_split($value, $padding);
// Print lines // Print lines
foreach ($lines as $i => $line) { foreach ($lines as $i => $line)
if ($i == 0) { {
if ($i === 0)
{
// First line - print key and value // First line - print key and value
echo ' ' . str_pad(strtoupper($key), $max_key_len, ' ', STR_PAD_RIGHT) . ' ' . $line . PHP_EOL; print(' ' . str_pad(strtoupper($key), $max_key_len) . ' ' . $line . PHP_EOL);
} else { }
else
{
// Additional lines - only value // Additional lines - only value
echo str_repeat(' ', $max_key_len + 4) . $line . PHP_EOL; print(str_repeat(' ', $max_key_len + 4) . $line . PHP_EOL);
} }
} }
} }