Skip to content

Commit

Permalink
[Yaml] Extract duplicate code to private function
Browse files Browse the repository at this point in the history
  • Loading branch information
alamirault committed Nov 8, 2022
1 parent b02a689 commit 6026422
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,7 @@ public static function dump(mixed $value, int $flags = 0): string
}

if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
$output = [];

foreach ($value as $key => $val) {
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
}

return sprintf('{ %s }', implode(', ', $output));
return self::dumpHashArray($value, $flags);
}

if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
Expand Down Expand Up @@ -232,7 +226,17 @@ private static function dumpArray(array $value, int $flags): string
return sprintf('[%s]', implode(', ', $output));
}

// hash
return self::dumpHashArray($value, $flags);
}

/**
* Dumps hash array to a YAML string.
*
* @param array|\ArrayObject|\stdClass $value The hash array to dump
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
*/
private static function dumpHashArray(array|\ArrayObject|\stdClass $value, int $flags): string
{
$output = [];
foreach ($value as $key => $val) {
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
Expand Down

0 comments on commit 6026422

Please sign in to comment.