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

Appearance settings

Commit bbb6c7c

Browse filesBrowse files
committed
[Yaml] dump customization option with dumper flags
1 parent 83b53f4 commit bbb6c7c
Copy full SHA for bbb6c7c

File tree

7 files changed

+86
-13
lines changed
Filter options

7 files changed

+86
-13
lines changed

‎UPDGRADE-3.1.md

Copy file name to clipboard
+19Lines 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

Copy file name to clipboardExpand all lines: UPGRADE-4.0.md
+14Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/CHANGELOG.md
+9Lines 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, Yaml::DUMP_OBJECT);
11+
```
12+
413
3.0.0
514
-----
615

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Dumper.php
+11-5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+10-4Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/DumperTest.php
+12-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Yaml.php
+11-3Lines 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 array.
2527
*
@@ -58,15 +60,21 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup
5860
* @param int $inline The level where you switch to inline YAML
5961
* @param int $indent The amount of spaces to use for indentation of nested nodes.
6062
* @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
63+
* @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
6264
*
6365
* @return string A YAML string representing the original PHP array
6466
*/
65-
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
67+
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $flags = 0)
6668
{
69+
if (is_bool($flags)) {
70+
@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);
71+
72+
$flags = (int) $flags;
73+
}
74+
6775
$yaml = new Dumper();
6876
$yaml->setIndentation($indent);
6977

70-
return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $objectSupport);
78+
return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $flags);
7179
}
7280
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.