Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commiteff6902

Browse files
committed
option to dump multi line strings as scalar blocks
1 parentfce909a commiteff6902

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

‎src/Symfony/Component/Yaml/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
3.1.0
55
-----
66

7+
* Added support for dumping multi line strings as literal blocks.
8+
79
* Added support for parsing base64 encoded binary data when they are tagged
810
with the`!!binary` tag.
911

‎src/Symfony/Component/Yaml/Dumper.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
8484
$isAHash =array_keys($input) !==range(0,count($input) -1);
8585

8686
foreach ($inputas$key =>$value) {
87+
if ($inline >1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK &$flags &&is_string($value) &&false !==strpos($value,"\n")) {
88+
$output .=sprintf("%s%s%s |\n",$prefix,$isAHash ? Inline::dump($key,$flags).':' :'-','');
89+
90+
foreach (preg_split('/\n|\r\n/',$value)as$row) {
91+
$output .=sprintf("%s%s%s\n",$prefix,str_repeat('',$this->indentation),$row);
92+
}
93+
94+
continue;
95+
}
96+
8797
$willBeInlined =$inline -1 <=0 || !is_array($value) ||empty($value);
8898

8999
$output .=sprintf('%s%s%s%s',

‎src/Symfony/Component/Yaml/Tests/DumperTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,21 @@ public function objectAsMapProvider()
332332

333333
return$tests;
334334
}
335+
336+
publicfunctiontestDumpMultiLineStringAsScalarBlock()
337+
{
338+
$data =array(
339+
'data' =>array(
340+
'single_line' =>'foo bar baz',
341+
'multi_line' =>"foo\nline with trailing spaces:\n\nbar\r\ninteger like line:\n123456789\nempty line:\n\nbaz",
342+
'nested_inlined_multi_line_string' =>array(
343+
'inlined_multi_line' =>"foo\nbar\r\nempty line:\n\nbaz",
344+
),
345+
),
346+
);
347+
348+
$this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block.yml'),$this->dumper->dump($data,3,0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
349+
}
335350
}
336351

337352
class A
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
data:
2+
single_line:'foo bar baz'
3+
multi_line:|
4+
foo
5+
line with trailing spaces:
6+
7+
bar
8+
integer like line:
9+
123456789
10+
empty line:
11+
12+
baz
13+
nested_inlined_multi_line_string:
14+
inlined_multi_line:"foo\nbar\r\nempty line:\n\nbaz"

‎src/Symfony/Component/Yaml/Yaml.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Yaml
2828
constPARSE_DATETIME =32;
2929
constDUMP_BASE64_BINARY_DATA =64;
3030
constDUMP_OBJECT_AS_MAP =128;
31+
constDUMP_MULTI_LINE_LITERAL_BLOCK =256;
3132

3233
/**
3334
* Parses YAML into a PHP value.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp