Support overriding `DefaultValueBinder::dataTypeForValue()`

This allow to avoid overriding `DefaultValueBinder::bindValue()`

Fixes #735
This commit is contained in:
Milan Davídek 2018-10-23 05:40:41 +02:00 committed by Adrien Crivelli
parent fdc224af7c
commit 3be06a5e87
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
4 changed files with 37 additions and 7 deletions

View File

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- SUMIFS containing multiple conditions - [#704](https://github.com/PHPOffice/PhpSpreadsheet/issues/704)
- Csv reader avoid notice when the file is empty - [#743](https://github.com/PHPOffice/PhpSpreadsheet/pull/743)
- Fix print area parser for XLSX reader - [#734](https://github.com/PHPOffice/PhpSpreadsheet/pull/734)
- Support overriding `DefaultValueBinder::dataTypeForValue()` without overriding `DefaultValueBinder::bindValue()` - [#735](https://github.com/PHPOffice/PhpSpreadsheet/pull/735)
## [1.5.0] - 2018-10-21

View File

@ -31,7 +31,7 @@ class DefaultValueBinder implements IValueBinder
}
// Set value explicit
$cell->setValueExplicit($value, self::dataTypeForValue($value));
$cell->setValueExplicit($value, static::dataTypeForValue($value));
// Done!
return true;

View File

@ -12,18 +12,20 @@ use PHPUnit\Framework\TestCase;
class DefaultValueBinderTest extends TestCase
{
private $cellStub;
private function createCellStub()
{
// Create a stub for the Cell class.
$this->cellStub = $this->getMockBuilder(Cell::class)
/** @var Cell $cellStub */
$cellStub = $this->getMockBuilder(Cell::class)
->disableOriginalConstructor()
->getMock();
// Configure the stub.
$this->cellStub->expects($this->any())
$cellStub->expects($this->any())
->method('setValueExplicit')
->will($this->returnValue(true));
return $cellStub;
}
/**
@ -33,9 +35,9 @@ class DefaultValueBinderTest extends TestCase
*/
public function testBindValue($value)
{
$this->createCellStub();
$cellStub = $this->createCellStub();
$binder = new DefaultValueBinder();
$result = $binder->bindValue($this->cellStub, $value);
$result = $binder->bindValue($cellStub, $value);
self::assertTrue($result);
}
@ -83,4 +85,14 @@ class DefaultValueBinderTest extends TestCase
$result = DefaultValueBinder::dataTypeForValue($objRichText);
self::assertEquals($expectedResult, $result);
}
public function testCanOverrideStaticMethodWithoutOverridingBindValue()
{
$cellStub = $this->createCellStub();
$binder = new ValueBinderWithOverriddenDataTypeForValue();
self::assertFalse($binder::$called);
$binder->bindValue($cellStub, 123);
self::assertTrue($binder::$called);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
class ValueBinderWithOverriddenDataTypeForValue extends DefaultValueBinder
{
public static $called = false;
public static function dataTypeForValue($pValue)
{
self::$called = true;
return parent::dataTypeForValue($pValue);
}
}