From de4b207c4da670f86488d05ba4564b67018ffbcc Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 9 Feb 2016 23:58:46 +0100 Subject: [PATCH] deprecate the Dumper::setIndentation() method --- UPGRADE-3.1.md | 3 ++ UPGRADE-4.0.md | 3 ++ src/Symfony/Component/Yaml/Dumper.php | 12 +++++++- .../Component/Yaml/Tests/DumperTest.php | 28 +++++++++++++++++++ src/Symfony/Component/Yaml/Yaml.php | 3 +- 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/UPGRADE-3.1.md b/UPGRADE-3.1.md index 2920418afa2a4..f86bcc41efe17 100644 --- a/UPGRADE-3.1.md +++ b/UPGRADE-3.1.md @@ -33,6 +33,9 @@ Serializer Yaml ---- + * The `Dumper::setIndentation()` method is deprecated and will be removed in + Symfony 4.0. Pass the indentation level to the constructor instead. + * Deprecated support for passing `true`/`false` as the second argument to the `parse()` method to trigger exceptions when an invalid type was passed. diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index 0f79ed5a244cc..df1b8bf7b9c78 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -24,6 +24,9 @@ Serializer Yaml ---- + * The `Dumper::setIndentation()` method was removed. Pass the indentation + level to the constructor instead. + * Removed support for passing `true`/`false` as the second argument to the `parse()` method to trigger exceptions when an invalid type was passed. diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index a427e3c752db0..c6c17902357f3 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -23,7 +23,15 @@ class Dumper * * @var int */ - protected $indentation = 4; + protected $indentation; + + /** + * @param int $indentation + */ + public function __construct($indentation = 4) + { + $this->indentation = $indentation; + } /** * Sets the indentation. @@ -32,6 +40,8 @@ class Dumper */ public function setIndentation($num) { + @trigger_error('The '.__METHOD__.' method is deprecated since version 3.1 and will be removed in 4.0. Pass the indentation to the constructor instead.', E_USER_DEPRECATED); + $this->indentation = (int) $num; } diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index f5a9521f1c7de..b6e5365ad00a2 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -51,6 +51,34 @@ protected function tearDown() $this->array = null; } + public function testIndentationInConstructor() + { + $dumper = new Dumper(7); + $expected = <<<'EOF' +'': bar +foo: '#bar' +'foo''bar': { } +bar: + - 1 + - foo +foobar: + foo: bar + bar: + - 1 + - foo + foobar: + foo: bar + bar: + - 1 + - foo + +EOF; + $this->assertEquals($expected, $dumper->dump($this->array, 4, 0)); + } + + /** + * @group legacy + */ public function testSetIndentation() { $this->dumper->setIndentation(7); diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index b06a7250ed0e9..743fde22d34fc 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -96,8 +96,7 @@ public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvali $flags = (int) $flags; } - $yaml = new Dumper(); - $yaml->setIndentation($indent); + $yaml = new Dumper($indent); return $yaml->dump($array, $inline, 0, $exceptionOnInvalidType, $flags); }