From b792a6cc7bcd953005edfba3edbab69fb80b3141 Mon Sep 17 00:00:00 2001 From: Fonata Date: Mon, 6 Jan 2020 18:04:45 +0100 Subject: [PATCH 1/2] Code quality: Removed superfluous brackets; should not change anything --- src/Csv.php | 2 +- tests/methods/HasToString.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/methods/HasToString.php diff --git a/src/Csv.php b/src/Csv.php index eee55de..319265f 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -1137,7 +1137,7 @@ class Csv { : ''; $enclosure_quoted = preg_quote($this->enclosure, '/'); $pattern = "/" . $delimiter_quoted . $enclosure_quoted . "|\n|\r/i"; - if ($this->enclose_all || preg_match($pattern, $value) || (strpos($value, ' ') === 0 || substr($value, -1) == ' ')) { + if ($this->enclose_all || preg_match($pattern, $value) || strpos($value, ' ') === 0 || substr($value, -1) == ' ') { $value = str_replace($this->enclosure, $this->enclosure . $this->enclosure, $value); $value = $this->enclosure . $value . $this->enclosure; } diff --git a/tests/methods/HasToString.php b/tests/methods/HasToString.php new file mode 100644 index 0000000..9c17166 --- /dev/null +++ b/tests/methods/HasToString.php @@ -0,0 +1,9 @@ + Date: Mon, 6 Jan 2020 18:07:42 +0100 Subject: [PATCH 2/2] Added test for the case of objects as cells I encountered this in Drupal 8, where cells implemented the MarkupInterface. It was objects that, when cast to a string, returned the translated string. Before 5ca540daa74d1a0325d8d720b41014d726dfa81a, this new test would fail. --- tests/methods/HasToString.php | 9 --------- tests/methods/ObjectThatHasToStringMethod.php | 13 +++++++++++++ tests/methods/UnparseTest.php | 12 ++++++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) delete mode 100644 tests/methods/HasToString.php create mode 100644 tests/methods/ObjectThatHasToStringMethod.php diff --git a/tests/methods/HasToString.php b/tests/methods/HasToString.php deleted file mode 100644 index 9c17166..0000000 --- a/tests/methods/HasToString.php +++ /dev/null @@ -1,9 +0,0 @@ -unparseAndCompare($expected); } + public function testObjectCells() { + $this->csv->data = [ + [ + 'column1' => new ObjectThatHasToStringMethod(), + 'column2' => 'boring', + ], + ]; + $this->csv->linefeed = "\n"; + $expected = "column1,column2\nsome value,boring\n"; + $this->unparseAndCompare($expected); + } + private function unparseAndCompare($expected, $fields = array()) { $str = $this->csv->unparse($this->csv->data, $fields); $this->assertEquals($expected, $str);