10000 feature #17578 [Yaml] dump customization option with dumper flags (xa… · symfony/symfony@b34653f · GitHub
[go: up one dir, main page]

Skip to content

Commit b34653f

Browse files
committed
feature #17578 [Yaml] dump customization option with dumper flags (xabbuh)
This PR was merged into the 3.1-dev branch. Discussion ---------- [Yaml] dump customization option with dumper flags | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #17520 | License | MIT | Doc PR | TODO Commits ------- 286103b [Yaml] dump customization option with dumper flags
2 parents 512abd7 + 286103b commit b34653f

File tree

7 files changed

+80
-13
lines changed

7 files changed

+80
-13
lines changed

UPGRADE-3.1.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,18 @@ Serializer
3333
Yaml
3434
----
3535

36+
* Deprecated support for passing `true`/`false` as the third argument to the `dump()` methods to toggle object support.
37+
38+
Before:
39+
40+
```php
41+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
42+
```
43+
44+
After:
45+
46+
```php
47+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Yaml::DUMP_OBJECT);
48+
3649
* The `!!php/object` tag to indicate dumped PHP objects has been deprecated
3750
and will be removed in Symfony 4.0. Use the `!php/object` tag instead.

UPGRADE-4.0.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,19 @@ Serializer
2424
Yaml
2525
----
2626

27+
* Removed support for passing `true`/`false` as the third argument to the `dump()` methods to toggle object support.
28+
29+
Before:
30+
31+
```php
32+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
33+
```
34+
35+
After:
36+
37+
```php
38+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Yaml::DUMP_OBJECT);
39+
```
40+
2741
* The `!!php/object` tag to indicate dumped PHP objects was removed in favor of
2842
the `!php/object` tag.

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
57A7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
CHANGELOG
22
=========
33

4+
3.1.0
5+
-----
6+
7+
* Added support for customizing the dumped YAML string through an optional bit field:
8+
9+
```php
10+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Yaml::DUMP_OBJECT);
11+
```
12+
413
3.0.0
514
-----
615

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,23 @@ public function setIndentation($num)
4242
* @param int $inline The level where you switch to inline YAML
4343
* @param int $indent The level of indentation (used internally)
4444
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
45-
* @param bool $objectSupport true if object support is enabled, false otherwise
45+
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
4646
*
4747
* @return string The YAML representation of the PHP value
4848
*/
49-
public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $objectSupport = false)
49+
public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $flags = 0)
5050
{
51+
if (is_bool($flags)) {
52+
@trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
53+
54+
$flags = (int) $flags;
55+
}
56+
5157
$output = '';
5258
$prefix = $indent ? str_repeat(' ', $indent) : '';
5359

5460
if ($inline <= 0 || !is_array($input) || empty($input)) {
55-
$output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $objectSupport);
61+
$output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $flags);
5662
} else {
5763
$isAHash = array_keys($input) !== range(0, count($input) - 1);
5864

@@ -61,9 +67,9 @@ public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType =
6167

6268
$output .= sprintf('%s%s%s%s',
6369
$prefix,
64-
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $objectSupport).':' : '-',
70+
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $flags).':' : '-',
6571
$willBeInlined ? ' ' : "\n",
66-
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $objectSupport)
72+
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $flags)
6773
).($willBeInlined ? "\n" : '');
6874
}
6975
}

src/Symfony/Component/Yaml/Inline.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,20 @@ public static function parse($value, $exceptionOnInvalidType = false, $objectSup
8888
*
8989
* @param mixed $value The PHP variable to convert
9090
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
91-
* @param bool $objectSupport true if object support is enabled, false otherwise
91+
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
9292
*
9393
* @return string The YAML string representing the PHP array
9494
*
9595
* @throws DumpException When trying to dump PHP resource
9696
*/
97-
public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false)
97+
public static function dump($value, $exceptionOnInvalidType = false, $flags = 0)
9898
{
99+
if (is_bool($flags)) {
100+
@trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
101+
102+
$flags = (int) $flags;
103+
}
104+
99105
switch (true) {
100106
case is_resource($value):
101107
if ($exceptionOnInvalidType) {
@@ -104,7 +110,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
104110

105111
return 'null';
106112
case is_object($value):
107-
if ($objectSupport) {
113+
if (Yaml::DUMP_OBJECT & $flags) {
108114
return '!php/object:'.serialize($value);
109115
}
110116

@@ -114,7 +120,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
114120

115121
return 'null';
116122
case is_array($value):
117-
return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport);
123+
return self::dumpArray($value, $exceptionOnInvalidType, $flags);
118124
case null === $value:
119125
return 'null';
120126
case true === $value:

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Yaml\Parser;
1515
use Symfony\Component\Yaml\Dumper;
16+
use Symfony\Component\Yaml\Yaml;
1617

1718
class DumperTest extends \PHPUnit_Framework_TestCase
1819
{
@@ -177,6 +178,16 @@ public function testInlineLevel()
177178
}
178179

179180
public function testObjectSupportEnabled()
181+
{
182+
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Yaml::DUMP_OBJECT);
183+
184+
$this->assertEquals('{ foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $dump, '->dump() is able to dump objects');
185+
}
186+
187+
/**
188+
* @group legacy
189+
*/
190+
public function testObjectSupportEnabledPassingTrue()
180191
{
181192
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
182193

@@ -195,7 +206,7 @@ public function testObjectSupportDisabledButNoExceptions()
195206
*/
196207
public function testObjectSupportDisabledWithExceptions()
197208
{
198-
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true, false);
209+
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
199210
}
200211

201212
/**

src/Symfony/Component/Yaml/Yaml.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
class Yaml
2222
{
23+
const DUMP_OBJECT = 1;
24+
2325
/**
2426
* Parses YAML into a PHP value.
2527
*
@@ -55,15 +57,21 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup
5557
* @param int $inline The level where you switch to inline YAML
5658
* @param int $indent The amount of spaces to use for indentation of nested nodes.
5759
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
58-
* @param bool $objectSupport true if object support is enabled, false otherwise
60+
* @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
5961
*
6062
* @return string A YAML string representing the original PHP array
6163
*/
62-
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
64+
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $flags = 0)
6365
{
66+
if (is_bool($flags)) {
67+
@trigger_error('Passing a boolean flag to toggle object support is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
68+
69+
$flags = (int) $flags;
70+
}
71+
6472
$yaml = new Dumper();
6573
$yaml->setIndentation($indent);
6674

67-
return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport);
75+
return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $flags);
6876
}
6977
}

0 commit comments

Comments
 (0)
0