From f7c483adbd4d774530e9990663dee228a1e17743 Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Sun, 3 Jun 2018 22:20:45 +0200 Subject: [PATCH] Better support for errored cells --- src/Spout/Common/Entity/Cell.php | 4 ++-- src/Spout/Common/Entity/Row.php | 2 +- tests/Spout/Common/Entity/CellTest.php | 14 +++++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Spout/Common/Entity/Cell.php b/src/Spout/Common/Entity/Cell.php index 73e7040..bd7a730 100644 --- a/src/Spout/Common/Entity/Cell.php +++ b/src/Spout/Common/Entity/Cell.php @@ -88,7 +88,7 @@ class Cell */ public function getValue() { - return $this->value; + return !$this->isError() ? $this->value : null; } /** @@ -203,6 +203,6 @@ class Cell */ public function __toString() { - return (string) $this->value; + return (string) $this->getValue(); } } diff --git a/src/Spout/Common/Entity/Row.php b/src/Spout/Common/Entity/Row.php index abf610a..ccb6bf5 100644 --- a/src/Spout/Common/Entity/Row.php +++ b/src/Spout/Common/Entity/Row.php @@ -117,7 +117,7 @@ class Row public function toArray() { return array_map(function (Cell $cell) { - return !$cell->isError() ? $cell->getValue() : null; + return $cell->getValue(); }, $this->cells); } } diff --git a/tests/Spout/Common/Entity/CellTest.php b/tests/Spout/Common/Entity/CellTest.php index e5f763a..c2eae98 100644 --- a/tests/Spout/Common/Entity/CellTest.php +++ b/tests/Spout/Common/Entity/CellTest.php @@ -2,7 +2,9 @@ namespace Box\Spout\Common\Entity; -class CellTest extends \PHPUnit\Framework\TestCase +use PHPUnit\Framework\TestCase; + +class CellTest extends TestCase { /** * @return void @@ -70,4 +72,14 @@ class CellTest extends \PHPUnit\Framework\TestCase { $this->assertTrue((new Cell([]))->isError()); } + + /** + * @return void + */ + public function testErroredCellValueShouldBeNull() + { + $cell = new Cell([]); + $this->assertTrue($cell->isError()); + $this->assertNull($cell->getValue()); + } }