From 61f5baac4a24f2311d55ea7d596aa361bea3ea92 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Thu, 16 Apr 2015 01:09:29 +0100 Subject: [PATCH] GH-543 - Datetime at DefaultValueBinder Personally, I don't call it a bug if people pass invalid arguments and it complains with an exception; but I've made some basic changes to support DateTime objects and objects with a __toString() method... Any other objects will throw a catchable fatal error, and it's up to user code to implement any exception handler to convert it to an exception and handle it --- Classes/PHPExcel/Cell/DefaultValueBinder.php | 11 +++- .../PHPExcel/Cell/DefaultValueBinderTest.php | 54 ++++++++++++++++++- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/Classes/PHPExcel/Cell/DefaultValueBinder.php b/Classes/PHPExcel/Cell/DefaultValueBinder.php index a9dae410..252048f7 100644 --- a/Classes/PHPExcel/Cell/DefaultValueBinder.php +++ b/Classes/PHPExcel/Cell/DefaultValueBinder.php @@ -57,13 +57,20 @@ class PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder // sanitize UTF-8 strings if (is_string($value)) { $value = PHPExcel_Shared_String::SanitizeUTF8($value); + } elseif (is_object($value)) { + // Handle any objects that might be injected + if ($value instanceof DateTime) { + $value = $value->format('Y-m-d H:i:s'); + } elseif (!($value instanceof PHPExcel_RichText)) { + $value = (string) $value; + } } // Set value explicit $cell->setValueExplicit( $value, self::dataTypeForValue($value) ); // Done! - return TRUE; + return true; } /** @@ -74,7 +81,7 @@ class PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder */ public static function dataTypeForValue($pValue = null) { // Match the value against a few data types - if (is_null($pValue)) { + if ($pValue === null) { return PHPExcel_Cell_DataType::TYPE_NULL; } elseif ($pValue === '') { return PHPExcel_Cell_DataType::TYPE_STRING; diff --git a/unitTests/Classes/PHPExcel/Cell/DefaultValueBinderTest.php b/unitTests/Classes/PHPExcel/Cell/DefaultValueBinderTest.php index 59b1dd04..1c74d96f 100644 --- a/unitTests/Classes/PHPExcel/Cell/DefaultValueBinderTest.php +++ b/unitTests/Classes/PHPExcel/Cell/DefaultValueBinderTest.php @@ -4,6 +4,7 @@ require_once 'testDataFileIterator.php'; class DefaultValueBinderTest extends PHPUnit_Framework_TestCase { + protected $cellStub; public function setUp() { @@ -14,6 +15,48 @@ class DefaultValueBinderTest extends PHPUnit_Framework_TestCase require_once(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); } + protected function createCellStub() + { + // Create a stub for the Cell class. + $this->cellStub = $this->getMockBuilder('PHPExcel_Cell') + ->disableOriginalConstructor() + ->getMock(); + // Configure the stub. + $this->cellStub->expects($this->any()) + ->method('setValueExplicit') + ->will($this->returnValue(true)); + + } + + /** + * @dataProvider binderProvider + */ + public function testBindValue($value) + { + $this->createCellStub(); + $binder = new PHPExcel_Cell_DefaultValueBinder(); + $result = $binder->bindValue($this->cellStub, $value); + $this->assertTrue($result); + } + + public function binderProvider() + { + return array( + array(null), + array(''), + array('ABC'), + array('=SUM(A1:B2)'), + array(true), + array(false), + array(123), + array(-123.456), + array('123'), + array('-123.456'), + array('#REF!'), + array(new DateTime()), + ); + } + /** * @dataProvider providerDataTypeForValue */ @@ -21,7 +64,7 @@ class DefaultValueBinderTest extends PHPUnit_Framework_TestCase { $args = func_get_args(); $expectedResult = array_pop($args); - $result = call_user_func_array(array('PHPExcel_Cell_DefaultValueBinder','dataTypeForValue'),$args); + $result = call_user_func_array(array('PHPExcel_Cell_DefaultValueBinder','dataTypeForValue'), $args); $this->assertEquals($expectedResult, $result); } @@ -30,4 +73,13 @@ class DefaultValueBinderTest extends PHPUnit_Framework_TestCase return new testDataFileIterator('rawTestData/Cell/DefaultValueBinder.data'); } + public function testDataTypeForRichTextObject() + { + $objRichText = new PHPExcel_RichText(); + $objRichText->createText('Hello World'); + + $expectedResult = PHPExcel_Cell_DataType::TYPE_INLINE; + $result = call_user_func(array('PHPExcel_Cell_DefaultValueBinder','dataTypeForValue'), $objRichText); + $this->assertEquals($expectedResult, $result); + } }