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 c2da1fc

Browse filesBrowse files
committed
[Yaml] dump customization option with dumper flags
1 parent 5b59703 commit c2da1fc
Copy full SHA for c2da1fc

File tree

4 files changed

+43
-13
lines changed
Filter options

4 files changed

+43
-13
lines changed

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

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

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 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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Yaml.php
+9-3Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.