Support DateTimeImmutable as cell value

This commit is contained in:
Adrien Crivelli 2017-12-23 21:50:07 +09:00
parent 6f76306de8
commit fb5f8d4763
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
3 changed files with 8 additions and 6 deletions

View File

@ -2,7 +2,7 @@
namespace PhpOffice\PhpSpreadsheet\Cell; namespace PhpOffice\PhpSpreadsheet\Cell;
use DateTime; use DateTimeInterface;
use PhpOffice\PhpSpreadsheet\RichText\RichText; use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
@ -16,14 +16,14 @@ class DefaultValueBinder implements IValueBinder
* *
* @return bool * @return bool
*/ */
public function bindValue(Cell $cell, $value = null) public function bindValue(Cell $cell, $value)
{ {
// sanitize UTF-8 strings // sanitize UTF-8 strings
if (is_string($value)) { if (is_string($value)) {
$value = StringHelper::sanitizeUTF8($value); $value = StringHelper::sanitizeUTF8($value);
} elseif (is_object($value)) { } elseif (is_object($value)) {
// Handle any objects that might be injected // Handle any objects that might be injected
if ($value instanceof DateTime) { if ($value instanceof DateTimeInterface) {
$value = $value->format('Y-m-d H:i:s'); $value = $value->format('Y-m-d H:i:s');
} elseif (!($value instanceof RichText)) { } elseif (!($value instanceof RichText)) {
$value = (string) $value; $value = (string) $value;

View File

@ -12,5 +12,5 @@ interface IValueBinder
* *
* @return bool * @return bool
*/ */
public function bindValue(Cell $cell, $value = null); public function bindValue(Cell $cell, $value);
} }

View File

@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Cell; namespace PhpOffice\PhpSpreadsheetTests\Cell;
use DateTime; use DateTime;
use DateTimeImmutable;
use PhpOffice\PhpSpreadsheet\Cell\Cell; use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder; use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
@ -11,9 +12,9 @@ use PHPUnit\Framework\TestCase;
class DefaultValueBinderTest extends TestCase class DefaultValueBinderTest extends TestCase
{ {
protected $cellStub; private $cellStub;
protected function createCellStub() private function createCellStub()
{ {
// Create a stub for the Cell class. // Create a stub for the Cell class.
$this->cellStub = $this->getMockBuilder(Cell::class) $this->cellStub = $this->getMockBuilder(Cell::class)
@ -53,6 +54,7 @@ class DefaultValueBinderTest extends TestCase
['-123.456'], ['-123.456'],
['#REF!'], ['#REF!'],
[new DateTime()], [new DateTime()],
[new DateTimeImmutable()],
]; ];
} }