-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Added pipe for multiline string (correctly indented), which makes them much more readable #17912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,7 +125,7 @@ public static function parse($value, $flags = 0, $references = array()) | |
* | ||
* @throws DumpException When trying to dump PHP resource | ||
*/ | ||
public static function dump($value, $flags = 0) | ||
public static function dump($value, $flags = 0, $indent = 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding this new argument will mess up with the BC layer for the old signature currently. |
||
{ | ||
if (is_bool($flags)) { | ||
@trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED); | ||
|
@@ -165,7 +165,7 @@ public static function dump($value, $flags = 0) | |
|
||
return 'null'; | ||
case is_array($value): | ||
return self::dumpArray($value, $flags); | ||
return self::dumpArray($value, $flags, $indent); | ||
case null === $value: | ||
return 'null'; | ||
case true === $value: | ||
|
@@ -197,6 +197,15 @@ public static function dump($value, $flags = 0) | |
return $repr; | ||
case '' == $value: | ||
return "''"; | ||
case strstr($value, "\n"): | ||
if (Yaml::DUMP_MULTI_LINE_AS_BLOCK & $flags) { | ||
if ($indent) { | ||
$prefix = $indent ? str_repeat(' ', $indent) : ''; | ||
|
||
return "|\n$prefix".preg_replace( '/\n/',"\n$prefix", str_replace( "\r", '', $value ) ); | ||
} | ||
} | ||
|
||
case Escaper::requiresDoubleQuoting($value): | ||
return Escaper::escapeWithDoubleQuotes($value); | ||
case Escaper::requiresSingleQuoting($value): | ||
|
@@ -216,7 +225,7 @@ public static function dump($value, $flags = 0) | |
* | ||
* @return string The YAML string representing the PHP array | ||
*/ | ||
private static function dumpArray($value, $flags) | ||
private static function dumpArray($value, $flags, $indent) | ||
{ | ||
// array | ||
$keys = array_keys($value); | ||
|
@@ -226,7 +235,7 @@ private static function dumpArray($value, $flags) | |
) { | ||
$output = array(); | ||
foreach ($value as $val) { | ||
$output[] = self::dump($val, $flags); | ||
$output[] = self::dump($val, $flags, $indent); | ||
} | ||
|
||
return sprintf('[%s]', implode(', ', $output)); | ||
|
@@ -235,7 +244,7 @@ private static function dumpArray($value, $flags) | |
// mapping | ||
$output = array(); | ||
foreach ($value as $key => $val) { | ||
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags)); | ||
$output[] = sprintf('%s: %s', self::dump($key, $flags, $indent), self::dump($val, $flags, $indent)); | ||
} | ||
|
||
return sprintf('{ %s }', implode(', ', $output)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ class Yaml | |
const PARSE_OBJECT_FOR_MAP = 8; | ||
const DUMP_EXCEPTION_ON_INVALID_TYPE = 16; | ||
const PARSE_DATETIME = 32; | ||
const DUMP_MULTI_LINE_AS_BLOCK = 64; | ||
|
||
/** | ||
* Parses YAML into a PHP value. | ||
|
@@ -89,7 +90,7 @@ public static function parse($input, $flags = 0) | |
* | ||
* @return string A YAML string representing the original PHP array | ||
*/ | ||
public static function dump($array, $inline = 2, $indent = 4, $flags = 0) | ||
public static function dump($array, $inline = 2, $indent = 4, $absIndent = 0,$flags = 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we actually need to expose it to the outside here ? This makes the signature harder to use and understand (and a pain for backward compatibility). Btw, the fact that you are adding the argument before an existing one breaks all usages of the flags. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, I don't know php.... How do I carry that value over without passing it as An hint will suffice! Will write tests once you approve at least what I am doing... Merc.
|
||
{ | ||
if (is_bool($flags)) { | ||
@trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED); | ||
|
@@ -111,6 +112,6 @@ public static function dump($array, $inline = 2, $indent = 4, $flags = 0) | |
|
||
$yaml = new Dumper($indent); | ||
|
||
return $yaml->dump($array, $inline, 0, $flags); | ||
return $yaml->dump($array, $inline, 0, 0, $flags); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the new argument must be placed after the flags, for BC reasons