forked from GNUsocial/gnu-social
[UTIL] Update Formatting::{toString,toArray} to allow spliting by either space or comma
This commit is contained in:
parent
256d57adaa
commit
51f65edb55
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user