Handle empty rows without E_WARNING when filling missing array indexes

In some cases, reading an XLSX file produce E_WARNING from the max()
call in the fillMissingArray() method. This commit fix the problem
by handling empty rows.
This commit is contained in:
Stian Liknes 2017-02-19 10:05:03 +01:00
parent 6f4ddb1569
commit 2f9a3e309e
2 changed files with 19 additions and 5 deletions

View File

@ -31,6 +31,9 @@ class CellHelper
*/
public static function fillMissingArrayIndexes($dataArray, $fillValue = '')
{
if (empty($dataArray)) {
return [];
}
$existingIndexes = array_keys($dataArray);
$newIndexes = array_fill_keys(range(0, max($existingIndexes)), $fillValue);

View File

@ -10,14 +10,25 @@ namespace Box\Spout\Reader\XLSX\Helper;
class CellHelperTest extends \PHPUnit_Framework_TestCase
{
/**
* @return void
* @return array
*/
public function testFillMissingArrayIndexes()
public function dataProviderForTestFillMissingArrayIndexes()
{
$arrayToFill = [1 => 1, 3 => 3];
$filledArray = CellHelper::fillMissingArrayIndexes($arrayToFill, 'FILL');
return [
[ null, [] ],
[ [], [] ],
[ [1 => 1, 3 => 3], ['FILL', 1, 'FILL', 3] ]
];
}
$expectedFilledArray = ['FILL', 1, 'FILL', 3];
/**
* @dataProvider dataProviderForTestFillMissingArrayIndexes
* @param array $arrayToFill
* @param array $expectedFilledArray
*/
public function testFillMissingArrayIndexes($arrayToFill, array $expectedFilledArray)
{
$filledArray = CellHelper::fillMissingArrayIndexes($arrayToFill, 'FILL');
$this->assertEquals($expectedFilledArray, $filledArray);
}