[UTIL] Update Formatting::{toString,toArray} to allow spliting by either space or comma

This commit is contained in:
Hugo Sales 2020-07-25 17:48:44 +00:00 committed by Hugo Sales
parent 256d57adaa
commit 51f65edb55
1 changed files with 13 additions and 7 deletions

View File

@ -156,16 +156,21 @@ abstract class Formatting
throw new InvalidArgumentException('Formatting::indent\'s first parameter must be either an array or a string. Input was: ' . $in); throw new InvalidArgumentException('Formatting::indent\'s first parameter must be either an array or a string. Input was: ' . $in);
} }
const SPLIT_BY_SPACE = ' ';
const SPLIT_BY_COMMA = ', ';
/** /**
* Convert scalars, objects implementing __toString or arrays to strings * Convert scalars, objects implementing __toString or arrays to strings
* *
* @param mixed $value * @param mixed $value
*/ */
public static function toString($value): string public static function toString($value, string $split_type = SPLIT_BY_COMMA): string
{ {
return is_array($value) if (!is_array($value)) {
? '[' . implode(', ', F\map($value, function ($s) { return "'{$s}'"; })) . ']' return (string) $value;
: (string) $value; } else {
return implode($split_type, $value);
}
} }
/** /**
@ -173,11 +178,12 @@ abstract class Formatting
* *
* @param mixed $output * @param mixed $output
*/ */
public static function toArray(string $input, &$output): bool public static function toArray(string $input, &$output, string $split_type = SPLIT_BY_COMMA): bool
{ {
$matches = []; $matches = [];
if (preg_match('/^ *\[([^,]+(, ?[^,]+)*)\] *$/', $input, $matches)) { if (preg_match('/^ *\[?([^,]+(, ?[^,]+)*)\]? *$/', $input, $matches)) {
$output = str_replace([' \'', '\'', ' "', '"'], '', explode(',', $matches[1])); $output = str_replace([' \'', '\'', ' "', '"'], '', explode($split_type[0], $matches[1]));
$output = F\map($output, F\ary('trim', 1));
return true; return true;
} }
return false; return false;