feature #17743 [Yaml] dumper flag for enabling exceptions on invalid type (xabbuh)
This PR was merged into the 3.1-dev branch.
Discussion
----------
[Yaml] dumper flag for enabling exceptions on invalid type
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | yes
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR |
Commits
-------
e572a64
dumper flag to enable exceptions on invalid types
This commit is contained in:
commit
0a44afa36f
@ -81,6 +81,21 @@ Yaml
|
||||
Yaml::parse('{ "foo": "bar", "fiz": "cat" }', Yaml::PARSE_OBJECT_FOR_MAP);
|
||||
```
|
||||
|
||||
* Deprecated support for passing `true`/`false` as the fourth argument to the
|
||||
`dump()` method to trigger exceptions when an invalid type was passed.
|
||||
|
||||
Before:
|
||||
|
||||
```php
|
||||
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
|
||||
```
|
||||
|
||||
After:
|
||||
|
||||
```php
|
||||
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
|
||||
```
|
||||
|
||||
* Deprecated support for passing `true`/`false` as the fifth argument to the `dump()` method to toggle object support.
|
||||
|
||||
Before:
|
||||
|
@ -78,6 +78,21 @@ Yaml
|
||||
Yaml::parse('{ "foo": "bar", "fiz": "cat" }', Yaml::PARSE_OBJECT_FOR_MAP);
|
||||
```
|
||||
|
||||
* Removed support for passing `true`/`false` as the fourth argument to the
|
||||
`dump()` method to trigger exceptions when an invalid type was passed.
|
||||
|
||||
Before:
|
||||
|
||||
```php
|
||||
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
|
||||
```
|
||||
|
||||
After:
|
||||
|
||||
```php
|
||||
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
|
||||
```
|
||||
|
||||
* Removed support for passing `true`/`false` as the fifth argument to the `dump()` method to toggle object support.
|
||||
|
||||
Before:
|
||||
|
@ -12,6 +12,7 @@
|
||||
namespace Symfony\Bridge\Twig\Extension;
|
||||
|
||||
use Symfony\Component\Yaml\Dumper as YamlDumper;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
/**
|
||||
* Provides integration of the Yaml component with Twig.
|
||||
@ -39,9 +40,16 @@ class YamlExtension extends \Twig_Extension
|
||||
$dumper = new YamlDumper();
|
||||
}
|
||||
|
||||
if (defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT') && is_bool($dumpObjects)) {
|
||||
if (defined('Symfony\Component\Yaml\Yaml::DUMP_OBJECT')) {
|
||||
if (is_bool($dumpObjects)) {
|
||||
@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);
|
||||
$dumpObjects = (int) $dumpObjects;
|
||||
|
||||
$flags = $dumpObjects ? Yaml::DUMP_OBJECT : 0;
|
||||
} else {
|
||||
$flags = $dumpObjects;
|
||||
}
|
||||
|
||||
return $dumper->dump($input, $inline, 0, $flags);
|
||||
}
|
||||
|
||||
return $dumper->dump($input, $inline, 0, false, $dumpObjects);
|
||||
|
@ -13,7 +13,7 @@ CHANGELOG
|
||||
* Added support for customizing the dumped YAML string through an optional bit field:
|
||||
|
||||
```php
|
||||
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Yaml::DUMP_OBJECT);
|
||||
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE | Yaml::DUMP_OBJECT);
|
||||
```
|
||||
|
||||
3.0.0
|
||||
|
@ -51,24 +51,35 @@ class Dumper
|
||||
* @param mixed $input The PHP value
|
||||
* @param int $inline The level where you switch to inline YAML
|
||||
* @param int $indent The level of indentation (used internally)
|
||||
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
|
||||
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
|
||||
*
|
||||
* @return string The YAML representation of the PHP value
|
||||
*/
|
||||
public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $flags = 0)
|
||||
public function dump($input, $inline = 0, $indent = 0, $flags = 0)
|
||||
{
|
||||
if (is_bool($flags)) {
|
||||
@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);
|
||||
|
||||
if ($flags) {
|
||||
$flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
|
||||
} else {
|
||||
$flags = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (func_num_args() >= 5) {
|
||||
@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);
|
||||
|
||||
$flags = (int) $flags;
|
||||
if (func_get_arg(4)) {
|
||||
$flags |= Yaml::DUMP_OBJECT;
|
||||
}
|
||||
}
|
||||
|
||||
$output = '';
|
||||
$prefix = $indent ? str_repeat(' ', $indent) : '';
|
||||
|
||||
if ($inline <= 0 || !is_array($input) || empty($input)) {
|
||||
$output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $flags);
|
||||
$output .= $prefix.Inline::dump($input, $flags);
|
||||
} else {
|
||||
$isAHash = array_keys($input) !== range(0, count($input) - 1);
|
||||
|
||||
@ -77,9 +88,9 @@ class Dumper
|
||||
|
||||
$output .= sprintf('%s%s%s%s',
|
||||
$prefix,
|
||||
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $flags).':' : '-',
|
||||
$isAHash ? Inline::dump($key, $flags).':' : '-',
|
||||
$willBeInlined ? ' ' : "\n",
|
||||
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $flags)
|
||||
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
|
||||
).($willBeInlined ? "\n" : '');
|
||||
}
|
||||
}
|
||||
|
@ -117,24 +117,35 @@ class Inline
|
||||
* Dumps a given PHP variable to a YAML string.
|
||||
*
|
||||
* @param mixed $value The PHP variable to convert
|
||||
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
|
||||
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
|
||||
*
|
||||
* @return string The YAML string representing the PHP array
|
||||
*
|
||||
* @throws DumpException When trying to dump PHP resource
|
||||
*/
|
||||
public static function dump($value, $exceptionOnInvalidType = false, $flags = 0)
|
||||
public static function dump($value, $flags = 0)
|
||||
{
|
||||
if (is_bool($flags)) {
|
||||
@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);
|
||||
|
||||
if ($flags) {
|
||||
$flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
|
||||
} else {
|
||||
$flags = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (func_num_args() >= 3) {
|
||||
@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);
|
||||
|
||||
$flags = (int) $flags;
|
||||
if (func_get_arg(2)) {
|
||||
$flags |= Yaml::DUMP_OBJECT;
|
||||
}
|
||||
}
|
||||
|
||||
switch (true) {
|
||||
case is_resource($value):
|
||||
if ($exceptionOnInvalidType) {
|
||||
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
|
||||
throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
|
||||
}
|
||||
|
||||
@ -144,13 +155,13 @@ class Inline
|
||||
return '!php/object:'.serialize($value);
|
||||
}
|
||||
|
||||
if ($exceptionOnInvalidType) {
|
||||
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
|
||||
throw new DumpException('Object support when dumping a YAML file has been disabled.');
|
||||
}
|
||||
|
||||
return 'null';
|
||||
case is_array($value):
|
||||
return self::dumpArray($value, $exceptionOnInvalidType, $flags);
|
||||
return self::dumpArray($value, $flags);
|
||||
case null === $value:
|
||||
return 'null';
|
||||
case true === $value:
|
||||
@ -197,12 +208,11 @@ class Inline
|
||||
* Dumps a PHP array to a YAML string.
|
||||
*
|
||||
* @param array $value The PHP array to dump
|
||||
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
|
||||
* @param bool $objectSupport true if object support is enabled, false otherwise
|
||||
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
|
||||
*
|
||||
* @return string The YAML string representing the PHP array
|
||||
*/
|
||||
private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport)
|
||||
private static function dumpArray($value, $flags)
|
||||
{
|
||||
// array
|
||||
$keys = array_keys($value);
|
||||
@ -212,7 +222,7 @@ class Inline
|
||||
) {
|
||||
$output = array();
|
||||
foreach ($value as $val) {
|
||||
$output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport);
|
||||
$output[] = self::dump($val, $flags);
|
||||
}
|
||||
|
||||
return sprintf('[%s]', implode(', ', $output));
|
||||
@ -221,7 +231,7 @@ class Inline
|
||||
// mapping
|
||||
$output = array();
|
||||
foreach ($value as $key => $val) {
|
||||
$output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport));
|
||||
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
|
||||
}
|
||||
|
||||
return sprintf('{ %s }', implode(', ', $output));
|
||||
|
@ -207,7 +207,7 @@ EOF;
|
||||
|
||||
public function testObjectSupportEnabled()
|
||||
{
|
||||
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Yaml::DUMP_OBJECT);
|
||||
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_OBJECT);
|
||||
|
||||
$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');
|
||||
}
|
||||
@ -233,6 +233,15 @@ EOF;
|
||||
* @expectedException \Symfony\Component\Yaml\Exception\DumpException
|
||||
*/
|
||||
public function testObjectSupportDisabledWithExceptions()
|
||||
{
|
||||
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedException \Symfony\Component\Yaml\Exception\DumpException
|
||||
*/
|
||||
public function testObjectSupportDisabledWithExceptionsPassingTrue()
|
||||
{
|
||||
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ class Yaml
|
||||
const PARSE_EXCEPTION_ON_INVALID_TYPE = 2;
|
||||
const PARSE_OBJECT = 4;
|
||||
const PARSE_OBJECT_FOR_MAP = 8;
|
||||
const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
|
||||
|
||||
/**
|
||||
* Parses YAML into a PHP value.
|
||||
@ -83,21 +84,32 @@ class Yaml
|
||||
* @param array $array PHP array
|
||||
* @param int $inline The level where you switch to inline YAML
|
||||
* @param int $indent The amount of spaces to use for indentation of nested nodes.
|
||||
* @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
|
||||
* @param int $flags A bit field of DUMP_* constants to customize the dumped YAML string
|
||||
*
|
||||
* @return string A YAML string representing the original PHP array
|
||||
*/
|
||||
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $flags = 0)
|
||||
public static function dump($array, $inline = 2, $indent = 4, $flags = 0)
|
||||
{
|
||||
if (is_bool($flags)) {
|
||||
@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);
|
||||
|
||||
if ($flags) {
|
||||
$flags = self::DUMP_EXCEPTION_ON_INVALID_TYPE;
|
||||
} else {
|
||||
$flags = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (func_num_args() >= 5) {
|
||||
@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);
|
||||
|
||||
$flags = (int) $flags;
|
||||
if (func_get_arg(4)) {
|
||||
$flags |= self::DUMP_OBJECT;
|
||||
}
|
||||
}
|
||||
|
||||
$yaml = new Dumper($indent);
|
||||
|
||||
return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $flags);
|
||||
return $yaml->dump($array, $inline, 0, $flags);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user