[FEATURE] arrayExport() should recognize int keys
authorChristian Kuhn <lolli@schwarzbu.ch>
Sat, 28 Jul 2012 17:08:23 +0000 (19:08 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sun, 29 Jul 2012 16:47:25 +0000 (18:47 +0200)
t3lib_utility_Array::arrayExport() currently exports array keys always
as strings. With the patch integer array keys are now recognized and
written as integers. Additionally, if all array keys are integers and
consecutive from zero to n, the key is left out completly. See the
unit tests for details.

This is useful for the extListArray in LocalConfiguration, which now
misses the array key and makes the array even more easy to handle.

Change-Id: I6233798a8d22257f542c575791f95226220703ca
Resolves: #39134
Releases: 6.0
Reviewed-on: http://review.typo3.org/13317
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
t3lib/utility/class.t3lib_utility_array.php
tests/Unit/t3lib/utility/class.t3lib_utility_arrayTest.php

index d0009c7..443c771 100644 (file)
@@ -274,8 +274,28 @@ class t3lib_utility_Array {
        public static function arrayExport(array $array = array(), $level = 0) {
                $lines = 'array(' . LF;
                $level ++;
+
+               $writeKeyIndex = FALSE;
+               $expectedKeyIndex = 0;
                foreach ($array as $key => $value) {
-                       $lines .= str_repeat(TAB, $level) . '\'' . $key . '\' => ';
+                       if ($key === $expectedKeyIndex) {
+                               $expectedKeyIndex ++;
+                       } else {
+                                       // Found a non integer or non consecutive key, so we can break here
+                               $writeKeyIndex = TRUE;
+                               break;
+                       }
+               }
+
+               foreach ($array as $key => $value) {
+                               // Indention
+                       $lines .= str_repeat(TAB, $level);
+
+                       if ($writeKeyIndex) {
+                                       // Numeric / string keys
+                               $lines .= is_int($key) ? $key . ' => ' : '\'' . $key . '\' => ';
+                       }
+
                        if (is_array($value)) {
                                if (count($value) > 0) {
                                        $lines .= self::arrayExport($value, $level);
index 98585bb..45f0c45 100644 (file)
@@ -706,6 +706,59 @@ class t3lib_utility_ArrayTest extends tx_phpunit_testcase {
                );
                t3lib_utility_Array::arrayExport($array);
        }
+
+       /**
+        * @test
+        */
+       public function arrayExportReturnsNumericArrayKeys() {
+               $array = array(
+                       'foo' => 'string key',
+                       23 => 'integer key',
+                       '42' => 'string key representing integer',
+               );
+               $expected = 'array(' . LF .
+                       TAB . '\'foo\' => \'string key\',' . LF .
+                       TAB . '23 => \'integer key\',' . LF .
+                       TAB . '42 => \'string key representing integer\',' . LF .
+               ')';
+               $this->assertSame($expected, t3lib_utility_Array::arrayExport($array));
+       }
+
+       /**
+        * @test
+        */
+       public function arrayExportReturnsNoKeyIndexForConsecutiveCountedArrays() {
+               $array = array(
+                       0 => 'zero',
+                       1 => 'one',
+                       2 => 'two',
+               );
+               $expected = 'array(' . LF .
+                       TAB . '\'zero\',' . LF .
+                       TAB . '\'one\',' . LF .
+                       TAB . '\'two\',' . LF .
+               ')';
+               $this->assertSame($expected, t3lib_utility_Array::arrayExport($array));
+       }
+
+       /**
+        * @test
+        */
+       public function arrayExportReturnsKeyIndexForNonConsecutiveCountedArrays() {
+               $array = array(
+                       0 => 'zero',
+                       1 => 'one',
+                       3 => 'three',
+                       4 => 'four',
+               );
+               $expected = 'array(' . LF .
+                       TAB . '0 => \'zero\',' . LF .
+                       TAB . '1 => \'one\',' . LF .
+                       TAB . '3 => \'three\',' . LF .
+                       TAB . '4 => \'four\',' . LF .
+               ')';
+               $this->assertSame($expected, t3lib_utility_Array::arrayExport($array));
+       }
 }
 
 ?>
\ No newline at end of file