10000 [Yaml] dump customization option with dumper flags · symfony/symfony@2dab718 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2dab718

Browse files
committed
[Yaml] dump customization option with dumper flags
1 parent 5b59703 commit 2dab718

File tree

7 files changed

+90
-13
lines changed

7 files changed

+90
-13
lines changed

UPDGRADE-3.1.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
UPGRADE FROM 3.0 to 3.1
2+
=======================
3+
4+
Yaml
5+
----
6+
7+
* Deprecated support for passing `true`/`false` as the third argument to the `dump()` methods to toggle object support.
8+
9+
Before:
10+
11+
```php
12+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
13+
```
14+
15+
After:
16+
17+
```php
18+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Dumper::DUMP_OBJECT);
19+
```

UPGRADE-4.0.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
UPGRADE FROM 3.x to 4.0
2+
=======================
3+
4+
Yaml
5+
----
6+
7+
* Removed support for passing `true`/`false` as the third argument to the `dump()` methods to toggle object support.
8+
9+
Before:
10+
11+
```php
12+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
13+
```
14+
15+
After:
16+
17+
```php
18+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Dumper::DUMP_OBJECT);
19+
```

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
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, Dumper::DUMP_OBJECT);
11+
```
12+
413
3.0.0
514
-----
615

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
class Dumper
2020
{
21+
const DUMP_OBJECT = 1;
22+
2123
/**
2224
* The amount of spaces to use for indentation of nested nodes.
2325
*
@@ -42,17 +44,23 @@ public function setIndentation($num)
4244
* @param int $inline The level where you switch to inline YAML
4345
* @param int $indent The level of indentation (used internally)
4446
* @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
47+
* @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
4648
*
4749
* @return string The YAML representation of the PHP value
4850
*/
49-
public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $objectSupport = false)
51+
public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $flags = 0)
5052
{
53+
if (is_bool($flags)) {
54+
@trigger_error('Passing a boolean flag to toogle object support is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
55+
56+
$flags = (int) $flags;
57+
}
58+
5159
$output = '';
5260
$prefix = $indent ? str_repeat(' ', $indent) : '';
5361

5462
if ($inline <= 0 || !is_array($input) || empty($input)) {
55-
$output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $objectSupport);
63+
$output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $flags);
5664
} else {
5765
$isAHash = array_keys($input) !== range(0, count($input) - 1);
5866

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

6270
$output .= sprintf('%s%s%s%s',
6371
$prefix,
64-
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $objectSupport).':' : '-',
72+
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $flags).':' : '-',
6573
$willBeInlined ? ' ' : "\n",
66-
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $objectSupport)
74+
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $flags)
6775
).($willBeInlined ? "\n" : '');
6876
}
6977
}

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 Dumper::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 toogle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Dumper::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 (Dumper::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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ public function testInlineLevel()
177177
}
178178

179179
public function testObjectSupportEnabled()
180+
{
181+
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Dumper::DUMP_OBJECT);
182+
183+
$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');
184+
}
185+
186+
/**
187+
* @group legacy
188+
*/
189+
public function testObjectSupportEnabledPassingTrue()
180190
{
181191
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
182192

@@ -195,7 +205,7 @@ public function testObjectSupportDisabledButNoExceptions()
195205
*/
196206
public function testObjectSupportDisabledWithExceptions()
197207
{
198-
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true, false);
208+
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
199209
}
200210

201211
/**

src/Symfony/Component/Yaml/Yaml.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,21 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup
5858
* @param int $inline The level where you switch to inline YAML
5959
* @param int $indent The amount of spaces to use for indentation of nested nodes.
6060
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
61-
* @param bool $objectSupport true if object support is enabled, false otherwise
61+
* @param int $flags A bit field of Dumper::DUMP_* constants to customize the dumped YAML string
6262
*
6363
* @return string A YAML string representing the original PHP array
6464
*/
65-
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
65+
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $flags = 0)
6666
{
67+
if (is_bool($flags)) {
68+
@trigger_error('Passing a boolean flag to toogle object support is deprecated since version 3.1 and will be removed in 4.0. Use the Dumper::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
69+
70+
$flags = (int) $flags;
71+
}
72+
6773
$yaml = new Dumper();
6874
$yaml->setIndentation($indent);
6975

70-
return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport);
76+
return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $flags);
7177
}
7278
}

0 commit comments

Comments
 (0)
0