[TASK] Cleanup GeneralUtility::rmFromList 23/25723/9
authorTomita Militaru <militarutomita@gmail.com>
Thu, 28 Nov 2013 10:31:25 +0000 (12:31 +0200)
committerMarkus Klein <klein.t3@mfc-linz.at>
Mon, 17 Feb 2014 15:00:14 +0000 (16:00 +0100)
Simplifies the method. Avoids using array as intermediate. Work directly
with the string value instead.

Resolves: #53677
Releases: 6.2
Change-Id: I1abcd606a2699e959be1816c88cb147dbf557141
Reviewed-on: https://review.typo3.org/25723
Reviewed-by: Stefan Neufeind
Tested-by: Stefan Neufeind
Reviewed-by: Oliver Klee
Reviewed-by: Markus Klein
Tested-by: Markus Klein
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php

index 0ee56bc..52ecaa3 100644 (file)
@@ -715,13 +715,14 @@ class GeneralUtility {
         * @return string New comma-separated list of items
         */
        static public function rmFromList($element, $list) {
-               $items = explode(',', $list);
-               foreach ($items as $k => $v) {
-                       if ($v == $element) {
-                               unset($items[$k]);
-                       }
-               }
-               return implode(',', $items);
+               return trim(
+                       str_replace(
+                               ',' . $element . ',',
+                               ',',
+                               ',' . $list . ','
+                       ),
+                       ','
+               );
        }
 
        /**
index 58be263..1995c27 100644 (file)
@@ -684,7 +684,9 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        'Element at end of list' => array('one,two,removeme', 'one,two'),
                        'One item list' => array('removeme', ''),
                        'Element not contained in list' => array('one,two,three', 'one,two,three'),
-                       'Empty list' => array('', '')
+                       'Empty list' => array('', ''),
+                       'List with leading comma is trimmed afterwards' => array(',one,two,removeme', 'one,two'),
+                       'List with trailing comma is trimmed afterwards' => array('one,two,removeme,', 'one,two'),
                );
        }