[FEATURE] Unset parts in t3lib_div::array_merge_recursive_overrule()
authorIngmar Schlecht <ingmar.schlecht@typo3.org>
Wed, 15 Feb 2012 12:22:10 +0000 (13:22 +0100)
committerSusanne Moog <typo3@susannemoog.de>
Thu, 16 Feb 2012 10:23:04 +0000 (11:23 +0100)
Add possibility to t3lib_div::array_merge_recursive_overrule()
to unset values from the resulting array using special values
__UNSET in the second array.

Change-Id: Iab17d59b80c1e1070c8b2db6b318da0b01fda13c
Resolves: #33941
Releases: 4.7
Reviewed-on: http://review.typo3.org/9026
Reviewed-by: Oliver Klee
Reviewed-by: Tolleiv Nietsch
Tested-by: Tolleiv Nietsch
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
t3lib/class.t3lib_div.php
tests/t3lib/class.t3lib_divTest.php

index 1a9ca62..7af6443 100644 (file)
@@ -1849,28 +1849,30 @@ final class t3lib_div {
         * @param array $arr1 Second array, overruling the first array
         * @param boolean $notAddKeys If set, keys that are NOT found in $arr0 (first array) will not be set. Thus only existing value can/will be overruled from second array.
         * @param boolean $includeEmptyValues If set, values from $arr1 will overrule if they are empty or zero. Default: TRUE
+        * @param boolean $enableUnsetFeature If set, special values "__UNSET" can be used in the second array in order to unset array keys in the resulting array.
         * @return array Resulting array where $arr1 values has overruled $arr0 values
         */
-       public static function array_merge_recursive_overrule(array $arr0, array $arr1, $notAddKeys = FALSE, $includeEmptyValues = TRUE) {
+       public static function array_merge_recursive_overrule(array $arr0, array $arr1, $notAddKeys = FALSE, $includeEmptyValues = TRUE, $enableUnsetFeature = TRUE) {
                foreach ($arr1 as $key => $val) {
                        if (is_array($arr0[$key])) {
                                if (is_array($arr1[$key])) {
-                                       $arr0[$key] = self::array_merge_recursive_overrule($arr0[$key], $arr1[$key], $notAddKeys, $includeEmptyValues);
+                                       $arr0[$key] = self::array_merge_recursive_overrule(
+                                               $arr0[$key],
+                                               $arr1[$key],
+                                               $notAddKeys,
+                                               $includeEmptyValues,
+                                               $enableUnsetFeature
+                                       );
                                }
-                       } else {
-                               if ($notAddKeys) {
-                                       if (isset($arr0[$key])) {
-                                               if ($includeEmptyValues || $val) {
-                                                       $arr0[$key] = $val;
-                                               }
-                                       }
-                               } else {
-                                       if ($includeEmptyValues || $val) {
-                                               $arr0[$key] = $val;
-                                       }
+                       } elseif (!$notAddKeys || isset($arr0[$key])) {
+                               if ($enableUnsetFeature && $val === '__UNSET') {
+                                       unset($arr0[$key]);
+                               } elseif ($includeEmptyValues || $val) {
+                                       $arr0[$key] = $val;
                                }
                        }
                }
+
                reset($arr0);
                return $arr0;
        }
index 2497e5b..dccbe98 100644 (file)
@@ -3630,5 +3630,33 @@ class t3lib_divTest extends tx_phpunit_testcase {
                );
        }
 
+
+       /**
+        * @test
+        */
+       public function arrayMergeRecursiveOverruleDoesConsiderUnsetValues() {
+               $array1 = array(
+                       'first' => array(
+                               'second' => 'second',
+                               'third' => 'third',
+                       ),
+               );
+               $array2 = array(
+                       'first' => array(
+                               'second' => 'overrule',
+                               'third' => '__UNSET',
+                               'fourth' => 'overrile',
+                       ),
+               );
+               $expected = array(
+                       'first' => array(
+                               'second' => 'overrule',
+                               'fourth' => 'overrile',
+                       ),
+               );
+
+               $result = t3lib_div::array_merge_recursive_overrule($array1, $array2);
+               $this->assertEquals($expected, $result);
+       }
 }
 ?>
\ No newline at end of file