Skip to content

Commit 676eabb

Browse files
committed
Merge branch '2.3' into 2.7
* 2.3: updated VERSION for 2.3.42 update CONTRIBUTORS for 2.3.42 updated CHANGELOG for 2.3.42 Revert "bug #18908 [DependencyInjection] force enabling the external XML entity loaders (xabbuh)" Partial revert of previous PR [DependencyInjection] Skip deep reference check for 'service_container' Catch \Throwable [Serializer] Add missing @throws annotations Fix for #18843 force enabling the external XML entity loaders Removed UTC specification with timestamp
2 parents 507901b + 2cb5f36 commit 676eabb

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

Dumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType =
5858
if ($inline <= 0 || !is_array($input) || empty($input)) {
5959
$output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $objectSupport);
6060
} else {
61-
$isAHash = array_keys($input) !== range(0, count($input) - 1);
61+
$isAHash = Inline::isHash($input);
6262

6363
foreach ($input as $key => $value) {
6464
$willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);

Inline.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,28 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
157157
}
158158
}
159159

160+
/**
161+
* Check if given array is hash or just normal indexed array.
162+
*
163+
* @internal
164+
*
165+
* @param array $value The PHP array to check
166+
*
167+
* @return bool true if value is hash array, false otherwise
168+
*/
169+
public static function isHash(array $value)
170+
{
171+
$expectedKey = 0;
172+
173+
foreach ($value as $key => $val) {
174+
if ($key !== $expectedKey++) {
175+
return true;
176+
}
177+
}
178+
179+
return false;
180+
}
181+
160182
/**
161183
* Dumps a PHP array to a YAML string.
162184
*
@@ -169,11 +191,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
169191
private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport)
170192
{
171193
// array
172-
$keys = array_keys($value);
173-
$keysCount = count($keys);
174-
if ((1 === $keysCount && '0' == $keys[0])
175-
|| ($keysCount > 1 && array_reduce($keys, function ($v, $w) { return (int) $v + $w; }, 0) === $keysCount * ($keysCount - 1) / 2)
176-
) {
194+
if ($value && !self::isHash($value)) {
177195
$output = array();
178196
foreach ($value as $val) {
179197
$output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport);
@@ -182,7 +200,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor
182200
return sprintf('[%s]', implode(', ', $output));
183201
}
184202

185-
// mapping
203+
// hash
186204
$output = array();
187205
foreach ($value as $key => $val) {
188206
$output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport));

Tests/InlineTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,24 @@ public function testParseUnquotedAsteriskFollowedByAComment()
173173
Inline::parse('{ foo: * #foo }');
174174
}
175175

176+
/**
177+
* @dataProvider getDataForIsHash
178+
*/
179+
public function testIsHash($array, $expected)
180+
{
181+
$this->assertSame($expected, Inline::isHash($array));
182+
}
183+
184+
public function getDataForIsHash()
185+
{
186+
return array(
187+
array(array(), false),
188+
array(array(1, 2, 3), false),
189+
array(array(2 => 1, 1 => 2, 0 => 3), true),
190+
array(array('foo' => 1, 'bar' => 2), true),
191+
);
192+
}
193+
176194
public function getTestsForParse()
177195
{
178196
return array(
@@ -379,6 +397,10 @@ public function getTestsForDump()
379397
array('[foo, { bar: foo, foo: [foo, { bar: foo }] }, [foo, { bar: foo }]]', array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo')))),
380398

381399
array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
400+
401+
array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
402+
403+
array('{ foo: { bar: { 1: 2, baz: 3 } } }', array('foo' => array('bar' => array(1 => 2, 'baz' => 3)))),
382404
);
383405
}
384406
}

0 commit comments

Comments
 (0)