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 a029822

Browse filesBrowse files
committed
dumper flag for enabling exception on invalid type
1 parent 33c797e commit a029822
Copy full SHA for a029822

File tree

8 files changed

+117
-37
lines changed
Filter options

8 files changed

+117
-37
lines changed

‎UPGRADE-3.1.md

Copy file name to clipboardExpand all lines: UPGRADE-3.1.md
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ Yaml
7878
Yaml::parse('{ "foo": "bar", "fiz": "cat" }', Yaml::PARSE_OBJECT_FOR_MAP);
7979
```
8080

81+
* Deprecated support for passing `true`/`false` as the fourth argument to the
82+
`dump()` method to trigger exceptions when an invalid type was passed.
83+
84+
Before:
85+
86+
```php
87+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
88+
```
89+
90+
After:
91+
92+
```php
93+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
94+
```
95+
8196
* Deprecated support for passing `true`/`false` as the fifth argument to the `dump()` method to toggle object support.
8297

8398
Before:

‎UPGRADE-4.0.md

Copy file name to clipboardExpand all lines: UPGRADE-4.0.md
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ Yaml
6969
Yaml::parse('{ "foo": "bar", "fiz": "cat" }', Yaml::PARSE_OBJECT_FOR_MAP);
7070
```
7171

72+
* Removed support for passing `true`/`false` as the fourth argument to the
73+
`dump()` method to trigger exceptions when an invalid type was passed.
74+
75+
Before:
76+
77+
```php
78+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
79+
```
80+
81+
After:
82+
83+
```php
84+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
85+
```
86+
7287
* Removed support for passing `true`/`false` as the fifth argument to the `dump()` method to toggle object support.
7388

7489
Before:

‎src/Symfony/Bridge/Twig/Extension/YamlExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Extension/YamlExtension.php
+11-3Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Twig\Extension;
1313

1414
use Symfony\Component\Yaml\Dumper as YamlDumper;
15+
use Symfony\Component\Yaml\Yaml;
1516

1617
/**
1718
* Provides integration of the Yaml component with Twig.
@@ -39,9 +40,16 @@ public function encode($input, $inline = 0, $dumpObjects = 0)
3940
$dumper = new YamlDumper();
4041
}
4142

42-
if (defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT') && is_bool($dumpObjects)) {
43-
@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);
44-
$dumpObjects = (int) $dumpObjects;
43+
if (defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT')) {
44+
if (is_bool($dumpObjects)) {
45+
@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);
46+
47+
$flags = $dumpObjects ? Yaml::DUMP_OBJECT : 0;
48+
} else {
49+
$flags = $dumpObjects;
50+
}
51+
52+
return $dumper->dump($input, $inline, 0, $flags);
4553
}
4654

4755
return $dumper->dump($input, $inline, 0, false, $dumpObjects);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/CHANGELOG.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CHANGELOG
1313
* Added support for customizing the dumped YAML string through an optional bit field:
1414

1515
```php
16-
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Yaml::DUMP_OBJECT);
16+
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE | Yaml::DUMP_OBJECT);
1717
```
1818

1919
3.0.0

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Dumper.php
+21-10Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,38 @@ public function setIndentation($num)
3838
/**
3939
* Dumps a PHP value to YAML.
4040
*
41-
* @param mixed $input The PHP value
42-
* @param int $inline The level where you switch to inline YAML
43-
* @param int $indent The level of indentation (used internally)
44-
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
45-
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
41+
* @param mixed $input The PHP value
42+
* @param int $inline The level where you switch to inline YAML
43+
* @param int $indent The level of indentation (used internally)
44+
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
4645
*
4746
* @return string The YAML representation of the PHP value
4847
*/
49-
public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $flags = 0)
48+
public function dump($input, $inline = 0, $indent = 0, $flags = 0)
5049
{
5150
if (is_bool($flags)) {
51+
@trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
52+
53+
if ($flags) {
54+
$flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
55+
} else {
56+
$flags = 0;
57+
}
58+
}
59+
60+
if (func_num_args() >= 5) {
5261
@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);
5362

54-
$flags = (int) $flags;
63+
if (func_get_arg(4)) {
64+
$flags |= Yaml::DUMP_OBJECT;
65+
}
5566
}
5667

5768
$output = '';
5869
$prefix = $indent ? str_repeat(' ', $indent) : '';
5970

6071
if ($inline <= 0 || !is_array($input) || empty($input)) {
61-
$output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $flags);
72+
$output .= $prefix.Inline::dump($input, $flags);
6273
} else {
6374
$isAHash = array_keys($input) !== range(0, count($input) - 1);
6475

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

6879
$output .= sprintf('%s%s%s%s',
6980
$prefix,
70-
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $flags).':' : '-',
81+
$isAHash ? Inline::dump($key, $flags).':' : '-',
7182
$willBeInlined ? ' ' : "\n",
72-
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $flags)
83+
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
7384
).($willBeInlined ? "\n" : '');
7485
}
7586
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+24-14Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,36 @@ public static function parse($value, $flags = 0, $references = array())
116116
/**
117117
* Dumps a given PHP variable to a YAML string.
118118
*
119-
* @param mixed $value The PHP variable to convert
120-
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
121-
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
119+
* @param mixed $value The PHP variable to convert
120+
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
122121
*
123122
* @return string The YAML string representing the PHP array
124123
*
125124
* @throws DumpException When trying to dump PHP resource
126125
*/
127-
public static function dump($value, $exceptionOnInvalidType = false, $flags = 0)
126+
public static function dump($value, $flags = 0)
128127
{
129128
if (is_bool($flags)) {
129+
@trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
130+
131+
if ($flags) {
132+
$flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
133+
} else {
134+
$flags = 0;
135+
}
136+
}
137+
138+
if (func_num_args() >= 3) {
130139
@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);
131140

132-
$flags = (int) $flags;
141+
if (func_get_arg(2)) {
142+
$flags |= Yaml::DUMP_OBJECT;
143+
}
133144
}
134145

135146
switch (true) {
136147
case is_resource($value):
137-
if ($exceptionOnInvalidType) {
148+
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
138149
throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
139150
}
140151

@@ -144,13 +155,13 @@ public static function dump($value, $exceptionOnInvalidType = false, $flags = 0)
144155
return '!php/object:'.serialize($value);
145156
}
146157

147-
if ($exceptionOnInvalidType) {
158+
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
148159
throw new DumpException('Object support when dumping a YAML file has been disabled.');
149160
}
150161

151162
return 'null';
152163
case is_array($value):
153-
return self::dumpArray($value, $exceptionOnInvalidType, $flags);
164+
return self::dumpArray($value, $flags);
154165
case null === $value:
155166
return 'null';
156167
case true === $value:
@@ -196,13 +207,12 @@ public static function dump($value, $exceptionOnInvalidType = false, $flags = 0)
196207
/**
197208
* Dumps a PHP array to a YAML string.
198209
*
199-
* @param array $value The PHP array to dump
200-
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
201-
* @param bool $objectSupport true if object support is enabled, false otherwise
210+
* @param array $value The PHP array to dump
211+
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
202212
*
203213
* @return string The YAML string representing the PHP array
204214
*/
205-
private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport)
215+
private static function dumpArray($value, $flags)
206216
{
207217
// array
208218
$keys = array_keys($value);
@@ -212,7 +222,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor
212222
) {
213223
$output = array();
214224
foreach ($value as $val) {
215-
$output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport);
225+
$output[] = self::dump($val, $flags);
216226
}
217227

218228
return sprintf('[%s]', implode(', ', $output));
@@ -221,7 +231,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor
221231
// mapping
222232
$output = array();
223233
foreach ($value as $key => $val) {
224-
$output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport));
234+
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
225235
}
226236

227237
return sprintf('{ %s }', implode(', ', $output));

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/DumperTest.php
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function testInlineLevel()
179179

180180
public function testObjectSupportEnabled()
181181
{
182-
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Yaml::DUMP_OBJECT);
182+
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_OBJECT);
183183

184184
$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');
185185
}
@@ -205,6 +205,15 @@ public function testObjectSupportDisabledButNoExceptions()
205205
* @expectedException \Symfony\Component\Yaml\Exception\DumpException
206206
*/
207207
public function testObjectSupportDisabledWithExceptions()
208+
{
209+
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
210+
}
211+
212+
/**
213+
* @group legacy
214+
* @expectedException \Symfony\Component\Yaml\Exception\DumpException
215+
*/
216+
public function testObjectSupportDisabledWithExceptionsPassingTrue()
208217
{
209218
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
210219
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Yaml.php
+20-8Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Yaml
2424
const PARSE_EXCEPTION_ON_INVALID_TYPE = 2;
2525
const PARSE_OBJECT = 4;
2626
const PARSE_OBJECT_FOR_MAP = 8;
27+
const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
2728

2829
/**
2930
* Parses YAML into a PHP value.
@@ -80,25 +81,36 @@ public static function parse($input, $flags = 0)
8081
* The dump method, when supplied with an array, will do its best
8182
* to convert the array into friendly YAML.
8283
*
83-
* @param array $array PHP array
84-
* @param int $inline The level where you switch to inline YAML
85-
* @param int $indent The amount of spaces to use for indentation of nested nodes.
86-
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
87-
* @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
84+
* @param array $array PHP array
85+
* @param int $inline The level where you switch to inline YAML
86+
* @param int $indent The amount of spaces to use for indentation of nested nodes.
87+
* @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
8888
*
8989
* @return string A YAML string representing the original PHP array
9090
*/
91-
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $flags = 0)
91+
public static function dump($array, $inline = 2, $indent = 4, $flags = 0)
9292
{
9393
if (is_bool($flags)) {
94+
@trigger_error('Passing a boolean flag to toggle exception handling is deprecated since version 3.1 and will be removed in 4.0. Use the DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
95+
96+
if ($flags) {
97+
$flags = self::DUMP_EXCEPTION_ON_INVALID_TYPE;
98+
} else {
99+
$flags = 0;
100+
}
101+
}
102+
103+
if (func_num_args() >= 5) {
94104
@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);
95105

96-
$flags = (int) $flags;
106+
if (func_get_arg(4)) {
107+
$flags |= self::DUMP_OBJECT;
108+
}
97109
}
98110

99111
$yaml = new Dumper();
100112
$yaml->setIndentation($indent);
101113

102-
return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $flags);
114+
return $yaml->dump($array, $inline, 0, $flags);
103115
}
104116
}

0 commit comments

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