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
This commit is contained in:
MarkBaker 2015-04-16 01:09:29 +01:00
parent eedcc49f81
commit 61f5baac4a
2 changed files with 62 additions and 3 deletions

View File

@ -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;

View File

@ -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
*/
@ -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);
}
}