Commit 2cc09dd9 authored by crell's avatar crell Committed by Christian Kuhn
Browse files

[TASK] Simplify trimExplode()

Make the code a bit more declarative and compact.
This also improves the documentation to make clear
the actual behavior, which was previously not self-evident.

Resolves: #94074
Releases: master, 10.4
Change-Id: I062d75c174ddea8fe1a3010f12831520ada8dafe
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/69185


Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Jochen's avatarJochen <rothjochen@gmail.com>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Jochen's avatarJochen <rothjochen@gmail.com>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
parent 72e0faea
......@@ -974,8 +974,12 @@ class GeneralUtility
}
/**
* Explodes a string and trims all values for whitespace in the end.
* If $onlyNonEmptyValues is set, then all blank ('') values are removed.
* Explodes a string and removes whitespace-only values.
*
* If $onlyNonEmptyValues is set, then all values that contain only whitespace are removed.
*
* Each item will have leading and trailing whitespace removed. However, if the tail items are
* returned as a single array item, their internal whitespace will not be modified.
*
* @param string $delim Delimiter string to explode with
* @param string $string The string to explode
......@@ -985,26 +989,31 @@ class GeneralUtility
* except the last -limit are returned.
* @return string[] Exploded values
*/
public static function trimExplode($delim, $string, $removeEmptyValues = false, $limit = 0)
public static function trimExplode($delim, $string, $removeEmptyValues = false, $limit = 0): array
{
$result = explode($delim, $string) ?: [];
if ($removeEmptyValues) {
$temp = [];
foreach ($result as $value) {
if (trim($value) !== '') {
$temp[] = $value;
}
}
$result = $temp;
// Remove items that are just whitespace, but leave whitespace intact for the rest.
$result = array_values(array_filter($result, fn ($item) => trim($item) !== ''));
}
if ($limit === 0) {
// Return everything.
return array_map('trim', $result);
}
if ($limit > 0 && count($result) > $limit) {
$lastElements = array_splice($result, $limit - 1);
$result[] = implode($delim, $lastElements);
} elseif ($limit < 0) {
$result = array_slice($result, 0, $limit);
if ($limit < 0) {
// Trim and return just the first $limit elements and ignore the rest.
return array_map('trim', array_slice($result, 0, $limit));
}
$result = array_map('trim', $result);
return $result;
// Fold the last length - $limit elements into a single trailing item, then trim and return the result.
$tail = array_slice($result, $limit - 1);
$result = array_slice($result, 0, $limit - 1);
if ($tail) {
$result[] = implode($delim, $tail);
}
return array_map('trim', $result);
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment