8000 [Yaml] dump customization option with dumper flags by xabbuh · Pull Request #17578 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] dump customization option with dumper flags #17578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions UPGRADE-3.1.md
8000
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,18 @@ Serializer
Yaml
----

* Deprecated support for passing `true`/`false` as the third argument to the `dump()` methods to toggle object support.

Before:

```php
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
```

After:

```php
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Yaml::DUMP_OBJECT);

* The `!!php/object` tag to indicate dumped PHP objects has been deprecated
and will be removed in Symfony 4.0. Use the `!php/object` tag instead.
14 changes: 14 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,19 @@ Serializer
Yaml
----

* Removed support for passing `true`/`false` as the third argument to the `dump()` methods to toggle object support.

Before:

```php
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);
```

After:

```php
Yaml::dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, Yaml::DUMP_OBJECT);
```

* The `!!php/object` tag to indicate dumped PHP objects was removed in favor of
the `!php/object` tag.
9 changes: 9 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
CHANGELOG
=========

3.1.0
-----

* 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);
```

3.0.0
-----

Expand Down
16 changes: 11 additions & 5 deletions src/Symfony/Component/Yaml/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,23 @@ public function setIndentation($num)
* @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 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 representation of the PHP value
*/
public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $objectSupport = false)
public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = false, $flags = 0)
{
if (is_bool($flags)) {
@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;
}

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

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

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

$output .= sprintf('%s%s%s%s',
$prefix,
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $objectSupport).':' : '-',
$isAHash ? Inline::dump($key, $exceptionOnInvalidType, $flags).':' : '-',
$willBeInlined ? ' ' : "\n",
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $objectSupport)
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $exceptionOnInvalidType, $flags)
).($willBeInlined ? "\n" : '');
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,20 @@ public static function parse($value, $exceptionOnInvalidType = false, $objectSup
*
* @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 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
*
* @throws DumpException When trying to dump PHP resource
*/
public static function dump($value, $exceptionOnInvalidType = false, $objectSupport = false)
public static function dump($value, $exceptionOnInvalidType = false, $flags = 0)
{
if (is_bool($flags)) {
@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;
}

switch (true) {
case is_resource($value):
if ($exceptionOnInvalidType) {
Expand All @@ -104,7 +110,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp

return 'null';
case is_object($value):
if ($objectSupport) {
if (Yaml::DUMP_OBJECT & $flags) {
return '!php/object:'.serialize($value);
}

Expand All @@ -114,7 +120,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp

return 'null';
case is_array($value):
return self::dumpArray($value, $exceptionOnInvalidType, $objectSupport);
return self::dumpArray($value, $exceptionOnInvalidType, $flags);
case null === $value:
return 'null';
case true === $value:
Expand Down
13 changes: 12 additions & 1 deletion src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Yaml;

class DumperTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -177,6 +178,16 @@ public function testInlineLevel()
}

public function testObjectSupportEnabled()
{
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, 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');
}

/**
* @group legacy
*/
public function testObjectSupportEnabledPassingTrue()
{
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true);

Expand All @@ -195,7 +206,7 @@ public function testObjectSupportDisabledButNoExceptions()
*/
public function testObjectSupportDisabledWithExceptions()
{
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true, false);
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
}

/**
Expand Down
9E81
14 changes: 11 additions & 3 deletions src/Symfony/Component/Yaml/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
class Yaml
{
const DUMP_OBJECT = 1;

/**
* Parses YAML into a PHP array.
*
Expand Down Expand Up @@ -58,15 +60,21 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup
* @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 bool $objectSupport true if object support is enabled, 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, $objectSupport = false)
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $flags = 0)
{
if (is_bool($flags)) {
@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;
}

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

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