Commit 04857a5d authored by Christian Kuhn's avatar Christian Kuhn
Browse files

[FEATURE] arrayExport() should recognize int keys

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
parent 11c44c58
......@@ -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);
......
......@@ -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
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