Skip to content

Commit

Permalink
dumper flag to enable exceptions on invalid types
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Feb 9, 2016
1 parent a3b577f commit afb873f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 34 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -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
Expand Down
31 changes: 21 additions & 10 deletions Dumper.php
Expand Up @@ -38,27 +38,38 @@ public function setIndentation($num)
/**
* Dumps a PHP value to YAML.
*
* @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
* @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 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);

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

$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" : '');
}
}
Expand Down
38 changes: 24 additions & 14 deletions Inline.php
Expand Up @@ -116,25 +116,36 @@ public static function parse($value, $flags = 0, $references = array())
/**
* 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
* @param mixed $value The PHP variable to convert
* @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)));
}

Expand All @@ -144,13 +155,13 @@ public static function dump($value, $exceptionOnInvalidType = false, $flags = 0)
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:
Expand Down Expand Up @@ -196,13 +207,12 @@ public static function dump($value, $exceptionOnInvalidType = false, $flags = 0)
/**
* 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 array $value The PHP array to dump
* @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);
Expand All @@ -212,7 +222,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor
) {
$output = array();
foreach ($value as $val) {
$output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport);
$output[] = self::dump($val, $flags);
}

return sprintf('[%s]', implode(', ', $output));
Expand All @@ -221,7 +231,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor
// 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));
Expand Down
11 changes: 10 additions & 1 deletion Tests/DumperTest.php
Expand Up @@ -179,7 +179,7 @@ public function testInlineLevel()

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');
}
Expand All @@ -205,6 +205,15 @@ public function testObjectSupportDisabledButNoExceptions()
* @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);
}
Expand Down
28 changes: 20 additions & 8 deletions Yaml.php
Expand Up @@ -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.
Expand Down Expand Up @@ -80,25 +81,36 @@ public static function parse($input, $flags = 0)
* The dump method, when supplied with an array, will do its best
* to convert the array into friendly 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
* @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 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();
$yaml->setIndentation($indent);

return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $flags);
return $yaml->dump($array, $inline, 0, $flags);
}
}

0 comments on commit afb873f

Please sign in to comment.