Merge b7831cc93313a0e56186df41d11c9bdcfb881bae into 538f6109ada0cad7e49d191e886b1f3ceda4ffa6

This commit is contained in:
gdaszuta 2015-04-04 13:24:26 +00:00
commit 101583f41e
2 changed files with 31 additions and 6 deletions

View File

@ -0,0 +1,14 @@
<?php
namespace Box\Spout\Writer\Exception;
use Box\Spout\Common\Exception\SpoutException;
/**
* Class InvalidDataException
*
* @package Box\Spout\Writer\Exception
*/
class InvalidDataException extends SpoutException
{
}

View File

@ -3,6 +3,7 @@
namespace Box\Spout\Writer\Internal\XLSX; namespace Box\Spout\Writer\Internal\XLSX;
use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Exception\IOException;
use Box\Spout\Writer\Exception\InvalidDataException;
use Box\Spout\Writer\Helper\XLSX\CellHelper; use Box\Spout\Writer\Helper\XLSX\CellHelper;
/** /**
@ -119,6 +120,7 @@ EOD;
* Example $dataRow = ['data1', 1234, null, '', 'data5']; * Example $dataRow = ['data1', 1234, null, '', 'data5'];
* @return void * @return void
* @throws \Box\Spout\Common\Exception\IOException If the data cannot be written * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written
* @throws \Box\Spout\Writer\Exception\InvalidDataException If input data are of invalid type
*/ */
public function addRow($dataRow) public function addRow($dataRow)
{ {
@ -132,19 +134,28 @@ EOD;
$columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber); $columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber);
$data .= ' <c r="' . $columnIndex . $rowIndex . '"'; $data .= ' <c r="' . $columnIndex . $rowIndex . '"';
if (empty($cellValue)) { switch(true) {
$data .= '/>' . PHP_EOL; case gettype($cellValue) === 'integer':
} else { case gettype($cellValue) === 'boolean':
if (is_numeric($cellValue)) { case gettype($cellValue) === 'float':
case gettype($cellValue) === 'double':
$data .= '><v>' . $cellValue . '</v></c>' . PHP_EOL; $data .= '><v>' . $cellValue . '</v></c>' . PHP_EOL;
} else { break;
case gettype($cellValue) === 'object' && method_exists($cellValue, '__toString'):
$cellValue = (string)$cellValue;
case gettype($cellValue) === 'string':
if ($this->shouldUseInlineStrings) { if ($this->shouldUseInlineStrings) {
$data .= ' t="inlineStr"><is><t>' . $this->stringsEscaper->escape($cellValue) . '</t></is></c>' . PHP_EOL; $data .= ' t="inlineStr"><is><t>' . $this->stringsEscaper->escape($cellValue) . '</t></is></c>' . PHP_EOL;
} else { } else {
$sharedStringId = $this->sharedStringsHelper->writeString($cellValue); $sharedStringId = $this->sharedStringsHelper->writeString($cellValue);
$data .= ' t="s"><v>' . $sharedStringId . '</v></c>' . PHP_EOL; $data .= ' t="s"><v>' . $sharedStringId . '</v></c>' . PHP_EOL;
} }
} break;
case empty($cellValue):
$data .= '/>' . PHP_EOL;
break;
default:
throw new InvalidDataException("Invalid data type " . gettype($cellValue));
} }
$cellNumber++; $cellNumber++;