From 3464282bd19ea3b12aee7184fe46773ba635b555 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 1 Mar 2016 21:23:51 +0100 Subject: [PATCH] ensure dump indentation to be greather than zero --- src/Symfony/Component/Yaml/Dumper.php | 4 ++++ .../Component/Yaml/Tests/DumperTest.php | 18 ++++++++++++++++++ src/Symfony/Component/Yaml/Tests/YamlTest.php | 18 ++++++++++++++++++ src/Symfony/Component/Yaml/Yaml.php | 4 ++++ 4 files changed, 44 insertions(+) diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index 39cdcfc536a1b..21351a5c34fc9 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -32,6 +32,10 @@ class Dumper */ public function setIndentation($num) { + if ($num < 1) { + throw new \InvalidArgumentException('The indentation must be greater than zero.'); + } + $this->indentation = (int) $num; } diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index b1c44c44befce..84ce02edda4c8 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -229,6 +229,24 @@ public function getEscapeSequences() 'paragraph-separator' => array("\t\\P", '"\t\\\\P"'), ); } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The indentation must be greater than zero + */ + public function testZeroIndentationThrowsException() + { + $this->dumper->setIndentation(0); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The indentation must be greater than zero + */ + public function testNegativeIndentationThrowsException() + { + $this->dumper->setIndentation(-4); + } } class A diff --git a/src/Symfony/Component/Yaml/Tests/YamlTest.php b/src/Symfony/Component/Yaml/Tests/YamlTest.php index 633978d63bd50..2b5917462ba51 100644 --- a/src/Symfony/Component/Yaml/Tests/YamlTest.php +++ b/src/Symfony/Component/Yaml/Tests/YamlTest.php @@ -28,4 +28,22 @@ public function testParseAndDump() $parsedByContents = Yaml::parse($contents); $this->assertEquals($parsedByFilename, $parsedByContents); } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The indentation must be greater than zero + */ + public function testZeroIndentationThrowsException() + { + Yaml::dump(array('lorem' => 'ipsum', 'dolor' => 'sit'), 2, 0); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The indentation must be greater than zero + */ + public function testNegativeIndentationThrowsException() + { + Yaml::dump(array('lorem' => 'ipsum', 'dolor' => 'sit'), 2, -4); + } } diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index 2e14a6c08352d..2b34e96ceb60e 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -83,6 +83,10 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup */ public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false) { + if ($indent < 1) { + throw new \InvalidArgumentException('The indentation must be greater than zero.'); + } + $yaml = new Dumper(); $yaml->setIndentation($indent);