Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
209372462b | ||
|
|
4a5da2ad74 | ||
|
|
c48c07db99 | ||
|
|
a804be4844 | ||
|
|
4bfbb41c95 | ||
|
|
61b71bf379 | ||
|
|
3c3294061a | ||
|
|
1fde6d836a | ||
|
|
05489cda88 | ||
|
|
128db45f22 | ||
|
|
9a85d84a2e | ||
|
|
4407cffeff | ||
|
|
8d27b3097d | ||
|
|
2c6cb1ffe5 | ||
|
|
22daea5f9a | ||
|
|
ed0e8f79cc | ||
|
|
44d72d8245 | ||
|
|
728dd3b399 | ||
|
|
05a9a1b60a | ||
|
|
a76624a721 | ||
|
|
90cbb7b5a6 | ||
|
|
f55520661e | ||
|
|
cb5dae22e4 | ||
|
|
2f6193ce20 | ||
|
|
582da8403d | ||
|
|
8b666fc6cd | ||
|
|
f8aab6eefd | ||
|
|
c4c6dddb20 | ||
|
|
6c97141679 | ||
|
|
9c8cc05364 | ||
|
|
2a9400dfca | ||
|
|
8ef6bdac62 | ||
|
|
46c246e6a4 | ||
|
|
8fd606ae4f | ||
|
|
6767386daf | ||
|
|
3395d3abb3 |
No files matched your search
@@ -2,7 +2,7 @@
|
||||
|
||||
[](https://packagist.org/packages/box/spout)
|
||||
[](http://opensource.box.com/badges)
|
||||
[](http://travis-ci.org/box/spout)
|
||||
[](https://travis-ci.org/box/spout)
|
||||
[](https://scrutinizer-ci.com/g/box/spout/?branch=master)
|
||||
[](https://packagist.org/packages/box/spout)
|
||||
[](https://packagist.org/packages/box/spout)
|
||||
@@ -18,16 +18,9 @@ Join the community and come discuss about Spout: [.
|
||||
|
||||
Add "box/spout" as a dependency in your project's composer.json file:
|
||||
```json
|
||||
"require": {
|
||||
"box/spout": "~2.0"
|
||||
}
|
||||
Run the following command:
|
||||
```
|
||||
|
||||
Then run the install command from Composer:
|
||||
```
|
||||
php composer.phar install
|
||||
$ composer require box/spout
|
||||
```
|
||||
|
||||
### Manual installation
|
||||
@@ -62,8 +55,8 @@ Regardless of the file type, the interface to read a file is always the same:
|
||||
use Box\Spout\Reader\ReaderFactory;
|
||||
use Box\Spout\Common\Type;
|
||||
|
||||
$reader = ReaderFactory::create(Type::CSV); // for CSV files
|
||||
//$reader = ReaderFactory::create(Type::XLSX); // for XLSX files
|
||||
$reader = ReaderFactory::create(Type::XLSX); // for XLSX files
|
||||
//$reader = ReaderFactory::create(Type::CSV); // for CSV files
|
||||
//$reader = ReaderFactory::create(Type::ODS); // for ODS files
|
||||
|
||||
$reader->open($filePath);
|
||||
@@ -87,8 +80,8 @@ As with the reader, there is one common interface to write data to a file:
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
use Box\Spout\Common\Type;
|
||||
|
||||
$writer = WriterFactory::create(Type::CSV); // for CSV files
|
||||
//$writer = WriterFactory::create(Type::XLSX); // for XLSX files
|
||||
$writer = WriterFactory::create(Type::XLSX); // for XLSX files
|
||||
//$writer = WriterFactory::create(Type::CSV); // for CSV files
|
||||
//$writer = WriterFactory::create(Type::ODS); // for ODS files
|
||||
|
||||
$writer->openToFile($filePath); // write data to a file or to a PHP stream
|
||||
@@ -105,6 +98,8 @@ For XLSX and ODS files, the number of rows per sheet is limited to 1,048,576. By
|
||||
|
||||
## Advanced usage
|
||||
|
||||
If you are looking for how to perform some common, more advanced tasks with Spout, please take a look at the [Wiki](https://github.com/box/spout/wiki). It contains code snippets, ready to be used.
|
||||
|
||||
### Configuring the CSV reader and writer
|
||||
|
||||
It is possible to configure both the CSV reader and writer to specify the field separator as well as the field enclosure:
|
||||
@@ -282,10 +277,12 @@ If you just want to check that everything is working as expected, executing the
|
||||
If you want to run performance tests, you will need to checkout the `perf-tests` branch. Multiple test suites can then be run, depending on the expected output:
|
||||
|
||||
* `phpunit` - runs the whole test suite (unit + functional + performance tests)
|
||||
* `phpunit --testuite no-perf-tests` - only runs the unit and functional tests
|
||||
* `phpunit --testuite perf-tests` - only runs the performance tests
|
||||
* `phpunit --exclude-group perf-tests` - only runs the unit and functional tests
|
||||
* `phpunit --group perf-tests` - only runs the performance tests
|
||||
|
||||
For information, the performance tests take about one hour to run (processing 2 million rows files is not a quick thing).
|
||||
For information, the performance tests take about 30 minutes to run (processing 1 million rows files is not a quick thing).
|
||||
|
||||
> Performance tests status: [](https://travis-ci.org/box/spout)
|
||||
|
||||
|
||||
## Frequently Asked Questions
|
||||
|
||||
@@ -29,5 +29,11 @@
|
||||
"psr-4": {
|
||||
"Box\\Spout\\": "src/Spout"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.5.x-dev"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,12 @@ use Box\Spout\Common\Helper\EncodingHelper;
|
||||
*/
|
||||
class RowIterator implements IteratorInterface
|
||||
{
|
||||
/**
|
||||
* If no value is given to stream_get_line(), it defaults to 8192 (which may be too low).
|
||||
* Alignement with other functions like fgets() is discussed here: https://bugs.php.net/bug.php?id=48421
|
||||
*/
|
||||
const MAX_READ_BYTES_PER_LINE = 32768;
|
||||
|
||||
/** @var resource Pointer to the CSV file to read */
|
||||
protected $filePointer;
|
||||
|
||||
@@ -147,7 +153,7 @@ class RowIterator implements IteratorInterface
|
||||
{
|
||||
// Read until the EOL delimiter or EOF is reached. The delimiter's encoding needs to match the CSV's encoding.
|
||||
$encodedEOLDelimiter = $this->getEncodedEOLDelimiter();
|
||||
$encodedLineData = $this->globalFunctionsHelper->stream_get_line($this->filePointer, 0, $encodedEOLDelimiter);
|
||||
$encodedLineData = $this->globalFunctionsHelper->stream_get_line($this->filePointer, self::MAX_READ_BYTES_PER_LINE, $encodedEOLDelimiter);
|
||||
|
||||
// If the line could have been read, it can be converted to UTF-8
|
||||
$utf8EncodedLineData = ($encodedLineData !== false) ?
|
||||
|
||||
@@ -11,7 +11,7 @@ use Box\Spout\Reader\SheetInterface;
|
||||
*/
|
||||
class Sheet implements SheetInterface
|
||||
{
|
||||
/** @var RowIterator To iterate over the CSV's rows */
|
||||
/** @var \Box\Spout\Reader\CSV\RowIterator To iterate over the CSV's rows */
|
||||
protected $rowIterator;
|
||||
|
||||
/**
|
||||
@@ -28,7 +28,7 @@ class Sheet implements SheetInterface
|
||||
|
||||
/**
|
||||
* @api
|
||||
* @return RowIterator
|
||||
* @return \Box\Spout\Reader\CSV\RowIterator
|
||||
*/
|
||||
public function getRowIterator()
|
||||
{
|
||||
|
||||
@@ -12,7 +12,7 @@ use Box\Spout\Reader\IteratorInterface;
|
||||
*/
|
||||
class SheetIterator implements IteratorInterface
|
||||
{
|
||||
/** @var Sheet The CSV unique "sheet" */
|
||||
/** @var \Box\Spout\Reader\CSV\Sheet The CSV unique "sheet" */
|
||||
protected $sheet;
|
||||
|
||||
/** @var bool Whether the unique "sheet" has already been read */
|
||||
@@ -67,7 +67,7 @@ class SheetIterator implements IteratorInterface
|
||||
* Return the current element
|
||||
* @link http://php.net/manual/en/iterator.current.php
|
||||
*
|
||||
* @return Sheet
|
||||
* @return \Box\Spout\Reader\CSV\Sheet
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
|
||||
@@ -47,7 +47,6 @@ class CellValueFormatter
|
||||
|
||||
/**
|
||||
* Returns the (unescaped) correctly marshalled, cell value associated to the given XML node.
|
||||
* @TODO Add other types !!
|
||||
* @see http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#refTable13
|
||||
*
|
||||
* @param \DOMNode $node
|
||||
@@ -119,7 +118,8 @@ class CellValueFormatter
|
||||
protected function formatFloatCellValue($node)
|
||||
{
|
||||
$nodeValue = $node->getAttribute(self::XML_ATTRIBUTE_VALUE);
|
||||
$cellValue = is_int($nodeValue) ? intval($nodeValue) : floatval($nodeValue);
|
||||
$nodeIntValue = intval($nodeValue);
|
||||
$cellValue = ($nodeIntValue == $nodeValue) ? $nodeIntValue : floatval($nodeValue);
|
||||
return $cellValue;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,13 +13,13 @@ use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
*/
|
||||
class Sheet implements SheetInterface
|
||||
{
|
||||
/** @var RowIterator To iterate over sheet's rows */
|
||||
/** @var \Box\Spout\Reader\ODS\RowIterator To iterate over sheet's rows */
|
||||
protected $rowIterator;
|
||||
|
||||
/** @var int ID of the sheet */
|
||||
protected $id;
|
||||
|
||||
/** @var int Index of the sheet, based on order of creation (zero-based) */
|
||||
/** @var int Index of the sheet, based on order in the workbook (zero-based) */
|
||||
protected $index;
|
||||
|
||||
/** @var string Name of the sheet */
|
||||
@@ -27,7 +27,7 @@ class Sheet implements SheetInterface
|
||||
|
||||
/**
|
||||
* @param XMLReader $xmlReader XML Reader, positioned on the "<table:table>" element
|
||||
* @param int $sheetIndex Index of the sheet, based on order of creation (zero-based)
|
||||
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
|
||||
* @param string $sheetName Name of the sheet
|
||||
*/
|
||||
public function __construct($xmlReader, $sheetIndex, $sheetName)
|
||||
@@ -39,7 +39,7 @@ class Sheet implements SheetInterface
|
||||
|
||||
/**
|
||||
* @api
|
||||
* @return RowIterator
|
||||
* @return \Box\Spout\Reader\ODS\RowIterator
|
||||
*/
|
||||
public function getRowIterator()
|
||||
{
|
||||
@@ -48,7 +48,7 @@ class Sheet implements SheetInterface
|
||||
|
||||
/**
|
||||
* @api
|
||||
* @return int Index of the sheet, based on order of creation (zero-based)
|
||||
* @return int Index of the sheet, based on order in the workbook (zero-based)
|
||||
*/
|
||||
public function getIndex()
|
||||
{
|
||||
|
||||
@@ -102,7 +102,7 @@ class SheetIterator implements IteratorInterface
|
||||
* Return the current element
|
||||
* @link http://php.net/manual/en/iterator.current.php
|
||||
*
|
||||
* @return Sheet
|
||||
* @return \Box\Spout\Reader\ODS\Sheet
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
|
||||
@@ -151,6 +151,22 @@ class SimpleXMLElement
|
||||
return $doesElementExist ? $this->wrapSimpleXMLElement($realElement) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the immediate children.
|
||||
*
|
||||
* @return array The children
|
||||
*/
|
||||
public function children()
|
||||
{
|
||||
$children = [];
|
||||
|
||||
foreach ($this->simpleXMLElement->children() as $child) {
|
||||
$children[] = $this->wrapSimpleXMLElement($child);
|
||||
}
|
||||
|
||||
return $children;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
||||
@@ -12,6 +12,14 @@ use Box\Spout\Common\Exception\InvalidArgumentException;
|
||||
*/
|
||||
class CellHelper
|
||||
{
|
||||
// Using ord() is super slow... Using a pre-computed hash table instead.
|
||||
private static $columnLetterToIndexMapping = [
|
||||
'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6,
|
||||
'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13,
|
||||
'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20,
|
||||
'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24, 'Z' => 25,
|
||||
];
|
||||
|
||||
/**
|
||||
* Fills the missing indexes of an array with a given value.
|
||||
* For instance, $dataArray = []; $a[1] = 1; $a[3] = 3;
|
||||
@@ -50,34 +58,31 @@ class CellHelper
|
||||
}
|
||||
|
||||
$columnIndex = 0;
|
||||
$capitalAAsciiValue = ord('A');
|
||||
$capitalZAsciiValue = ord('Z');
|
||||
$step = $capitalZAsciiValue - $capitalAAsciiValue + 1;
|
||||
|
||||
// Remove row information
|
||||
$column = preg_replace('/\d/', '', $cellIndex);
|
||||
$columnLength = strlen($column);
|
||||
$columnLetters = preg_replace('/\d/', '', $cellIndex);
|
||||
|
||||
/*
|
||||
* This is how the following loop will process the data:
|
||||
* A => 0
|
||||
* Z => 25
|
||||
* AA => 26 : (26^(2-1) * (0+1)) + 0
|
||||
* AB => 27 : (26^(2-1) * (0+1)) + 1
|
||||
* BC => 54 : (26^(2-1) * (1+1)) + 2
|
||||
* BCZ => 1455 : (26^(3-1) * (1+1)) + (26^(2-1) * (2+1)) + 25
|
||||
*/
|
||||
foreach (str_split($column) as $single_cell_index)
|
||||
{
|
||||
$currentColumnIndex = ord($single_cell_index) - $capitalAAsciiValue;
|
||||
// strlen() is super slow too... Using isset() is way faster and not too unreadable,
|
||||
// since we checked before that there are between 1 and 3 letters.
|
||||
$columnLength = isset($columnLetters[1]) ? (isset($columnLetters[2]) ? 3 : 2) : 1;
|
||||
|
||||
if ($columnLength === 1) {
|
||||
$columnIndex += $currentColumnIndex;
|
||||
} else {
|
||||
$columnIndex += pow($step, ($columnLength - 1)) * ($currentColumnIndex + 1);
|
||||
}
|
||||
|
||||
$columnLength--;
|
||||
// Looping over the different letters of the column is slower than this method.
|
||||
// Also, not using the pow() function because it's slooooow...
|
||||
switch ($columnLength) {
|
||||
case 1:
|
||||
$columnIndex = (self::$columnLetterToIndexMapping[$columnLetters]);
|
||||
break;
|
||||
case 2:
|
||||
$firstLetterIndex = (self::$columnLetterToIndexMapping[$columnLetters[0]] + 1) * 26;
|
||||
$secondLetterIndex = self::$columnLetterToIndexMapping[$columnLetters[1]];
|
||||
$columnIndex = $firstLetterIndex + $secondLetterIndex;
|
||||
break;
|
||||
case 3:
|
||||
$firstLetterIndex = (self::$columnLetterToIndexMapping[$columnLetters[0]] + 1) * 676;
|
||||
$secondLetterIndex = (self::$columnLetterToIndexMapping[$columnLetters[1]] + 1) * 26;
|
||||
$thirdLetterIndex = self::$columnLetterToIndexMapping[$columnLetters[2]];
|
||||
$columnIndex = $firstLetterIndex + $secondLetterIndex + $thirdLetterIndex;
|
||||
break;
|
||||
}
|
||||
|
||||
return $columnIndex;
|
||||
@@ -86,12 +91,13 @@ class CellHelper
|
||||
/**
|
||||
* Returns whether a cell index is valid, in an Excel world.
|
||||
* To be valid, the cell index should start with capital letters and be followed by numbers.
|
||||
* There can only be 3 letters, as there can only be 16,384 rows, which is equivalent to 'XFE'.
|
||||
*
|
||||
* @param string $cellIndex The Excel cell index ('A1', 'BC13', ...)
|
||||
* @return bool
|
||||
*/
|
||||
protected static function isValidCellIndex($cellIndex)
|
||||
{
|
||||
return (preg_match('/^[A-Z]+\d+$/', $cellIndex) === 1);
|
||||
return (preg_match('/^[A-Z]{1,3}\d+$/', $cellIndex) === 1);
|
||||
}
|
||||
}
|
||||
@@ -25,19 +25,34 @@ class CellValueFormatter
|
||||
|
||||
/** Definition of XML attributes used to parse data */
|
||||
const XML_ATTRIBUTE_TYPE = 't';
|
||||
const XML_ATTRIBUTE_STYLE_ID = 's';
|
||||
|
||||
/** Constants used for date formatting */
|
||||
const NUM_SECONDS_IN_ONE_DAY = 86400;
|
||||
|
||||
/**
|
||||
* February 29th, 1900 is NOT a leap year but Excel thinks it is...
|
||||
* @see https://en.wikipedia.org/wiki/Year_1900_problem#Microsoft_Excel
|
||||
*/
|
||||
const ERRONEOUS_EXCEL_LEAP_YEAR_DAY = 60;
|
||||
|
||||
/** @var SharedStringsHelper Helper to work with shared strings */
|
||||
protected $sharedStringsHelper;
|
||||
|
||||
/** @var StyleHelper Helper to work with styles */
|
||||
protected $styleHelper;
|
||||
|
||||
/** @var \Box\Spout\Common\Escaper\XLSX Used to unescape XML data */
|
||||
protected $escaper;
|
||||
|
||||
/**
|
||||
* @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
|
||||
* @param StyleHelper $styleHelper Helper to work with styles
|
||||
*/
|
||||
public function __construct($sharedStringsHelper)
|
||||
public function __construct($sharedStringsHelper, $styleHelper)
|
||||
{
|
||||
$this->sharedStringsHelper = $sharedStringsHelper;
|
||||
$this->styleHelper = $styleHelper;
|
||||
|
||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||
$this->escaper = new \Box\Spout\Common\Escaper\XLSX();
|
||||
@@ -53,6 +68,7 @@ class CellValueFormatter
|
||||
{
|
||||
// Default cell type is "n"
|
||||
$cellType = $node->getAttribute(self::XML_ATTRIBUTE_TYPE) ?: self::CELL_TYPE_NUMERIC;
|
||||
$cellStyleId = intval($node->getAttribute(self::XML_ATTRIBUTE_STYLE_ID));
|
||||
$vNodeValue = $this->getVNodeValue($node);
|
||||
|
||||
if (($vNodeValue === '') && ($cellType !== self::CELL_TYPE_INLINE_STRING)) {
|
||||
@@ -69,7 +85,7 @@ class CellValueFormatter
|
||||
case self::CELL_TYPE_BOOLEAN:
|
||||
return $this->formatBooleanCellValue($vNodeValue);
|
||||
case self::CELL_TYPE_NUMERIC:
|
||||
return $this->formatNumericCellValue($vNodeValue);
|
||||
return $this->formatNumericCellValue($vNodeValue, $cellStyleId);
|
||||
case self::CELL_TYPE_DATE:
|
||||
return $this->formatDateCellValue($vNodeValue);
|
||||
default:
|
||||
@@ -138,14 +154,59 @@ class CellValueFormatter
|
||||
|
||||
/**
|
||||
* Returns the cell Numeric value from string of nodeValue.
|
||||
* The value can also represent a timestamp and a DateTime will be returned.
|
||||
*
|
||||
* @param string $nodeValue
|
||||
* @return int|float The value associated with the cell
|
||||
* @param int $cellStyleId 0 being the default style
|
||||
* @return int|float|\DateTime|null The value associated with the cell
|
||||
*/
|
||||
protected function formatNumericCellValue($nodeValue)
|
||||
protected function formatNumericCellValue($nodeValue, $cellStyleId)
|
||||
{
|
||||
$cellValue = is_int($nodeValue) ? intval($nodeValue) : floatval($nodeValue);
|
||||
return $cellValue;
|
||||
// Numeric values can represent numbers as well as timestamps.
|
||||
// We need to look at the style of the cell to determine whether it is one or the other.
|
||||
$shouldFormatAsDate = $this->styleHelper->shouldFormatNumericValueAsDate($cellStyleId);
|
||||
|
||||
if ($shouldFormatAsDate) {
|
||||
return $this->formatExcelTimestampValue(floatval($nodeValue));
|
||||
} else {
|
||||
$nodeIntValue = intval($nodeValue);
|
||||
return ($nodeIntValue == $nodeValue) ? $nodeIntValue : floatval($nodeValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a cell's PHP Date value, associated to the given timestamp.
|
||||
* NOTE: The timestamp is a float representing the number of days since January 1st, 1900.
|
||||
*
|
||||
* @param float $nodeValue
|
||||
* @return \DateTime|null The value associated with the cell or NULL if invalid date value
|
||||
*/
|
||||
protected function formatExcelTimestampValue($nodeValue)
|
||||
{
|
||||
// Fix for the erroneous leap year in Excel
|
||||
if (ceil($nodeValue) > self::ERRONEOUS_EXCEL_LEAP_YEAR_DAY) {
|
||||
--$nodeValue;
|
||||
}
|
||||
|
||||
// The value 1.0 represents 1900-01-01. Numbers below 1.0 are not valid Excel dates.
|
||||
if ($nodeValue < 1.0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Do not use any unix timestamps for calculation to prevent
|
||||
// issues with numbers exceeding 2^31.
|
||||
$secondsRemainder = fmod($nodeValue, 1) * self::NUM_SECONDS_IN_ONE_DAY;
|
||||
$secondsRemainder = round($secondsRemainder, 0);
|
||||
|
||||
try {
|
||||
$cellValue = \DateTime::createFromFormat('|Y-m-d', '1899-12-31');
|
||||
$cellValue->modify('+' . intval($nodeValue) . 'days');
|
||||
$cellValue->modify('+' . $secondsRemainder . 'seconds');
|
||||
|
||||
return $cellValue;
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -143,7 +143,9 @@ class FileBasedStrategy implements CachingStrategyInterface
|
||||
}
|
||||
|
||||
$sharedString = null;
|
||||
if (array_key_exists($indexInFile, $this->inMemoryTempFileContents)) {
|
||||
|
||||
// Using isset here because it is way faster than array_key_exists...
|
||||
if (isset($this->inMemoryTempFileContents[$indexInFile])) {
|
||||
$escapedSharedString = $this->inMemoryTempFileContents[$indexInFile];
|
||||
$sharedString = $this->unescapeLineFeed($escapedSharedString);
|
||||
}
|
||||
|
||||
@@ -13,9 +13,6 @@ use Box\Spout\Reader\XLSX\Sheet;
|
||||
*/
|
||||
class SheetHelper
|
||||
{
|
||||
/** Extension for XML files */
|
||||
const XML_EXTENSION = '.xml';
|
||||
|
||||
/** Paths of XML files relative to the XLSX file root */
|
||||
const CONTENT_TYPES_XML_FILE_PATH = '[Content_Types].xml';
|
||||
const WORKBOOK_XML_RELS_FILE_PATH = 'xl/_rels/workbook.xml.rels';
|
||||
@@ -79,9 +76,15 @@ class SheetHelper
|
||||
$sheetNode = $sheetNodes[$i];
|
||||
$sheetDataXMLFilePath = $sheetNode->getAttribute('PartName');
|
||||
|
||||
$sheets[] = $this->getSheetFromXML($sheetDataXMLFilePath, $i);
|
||||
$sheets[] = $this->getSheetFromXML($sheetDataXMLFilePath);
|
||||
}
|
||||
|
||||
// make sure the sheets are sorted by index
|
||||
// (as the sheets are not necessarily in this order in the XML file)
|
||||
usort($sheets, function ($sheet1, $sheet2) {
|
||||
return ($sheet1->getIndex() - $sheet2->getIndex());
|
||||
});
|
||||
|
||||
return $sheets;
|
||||
}
|
||||
|
||||
@@ -91,60 +94,37 @@ class SheetHelper
|
||||
* Then we look at "xl/worbook.xml" to find the sheet entry associated to the found ID.
|
||||
* The entry contains the ID and name of the sheet.
|
||||
*
|
||||
* If this piece of data can't be found by parsing the different XML files, the ID will default
|
||||
* to the sheet index, based on order in [Content_Types].xml. Similarly, the sheet's name will
|
||||
* default to the data sheet XML file name ("xl/worksheets/sheet2.xml" => "sheet2").
|
||||
*
|
||||
* @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
|
||||
* @param int $sheetIndexZeroBased Index of the sheet, based on order in [Content_Types].xml (zero-based)
|
||||
* @return \Box\Spout\Reader\XLSX\Sheet Sheet instance
|
||||
*/
|
||||
protected function getSheetFromXML($sheetDataXMLFilePath, $sheetIndexZeroBased)
|
||||
protected function getSheetFromXML($sheetDataXMLFilePath)
|
||||
{
|
||||
$sheetName = $this->getDefaultSheetName($sheetDataXMLFilePath);
|
||||
|
||||
/*
|
||||
* In [Content_Types].xml, the path is "/xl/worksheets/sheet1.xml"
|
||||
* In workbook.xml.rels, it is only "worksheets/sheet1.xml"
|
||||
*/
|
||||
// In [Content_Types].xml, the path is "/xl/worksheets/sheet1.xml"
|
||||
// In workbook.xml.rels, it is only "worksheets/sheet1.xml"
|
||||
$sheetDataXMLFilePathInWorkbookXMLRels = ltrim($sheetDataXMLFilePath, '/xl/');
|
||||
|
||||
// find the node associated to the given file path
|
||||
$workbookXMLResElement = $this->getWorkbookXMLRelsAsXMLElement();
|
||||
$relationshipNodes = $workbookXMLResElement->xpath('//ns:Relationship[@Target="' . $sheetDataXMLFilePathInWorkbookXMLRels . '"]');
|
||||
$relationshipNode = $relationshipNodes[0];
|
||||
|
||||
if (count($relationshipNodes) === 1) {
|
||||
$relationshipNode = $relationshipNodes[0];
|
||||
$sheetId = $relationshipNode->getAttribute('Id');
|
||||
$relationshipSheetId = $relationshipNode->getAttribute('Id');
|
||||
|
||||
$workbookXMLElement = $this->getWorkbookXMLAsXMLElement();
|
||||
$sheetNodes = $workbookXMLElement->xpath('//ns:sheet[@r:id="' . $sheetId . '"]');
|
||||
$workbookXMLElement = $this->getWorkbookXMLAsXMLElement();
|
||||
$sheetNodes = $workbookXMLElement->xpath('//ns:sheet[@r:id="' . $relationshipSheetId . '"]');
|
||||
$sheetNode = $sheetNodes[0];
|
||||
|
||||
if (count($sheetNodes) === 1) {
|
||||
$sheetNode = $sheetNodes[0];
|
||||
$escapedSheetName = $sheetNode->getAttribute('name');
|
||||
$escapedSheetName = $sheetNode->getAttribute('name');
|
||||
$sheetIdOneBased = $sheetNode->getAttribute('sheetId');
|
||||
$sheetIndexZeroBased = $sheetIdOneBased - 1;
|
||||
|
||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||
$escaper = new \Box\Spout\Common\Escaper\XLSX();
|
||||
$sheetName = $escaper->unescape($escapedSheetName);
|
||||
}
|
||||
}
|
||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||
$escaper = new \Box\Spout\Common\Escaper\XLSX();
|
||||
$sheetName = $escaper->unescape($escapedSheetName);
|
||||
|
||||
return new Sheet($this->filePath, $sheetDataXMLFilePath, $this->sharedStringsHelper, $sheetIndexZeroBased, $sheetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default name of the sheet whose data is located
|
||||
* at the given path.
|
||||
*
|
||||
* @param string $sheetDataXMLFilePath Path of the sheet data XML file
|
||||
* @return string The default sheet name
|
||||
*/
|
||||
protected function getDefaultSheetName($sheetDataXMLFilePath)
|
||||
{
|
||||
return $this->globalFunctionsHelper->basename($sheetDataXMLFilePath, self::XML_EXTENSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a representation of the workbook.xml.rels file, ready to be parsed.
|
||||
* The returned value is cached.
|
||||
|
||||
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\XLSX\Helper;
|
||||
|
||||
use Box\Spout\Reader\Wrapper\SimpleXMLElement;
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
|
||||
/**
|
||||
* Class StyleHelper
|
||||
* This class provides helper functions related to XLSX styles
|
||||
*
|
||||
* @package Box\Spout\Reader\XLSX\Helper
|
||||
*/
|
||||
class StyleHelper
|
||||
{
|
||||
/** Paths of XML files relative to the XLSX file root */
|
||||
const STYLES_XML_FILE_PATH = 'xl/styles.xml';
|
||||
|
||||
/** Nodes used to find relevant information in the styles XML file */
|
||||
const XML_NODE_NUM_FMTS = 'numFmts';
|
||||
const XML_NODE_NUM_FMT = 'numFmt';
|
||||
const XML_NODE_CELL_XFS = 'cellXfs';
|
||||
const XML_NODE_XF = 'xf';
|
||||
|
||||
/** Attributes used to find relevant information in the styles XML file */
|
||||
const XML_ATTRIBUTE_NUM_FMT_ID = 'numFmtId';
|
||||
const XML_ATTRIBUTE_FORMAT_CODE = 'formatCode';
|
||||
const XML_ATTRIBUTE_APPLY_NUMBER_FORMAT = 'applyNumberFormat';
|
||||
|
||||
/** By convention, default style ID is 0 */
|
||||
const DEFAULT_STYLE_ID = 0;
|
||||
|
||||
/** @var string Path of the XLSX file being read */
|
||||
protected $filePath;
|
||||
|
||||
/** @var array Array containing a mapping NUM_FMT_ID => FORMAT_CODE */
|
||||
protected $customNumberFormats;
|
||||
|
||||
/** @var array Array containing a mapping STYLE_ID => [STYLE_ATTRIBUTES] */
|
||||
protected $stylesAttributes;
|
||||
|
||||
/**
|
||||
* @param string $filePath Path of the XLSX file being read
|
||||
*/
|
||||
public function __construct($filePath)
|
||||
{
|
||||
$this->filePath = $filePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the styles.xml file and extract the relevant information from the file.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function extractRelevantInfo()
|
||||
{
|
||||
$this->customNumberFormats = [];
|
||||
$this->stylesAttributes = [];
|
||||
|
||||
$stylesXmlFilePath = $this->filePath .'#' . self::STYLES_XML_FILE_PATH;
|
||||
$xmlReader = new XMLReader();
|
||||
|
||||
if ($xmlReader->open('zip://' . $stylesXmlFilePath)) {
|
||||
while ($xmlReader->read()) {
|
||||
if ($xmlReader->isPositionedOnStartingNode(self::XML_NODE_NUM_FMTS)) {
|
||||
$numFmtsNode = new SimpleXMLElement($xmlReader->readOuterXml());
|
||||
$this->extractNumberFormats($numFmtsNode);
|
||||
|
||||
} else if ($xmlReader->isPositionedOnStartingNode(self::XML_NODE_CELL_XFS)) {
|
||||
$cellXfsNode = new SimpleXMLElement($xmlReader->readOuterXml());
|
||||
$this->extractStyleAttributes($cellXfsNode);
|
||||
}
|
||||
}
|
||||
|
||||
$xmlReader->close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts number formats from the "numFmt" nodes.
|
||||
* For simplicity, the styles attributes are kept in memory. This is possible thanks
|
||||
* to the reuse of formats. So 1 million cells should not use 1 million formats.
|
||||
*
|
||||
* @param SimpleXMLElement $numFmtsNode The "numFmts" node
|
||||
* @return void
|
||||
*/
|
||||
protected function extractNumberFormats($numFmtsNode)
|
||||
{
|
||||
foreach ($numFmtsNode->children() as $numFmtNode) {
|
||||
$numFmtId = intval($numFmtNode->getAttribute(self::XML_ATTRIBUTE_NUM_FMT_ID));
|
||||
$formatCode = $numFmtNode->getAttribute(self::XML_ATTRIBUTE_FORMAT_CODE);
|
||||
$this->customNumberFormats[$numFmtId] = $formatCode;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts style attributes from the "xf" nodes, inside the "cellXfs" section.
|
||||
* For simplicity, the styles attributes are kept in memory. This is possible thanks
|
||||
* to the reuse of styles. So 1 million cells should not use 1 million styles.
|
||||
*
|
||||
* @param SimpleXMLElement $cellXfsNode The "cellXfs" node
|
||||
* @return void
|
||||
*/
|
||||
protected function extractStyleAttributes($cellXfsNode)
|
||||
{
|
||||
foreach ($cellXfsNode->children() as $xfNode) {
|
||||
$this->stylesAttributes[] = [
|
||||
self::XML_ATTRIBUTE_NUM_FMT_ID => intval($xfNode->getAttribute(self::XML_ATTRIBUTE_NUM_FMT_ID)),
|
||||
self::XML_ATTRIBUTE_APPLY_NUMBER_FORMAT => !!($xfNode->getAttribute(self::XML_ATTRIBUTE_APPLY_NUMBER_FORMAT)),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array The custom number formats
|
||||
*/
|
||||
protected function getCustomNumberFormats()
|
||||
{
|
||||
if (!isset($this->customNumberFormats)) {
|
||||
$this->extractRelevantInfo();
|
||||
}
|
||||
|
||||
return $this->customNumberFormats;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array The styles attributes
|
||||
*/
|
||||
protected function getStylesAttributes()
|
||||
{
|
||||
if (!isset($this->stylesAttributes)) {
|
||||
$this->extractRelevantInfo();
|
||||
}
|
||||
|
||||
return $this->stylesAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the style with the given ID should consider
|
||||
* numeric values as timestamps and format the cell as a date.
|
||||
*
|
||||
* @param int $styleId Zero-based style ID
|
||||
* @return bool Whether the cell with the given cell should display a date instead of a numeric value
|
||||
*/
|
||||
public function shouldFormatNumericValueAsDate($styleId)
|
||||
{
|
||||
$stylesAttributes = $this->getStylesAttributes();
|
||||
|
||||
// Default style (0) does not format numeric values as timestamps. Only custom styles do.
|
||||
// Also if the style ID does not exist in the styles.xml file, format as numeric value.
|
||||
// Using isset here because it is way faster than array_key_exists...
|
||||
if ($styleId === self::DEFAULT_STYLE_ID || !isset($stylesAttributes[$styleId])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$styleAttributes = $stylesAttributes[$styleId];
|
||||
|
||||
$applyNumberFormat = $styleAttributes[self::XML_ATTRIBUTE_APPLY_NUMBER_FORMAT];
|
||||
if (!$applyNumberFormat) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$numFmtId = $styleAttributes[self::XML_ATTRIBUTE_NUM_FMT_ID];
|
||||
return $this->doesNumFmtIdIndicateDate($numFmtId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $numFmtId
|
||||
* @return bool Whether the number format ID indicates that the number is a timestamp
|
||||
*/
|
||||
protected function doesNumFmtIdIndicateDate($numFmtId)
|
||||
{
|
||||
return (
|
||||
$this->isNumFmtIdBuiltInDateFormat($numFmtId) ||
|
||||
$this->isNumFmtIdCustomDateFormat($numFmtId)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $numFmtId
|
||||
* @return bool Whether the number format ID indicates that the number is a timestamp
|
||||
*/
|
||||
protected function isNumFmtIdBuiltInDateFormat($numFmtId)
|
||||
{
|
||||
$builtInDateFormatIds = [14, 15, 16, 17, 18, 19, 20, 21, 22, 45, 46, 47];
|
||||
return in_array($numFmtId, $builtInDateFormatIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $numFmtId
|
||||
* @return bool Whether the number format ID indicates that the number is a timestamp
|
||||
*/
|
||||
protected function isNumFmtIdCustomDateFormat($numFmtId)
|
||||
{
|
||||
$customNumberFormats = $this->getCustomNumberFormats();
|
||||
|
||||
// Using isset here because it is way faster than array_key_exists...
|
||||
if (!isset($customNumberFormats[$numFmtId])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$customNumberFormat = $customNumberFormats[$numFmtId];
|
||||
|
||||
// Remove extra formatting (what's between [ ], the brackets should not be preceded by a "\")
|
||||
$pattern = '((?<!\\\)\[.+?(?<!\\\)\])';
|
||||
$customNumberFormat = preg_replace($pattern, '', $customNumberFormat);
|
||||
|
||||
// custom date formats contain specific characters to represent the date:
|
||||
// e - yy - m - d - h - s
|
||||
// and all of their variants (yyyy - mm - dd...)
|
||||
$dateFormatCharacters = ['e', 'yy', 'm', 'd', 'h', 's'];
|
||||
|
||||
$hasFoundDateFormatCharacter = false;
|
||||
foreach ($dateFormatCharacters as $dateFormatCharacter) {
|
||||
// character not preceded by "\"
|
||||
$pattern = '/(?<!\\\)' . $dateFormatCharacter . '/';
|
||||
|
||||
if (preg_match($pattern, $customNumberFormat)) {
|
||||
$hasFoundDateFormatCharacter = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $hasFoundDateFormatCharacter;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use Box\Spout\Reader\IteratorInterface;
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
use Box\Spout\Reader\XLSX\Helper\CellHelper;
|
||||
use Box\Spout\Reader\XLSX\Helper\CellValueFormatter;
|
||||
use Box\Spout\Reader\XLSX\Helper\StyleHelper;
|
||||
|
||||
/**
|
||||
* Class RowIterator
|
||||
@@ -39,6 +40,9 @@ class RowIterator implements IteratorInterface
|
||||
/** @var Helper\CellValueFormatter Helper to format cell values */
|
||||
protected $cellValueFormatter;
|
||||
|
||||
/** @var Helper\StyleHelper $styleHelper Helper to work with styles */
|
||||
protected $styleHelper;
|
||||
|
||||
/** @var int Number of read rows */
|
||||
protected $numReadRows = 0;
|
||||
|
||||
@@ -62,7 +66,9 @@ class RowIterator implements IteratorInterface
|
||||
$this->sheetDataXMLFilePath = $this->normalizeSheetDataXMLFilePath($sheetDataXMLFilePath);
|
||||
|
||||
$this->xmlReader = new XMLReader();
|
||||
$this->cellValueFormatter = new CellValueFormatter($sharedStringsHelper);
|
||||
|
||||
$this->styleHelper = new StyleHelper($filePath);
|
||||
$this->cellValueFormatter = new CellValueFormatter($sharedStringsHelper, $this->styleHelper);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,10 +12,10 @@ use Box\Spout\Reader\SheetInterface;
|
||||
*/
|
||||
class Sheet implements SheetInterface
|
||||
{
|
||||
/** @var RowIterator To iterate over sheet's rows */
|
||||
/** @var \Box\Spout\Reader\XLSX\RowIterator To iterate over sheet's rows */
|
||||
protected $rowIterator;
|
||||
|
||||
/** @var int Index of the sheet, based on order of creation (zero-based) */
|
||||
/** @var int Index of the sheet, based on order in the workbook (zero-based) */
|
||||
protected $index;
|
||||
|
||||
/** @var string Name of the sheet */
|
||||
@@ -25,7 +25,7 @@ class Sheet implements SheetInterface
|
||||
* @param string $filePath Path of the XLSX file being read
|
||||
* @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
|
||||
* @param Helper\SharedStringsHelper Helper to work with shared strings
|
||||
* @param int $sheetIndex Index of the sheet, based on order of creation (zero-based)
|
||||
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
|
||||
* @param string $sheetName Name of the sheet
|
||||
*/
|
||||
public function __construct($filePath, $sheetDataXMLFilePath, $sharedStringsHelper, $sheetIndex, $sheetName)
|
||||
@@ -37,7 +37,7 @@ class Sheet implements SheetInterface
|
||||
|
||||
/**
|
||||
* @api
|
||||
* @return RowIterator
|
||||
* @return \Box\Spout\Reader\XLSX\RowIterator
|
||||
*/
|
||||
public function getRowIterator()
|
||||
{
|
||||
@@ -46,7 +46,7 @@ class Sheet implements SheetInterface
|
||||
|
||||
/**
|
||||
* @api
|
||||
* @return int Index of the sheet, based on order of creation (zero-based)
|
||||
* @return int Index of the sheet, based on order in the workbook (zero-based)
|
||||
*/
|
||||
public function getIndex()
|
||||
{
|
||||
|
||||
@@ -14,7 +14,7 @@ use Box\Spout\Reader\Exception\NoSheetsFoundException;
|
||||
*/
|
||||
class SheetIterator implements IteratorInterface
|
||||
{
|
||||
/** @var Sheet[] The list of sheet present in the file */
|
||||
/** @var \Box\Spout\Reader\XLSX\Sheet[] The list of sheet present in the file */
|
||||
protected $sheets;
|
||||
|
||||
/** @var int The index of the sheet being read (zero-based) */
|
||||
@@ -67,7 +67,8 @@ class SheetIterator implements IteratorInterface
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
if (array_key_exists($this->currentSheetIndex, $this->sheets)) {
|
||||
// Using isset here because it is way faster than array_key_exists...
|
||||
if (isset($this->sheets[$this->currentSheetIndex])) {
|
||||
$currentSheet = $this->sheets[$this->currentSheetIndex];
|
||||
$currentSheet->getRowIterator()->end();
|
||||
|
||||
@@ -79,7 +80,7 @@ class SheetIterator implements IteratorInterface
|
||||
* Return the current element
|
||||
* @link http://php.net/manual/en/iterator.current.php
|
||||
*
|
||||
* @return Sheet
|
||||
* @return \Box\Spout\Reader\XLSX\Sheet
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
|
||||
@@ -56,7 +56,9 @@ abstract class AbstractStyleHelper
|
||||
protected function hasStyleAlreadyBeenRegistered($style)
|
||||
{
|
||||
$serializedStyle = $style->serialize();
|
||||
return array_key_exists($serializedStyle, $this->serializedStyleToStyleIdMappingTable);
|
||||
|
||||
// Using isset here because it is way faster than array_key_exists...
|
||||
return isset($this->serializedStyleToStyleIdMappingTable[$serializedStyle]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,6 +10,9 @@ namespace Box\Spout\Writer\Common\Helper;
|
||||
*/
|
||||
class CellHelper
|
||||
{
|
||||
/** @var array Cache containing the mapping column index => cell index */
|
||||
private static $columnIndexToCellIndexCache = [];
|
||||
|
||||
/**
|
||||
* Returns the cell index (base 26) associated to the base 10 column index.
|
||||
* Excel uses A to Z letters for column indexing, where A is the 1st column,
|
||||
@@ -21,18 +24,26 @@ class CellHelper
|
||||
*/
|
||||
public static function getCellIndexFromColumnIndex($columnIndex)
|
||||
{
|
||||
$cellIndex = '';
|
||||
$capitalAAsciiValue = ord('A');
|
||||
$originalColumnIndex = $columnIndex;
|
||||
|
||||
do {
|
||||
$modulus = $columnIndex % 26;
|
||||
$cellIndex = chr($capitalAAsciiValue + $modulus) . $cellIndex;
|
||||
// Using isset here because it is way faster than array_key_exists...
|
||||
if (!isset(self::$columnIndexToCellIndexCache[$originalColumnIndex])) {
|
||||
$cellIndex = '';
|
||||
$capitalAAsciiValue = ord('A');
|
||||
|
||||
// substracting 1 because it's zero-based
|
||||
$columnIndex = intval($columnIndex / 26) - 1;
|
||||
} while ($columnIndex >= 0);
|
||||
do {
|
||||
$modulus = $columnIndex % 26;
|
||||
$cellIndex = chr($capitalAAsciiValue + $modulus) . $cellIndex;
|
||||
|
||||
return $cellIndex;
|
||||
// substracting 1 because it's zero-based
|
||||
$columnIndex = intval($columnIndex / 26) - 1;
|
||||
|
||||
} while ($columnIndex >= 0);
|
||||
|
||||
self::$columnIndexToCellIndexCache[$originalColumnIndex] = $cellIndex;
|
||||
}
|
||||
|
||||
return self::$columnIndexToCellIndexCache[$originalColumnIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,52 +12,142 @@ class ZipHelper
|
||||
{
|
||||
const ZIP_EXTENSION = '.zip';
|
||||
|
||||
/** Controls what to do when trying to add an existing file */
|
||||
const EXISTING_FILES_SKIP = 'skip';
|
||||
const EXISTING_FILES_OVERWRITE = 'overwrite';
|
||||
|
||||
/** @var string Path of the folder where the zip file will be created */
|
||||
protected $tmpFolderPath;
|
||||
|
||||
/** @var \ZipArchive The ZipArchive instance */
|
||||
protected $zip;
|
||||
|
||||
/**
|
||||
* Zips the root folder and streams the contents of the zip into the given stream
|
||||
*
|
||||
* @param string $folderPath Path to the folder to be zipped
|
||||
* @param resource $streamPointer Pointer to the stream to copy the zip
|
||||
* @return void
|
||||
* @param string $tmpFolderPath Path of the temp folder where the zip file will be created
|
||||
*/
|
||||
public function zipFolderAndCopyToStream($folderPath, $streamPointer)
|
||||
public function __construct($tmpFolderPath)
|
||||
{
|
||||
$zipFilePath = $this->getZipFilePath($folderPath);
|
||||
$this->zipFolder($folderPath, $zipFilePath);
|
||||
$this->copyZipToStream($zipFilePath, $streamPointer);
|
||||
$this->tmpFolderPath = $tmpFolderPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the already created ZipArchive instance or
|
||||
* creates one if none exists.
|
||||
*
|
||||
* @return \ZipArchive
|
||||
*/
|
||||
protected function createOrGetZip()
|
||||
{
|
||||
if (!isset($this->zip)) {
|
||||
$this->zip = new \ZipArchive();
|
||||
$zipFilePath = $this->getZipFilePath();
|
||||
|
||||
$this->zip->open($zipFilePath, \ZipArchive::CREATE|\ZipArchive::OVERWRITE);
|
||||
}
|
||||
|
||||
return $this->zip;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $folderPathToZip Path to the folder to be zipped
|
||||
* @return string Path where the zip file of the given folder will be created
|
||||
*/
|
||||
public function getZipFilePath($folderPathToZip)
|
||||
public function getZipFilePath()
|
||||
{
|
||||
return $folderPathToZip . self::ZIP_EXTENSION;
|
||||
return $this->tmpFolderPath . self::ZIP_EXTENSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zips the given folder
|
||||
* Adds the given file, located under the given root folder to the archive.
|
||||
* The file will be compressed.
|
||||
*
|
||||
* @param string $folderPath Path of the folder to be zipped
|
||||
* @param string $destinationPath Path where the zip file will be created
|
||||
* Example of use:
|
||||
* addFileToArchive('/tmp/xlsx/foo', 'bar/baz.xml');
|
||||
* => will add the file located at '/tmp/xlsx/foo/bar/baz.xml' in the archive, but only as 'bar/baz.xml'
|
||||
*
|
||||
* @param string $rootFolderPath Path of the root folder that will be ignored in the archive tree.
|
||||
* @param string $localFilePath Path of the file to be added, under the root folder
|
||||
* @param string|void $existingFileMode Controls what to do when trying to add an existing file
|
||||
* @return void
|
||||
*/
|
||||
public function zipFolder($folderPath, $destinationPath)
|
||||
public function addFileToArchive($rootFolderPath, $localFilePath, $existingFileMode = self::EXISTING_FILES_OVERWRITE)
|
||||
{
|
||||
$zip = new \ZipArchive();
|
||||
if ($zip->open($destinationPath, \ZipArchive::CREATE)) {
|
||||
$this->addFolderToZip($zip, $folderPath);
|
||||
$zip->close();
|
||||
$this->addFileToArchiveWithCompressionMethod(
|
||||
$rootFolderPath,
|
||||
$localFilePath,
|
||||
$existingFileMode,
|
||||
\ZipArchive::CM_DEFAULT
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given file, located under the given root folder to the archive.
|
||||
* The file will NOT be compressed.
|
||||
*
|
||||
* Example of use:
|
||||
* addUncompressedFileToArchive('/tmp/xlsx/foo', 'bar/baz.xml');
|
||||
* => will add the file located at '/tmp/xlsx/foo/bar/baz.xml' in the archive, but only as 'bar/baz.xml'
|
||||
*
|
||||
* @param string $rootFolderPath Path of the root folder that will be ignored in the archive tree.
|
||||
* @param string $localFilePath Path of the file to be added, under the root folder
|
||||
* @param string|void $existingFileMode Controls what to do when trying to add an existing file
|
||||
* @return void
|
||||
*/
|
||||
public function addUncompressedFileToArchive($rootFolderPath, $localFilePath, $existingFileMode = self::EXISTING_FILES_OVERWRITE)
|
||||
{
|
||||
$this->addFileToArchiveWithCompressionMethod(
|
||||
$rootFolderPath,
|
||||
$localFilePath,
|
||||
$existingFileMode,
|
||||
\ZipArchive::CM_STORE
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given file, located under the given root folder to the archive.
|
||||
* The file will NOT be compressed.
|
||||
*
|
||||
* Example of use:
|
||||
* addUncompressedFileToArchive('/tmp/xlsx/foo', 'bar/baz.xml');
|
||||
* => will add the file located at '/tmp/xlsx/foo/bar/baz.xml' in the archive, but only as 'bar/baz.xml'
|
||||
*
|
||||
* @param string $rootFolderPath Path of the root folder that will be ignored in the archive tree.
|
||||
* @param string $localFilePath Path of the file to be added, under the root folder
|
||||
* @param string $existingFileMode Controls what to do when trying to add an existing file
|
||||
* @param int $compressionMethod The compression method
|
||||
* @return void
|
||||
*/
|
||||
protected function addFileToArchiveWithCompressionMethod($rootFolderPath, $localFilePath, $existingFileMode, $compressionMethod)
|
||||
{
|
||||
$zip = $this->createOrGetZip();
|
||||
|
||||
if (!$this->shouldSkipFile($zip, $localFilePath, $existingFileMode)) {
|
||||
$normalizedFullFilePath = $this->getNormalizedRealPath($rootFolderPath . '/' . $localFilePath);
|
||||
$zip->addFile($normalizedFullFilePath, $localFilePath);
|
||||
|
||||
if (self::canChooseCompressionMethod()) {
|
||||
$zip->setCompressionName($localFilePath, $compressionMethod);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \ZipArchive $zip
|
||||
* @param string $folderPath Path of the folder to add to the zip
|
||||
* @return bool Whether it is possible to choose the desired compression method to be used
|
||||
*/
|
||||
public static function canChooseCompressionMethod()
|
||||
{
|
||||
// setCompressionName() is a PHP7+ method...
|
||||
return (method_exists(new \ZipArchive(), 'setCompressionName'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $folderPath Path to the folder to be zipped
|
||||
* @param string|void $existingFileMode Controls what to do when trying to add an existing file
|
||||
* @return void
|
||||
*/
|
||||
protected function addFolderToZip($zip, $folderPath)
|
||||
public function addFolderToArchive($folderPath, $existingFileMode = self::EXISTING_FILES_OVERWRITE)
|
||||
{
|
||||
$zip = $this->createOrGetZip();
|
||||
|
||||
$folderRealPath = $this->getNormalizedRealPath($folderPath) . '/';
|
||||
$itemIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($folderPath, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
|
||||
|
||||
@@ -65,14 +155,26 @@ class ZipHelper
|
||||
$itemRealPath = $this->getNormalizedRealPath($itemInfo->getPathname());
|
||||
$itemLocalPath = str_replace($folderRealPath, '', $itemRealPath);
|
||||
|
||||
if ($itemInfo->isFile()) {
|
||||
if ($itemInfo->isFile() && !$this->shouldSkipFile($zip, $itemLocalPath, $existingFileMode)) {
|
||||
$zip->addFile($itemRealPath, $itemLocalPath);
|
||||
} else if ($itemInfo->isDir()) {
|
||||
$zip->addEmptyDir($itemLocalPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \ZipArchive $zip
|
||||
* @param string $itemLocalPath
|
||||
* @param string $existingFileMode
|
||||
* @return bool Whether the file should be added to the archive or skipped
|
||||
*/
|
||||
protected function shouldSkipFile($zip, $itemLocalPath, $existingFileMode)
|
||||
{
|
||||
// Skip files if:
|
||||
// - EXISTING_FILES_SKIP mode chosen
|
||||
// - File already exists in the archive
|
||||
return ($existingFileMode === self::EXISTING_FILES_SKIP && $zip->locateName($itemLocalPath) !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns canonicalized absolute pathname, containing only forward slashes.
|
||||
*
|
||||
@@ -85,16 +187,30 @@ class ZipHelper
|
||||
return str_replace(DIRECTORY_SEPARATOR, '/', $realPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the archive and copies it into the given stream
|
||||
*
|
||||
* @param resource $streamPointer Pointer to the stream to copy the zip
|
||||
* @return void
|
||||
*/
|
||||
public function closeArchiveAndCopyToStream($streamPointer)
|
||||
{
|
||||
$zip = $this->createOrGetZip();
|
||||
$zip->close();
|
||||
unset($this->zip);
|
||||
|
||||
$this->copyZipToStream($streamPointer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Streams the contents of the zip file into the given stream
|
||||
*
|
||||
* @param string $zipFilePath Path to the zip file
|
||||
* @param resource $pointer Pointer to the stream to copy the zip
|
||||
* @return void
|
||||
*/
|
||||
protected function copyZipToStream($zipFilePath, $pointer)
|
||||
protected function copyZipToStream($pointer)
|
||||
{
|
||||
$zipFilePointer = fopen($zipFilePath, 'r');
|
||||
$zipFilePointer = fopen($this->getZipFilePath(), 'r');
|
||||
stream_copy_to_stream($zipFilePointer, $pointer);
|
||||
fclose($zipFilePointer);
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class Sheet
|
||||
/** @var array Associative array [SHEET_INDEX] => [SHEET_NAME] keeping track of sheets' name to enforce uniqueness */
|
||||
protected static $SHEETS_NAME_USED = [];
|
||||
|
||||
/** @var int Index of the sheet, based on order of creation (zero-based) */
|
||||
/** @var int Index of the sheet, based on order in the workbook (zero-based) */
|
||||
protected $index;
|
||||
|
||||
/** @var string Name of the sheet */
|
||||
@@ -34,7 +34,7 @@ class Sheet
|
||||
protected $stringHelper;
|
||||
|
||||
/**
|
||||
* @param int $sheetIndex Index of the sheet, based on order of creation (zero-based)
|
||||
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
|
||||
*/
|
||||
public function __construct($sheetIndex)
|
||||
{
|
||||
@@ -45,7 +45,7 @@ class Sheet
|
||||
|
||||
/**
|
||||
* @api
|
||||
* @return int Index of the sheet, based on order of creation (zero-based)
|
||||
* @return int Index of the sheet, based on order in the workbook (zero-based)
|
||||
*/
|
||||
public function getIndex()
|
||||
{
|
||||
|
||||
@@ -103,14 +103,12 @@ class FileSystemHelper extends \Box\Spout\Common\Helper\FileSystemHelper
|
||||
protected function createManifestFile()
|
||||
{
|
||||
$manifestXmlFileContents = <<<EOD
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">
|
||||
<manifest:file-entry manifest:full-path="/" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/>
|
||||
<manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/>
|
||||
<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>
|
||||
<manifest:file-entry manifest:full-path="meta.xml" manifest:media-type="text/xml"/>
|
||||
<manifest:file-entry manifest:full-path="mimetype" manifest:media-type="text/plain"/>
|
||||
<manifest:file-entry manifest:full-path="META-INF/manifest.xml" manifest:media-type="text/xml"/>
|
||||
</manifest:manifest>
|
||||
EOD;
|
||||
|
||||
@@ -265,10 +263,17 @@ EOD;
|
||||
*/
|
||||
public function zipRootFolderAndCopyToStream($streamPointer)
|
||||
{
|
||||
$zipHelper = new ZipHelper();
|
||||
$zipHelper->zipFolderAndCopyToStream($this->rootFolder, $streamPointer);
|
||||
$zipHelper = new ZipHelper($this->rootFolder);
|
||||
|
||||
// In order to have the file's mime type detected properly, files need to be added
|
||||
// to the zip file in a particular order.
|
||||
// @see http://www.jejik.com/articles/2010/03/how_to_correctly_create_odf_documents_using_zip/
|
||||
$zipHelper->addUncompressedFileToArchive($this->rootFolder, self::MIMETYPE_FILE_NAME);
|
||||
|
||||
$zipHelper->addFolderToArchive($this->rootFolder, ZipHelper::EXISTING_FILES_SKIP);
|
||||
$zipHelper->closeArchiveAndCopyToStream($streamPointer);
|
||||
|
||||
// once the zip is copied, remove it
|
||||
$this->deleteFile($zipHelper->getZipFilePath($this->rootFolder));
|
||||
$this->deleteFile($zipHelper->getZipFilePath());
|
||||
}
|
||||
}
|
||||
@@ -146,7 +146,8 @@ class Worksheet implements WorksheetInterface
|
||||
for ($i = 0; $i < $cellsCount; $i++) {
|
||||
$currentCellValue = $dataRow[$currentCellIndex];
|
||||
|
||||
if (!array_key_exists($nextCellIndex, $dataRow) || $currentCellValue !== $dataRow[$nextCellIndex]) {
|
||||
// Using isset here because it is way faster than array_key_exists...
|
||||
if (!isset($dataRow[$nextCellIndex]) || $currentCellValue !== $dataRow[$nextCellIndex]) {
|
||||
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex);
|
||||
$data .= $this->getCellContent($currentCellValue, $styleIndex, $numTimesValueRepeated);
|
||||
|
||||
|
||||
@@ -293,7 +293,7 @@ class Style
|
||||
if (!$this->hasSetFontSize && $baseStyle->getFontSize() !== self::DEFAULT_FONT_SIZE) {
|
||||
$mergedStyle->setFontSize($baseStyle->getFontSize());
|
||||
}
|
||||
if (!$this->hasSetFontSize && $baseStyle->getFontColor() !== self::DEFAULT_FONT_COLOR) {
|
||||
if (!$this->hasSetFontColor && $baseStyle->getFontColor() !== self::DEFAULT_FONT_COLOR) {
|
||||
$mergedStyle->setFontColor($baseStyle->getFontColor());
|
||||
}
|
||||
if (!$this->hasSetFontName && $baseStyle->getFontName() !== self::DEFAULT_FONT_NAME) {
|
||||
|
||||
@@ -94,7 +94,7 @@ class FileSystemHelper extends \Box\Spout\Common\Helper\FileSystemHelper
|
||||
*/
|
||||
protected function createRootFolder()
|
||||
{
|
||||
$this->rootFolder = $this->createFolder($this->baseFolderPath, uniqid('xlsx'));
|
||||
$this->rootFolder = $this->createFolder($this->baseFolderPath, uniqid('xlsx', true));
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -353,10 +353,19 @@ EOD;
|
||||
*/
|
||||
public function zipRootFolderAndCopyToStream($streamPointer)
|
||||
{
|
||||
$zipHelper = new ZipHelper();
|
||||
$zipHelper->zipFolderAndCopyToStream($this->rootFolder, $streamPointer);
|
||||
$zipHelper = new ZipHelper($this->rootFolder);
|
||||
|
||||
// In order to have the file's mime type detected properly, files need to be added
|
||||
// to the zip file in a particular order.
|
||||
// "[Content_Types].xml" then at least 2 files located in "xl" folder should be zipped first.
|
||||
$zipHelper->addFileToArchive($this->rootFolder, self::CONTENT_TYPES_XML_FILE_NAME);
|
||||
$zipHelper->addFileToArchive($this->rootFolder, self::XL_FOLDER_NAME . '/' . self::WORKBOOK_XML_FILE_NAME);
|
||||
$zipHelper->addFileToArchive($this->rootFolder, self::XL_FOLDER_NAME . '/' . self::STYLES_XML_FILE_NAME);
|
||||
|
||||
$zipHelper->addFolderToArchive($this->rootFolder, ZipHelper::EXISTING_FILES_SKIP);
|
||||
$zipHelper->closeArchiveAndCopyToStream($streamPointer);
|
||||
|
||||
// once the zip is copied, remove it
|
||||
$this->deleteFile($zipHelper->getZipFilePath($this->rootFolder));
|
||||
$this->deleteFile($zipHelper->getZipFilePath());
|
||||
}
|
||||
}
|
||||
@@ -131,36 +131,38 @@ EOD;
|
||||
$rowIndex = $this->lastWrittenRowIndex + 1;
|
||||
$numCells = count($dataRow);
|
||||
|
||||
$data = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';
|
||||
$rowXML = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';
|
||||
|
||||
foreach($dataRow as $cellValue) {
|
||||
$columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber);
|
||||
$data .= '<c r="' . $columnIndex . $rowIndex . '"';
|
||||
$data .= ' s="' . $style->getId() . '"';
|
||||
$cellXML = '<c r="' . $columnIndex . $rowIndex . '"';
|
||||
$cellXML .= ' s="' . $style->getId() . '"';
|
||||
|
||||
if (CellHelper::isNonEmptyString($cellValue)) {
|
||||
if ($this->shouldUseInlineStrings) {
|
||||
$data .= ' t="inlineStr"><is><t>' . $this->stringsEscaper->escape($cellValue) . '</t></is></c>';
|
||||
$cellXML .= ' t="inlineStr"><is><t>' . $this->stringsEscaper->escape($cellValue) . '</t></is></c>';
|
||||
} else {
|
||||
$sharedStringId = $this->sharedStringsHelper->writeString($cellValue);
|
||||
$data .= ' t="s"><v>' . $sharedStringId . '</v></c>';
|
||||
$cellXML .= ' t="s"><v>' . $sharedStringId . '</v></c>';
|
||||
}
|
||||
} else if (CellHelper::isBoolean($cellValue)) {
|
||||
$data .= ' t="b"><v>' . $cellValue . '</v></c>';
|
||||
$cellXML .= ' t="b"><v>' . $cellValue . '</v></c>';
|
||||
} else if (CellHelper::isNumeric($cellValue)) {
|
||||
$data .= '><v>' . $cellValue . '</v></c>';
|
||||
$cellXML .= '><v>' . $cellValue . '</v></c>';
|
||||
} else if (empty($cellValue)) {
|
||||
$data .= '/>';
|
||||
// don't write empty cells (not appending to $cellXML is the right behavior!)
|
||||
$cellXML = '';
|
||||
} else {
|
||||
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue));
|
||||
}
|
||||
|
||||
$rowXML .= $cellXML;
|
||||
$cellNumber++;
|
||||
}
|
||||
|
||||
$data .= '</row>';
|
||||
$rowXML .= '</row>';
|
||||
|
||||
$wasWriteSuccessful = fwrite($this->sheetFilePointer, $data);
|
||||
$wasWriteSuccessful = fwrite($this->sheetFilePointer, $rowXML);
|
||||
if ($wasWriteSuccessful === false) {
|
||||
throw new IOException("Unable to write data in {$this->worksheetFilePath}");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\XLSX\Helper;
|
||||
|
||||
/**
|
||||
* Class CellValueFormatterTest
|
||||
*
|
||||
* @package Box\Spout\Reader\XLSX\Helper
|
||||
*/
|
||||
class CellValueFormatterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForTestExcelDate()
|
||||
{
|
||||
return [
|
||||
[CellValueFormatter::CELL_TYPE_NUMERIC, 42429, '2016-02-29 00:00:00'],
|
||||
[CellValueFormatter::CELL_TYPE_NUMERIC, '146098', '2299-12-31 00:00:00'],
|
||||
[CellValueFormatter::CELL_TYPE_NUMERIC, -700, null],
|
||||
[CellValueFormatter::CELL_TYPE_NUMERIC, 0, null],
|
||||
[CellValueFormatter::CELL_TYPE_NUMERIC, 0.5, null],
|
||||
[CellValueFormatter::CELL_TYPE_NUMERIC, 1, '1900-01-01 00:00:00'],
|
||||
[CellValueFormatter::CELL_TYPE_NUMERIC, 59.999988425926, '1900-02-28 23:59:59'],
|
||||
[CellValueFormatter::CELL_TYPE_NUMERIC, 60.458333333333, '1900-02-28 11:00:00'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForTestExcelDate
|
||||
*
|
||||
* @param string $cellType
|
||||
* @param int|float|string $nodeValue
|
||||
* @param string|null $expectedDateAsString
|
||||
* @return void
|
||||
*/
|
||||
public function testExcelDate($cellType, $nodeValue, $expectedDateAsString)
|
||||
{
|
||||
$nodeListMock = $this->getMockBuilder('DOMNodeList')->disableOriginalConstructor()->getMock();
|
||||
|
||||
$nodeListMock
|
||||
->expects($this->atLeastOnce())
|
||||
->method('item')
|
||||
->with(0)
|
||||
->will($this->returnValue((object)['nodeValue' => $nodeValue]));
|
||||
|
||||
$nodeMock = $this->getMockBuilder('DOMElement')->disableOriginalConstructor()->getMock();
|
||||
|
||||
$nodeMock
|
||||
->expects($this->atLeastOnce())
|
||||
->method('getAttribute')
|
||||
->will($this->returnValueMap([
|
||||
[CellValueFormatter::XML_ATTRIBUTE_TYPE, $cellType],
|
||||
[CellValueFormatter::XML_ATTRIBUTE_STYLE_ID, 123],
|
||||
]));
|
||||
|
||||
$nodeMock
|
||||
->expects($this->atLeastOnce())
|
||||
->method('getElementsByTagName')
|
||||
->with(CellValueFormatter::XML_NODE_VALUE)
|
||||
->will($this->returnValue($nodeListMock));
|
||||
|
||||
$styleHelperMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Helper\StyleHelper')->disableOriginalConstructor()->getMock();
|
||||
|
||||
$styleHelperMock
|
||||
->expects($this->once())
|
||||
->method('shouldFormatNumericValueAsDate')
|
||||
->with(123)
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$formatter = new CellValueFormatter(null, $styleHelperMock);
|
||||
$result = $formatter->extractAndFormatNodeValue($nodeMock);
|
||||
|
||||
if ($expectedDateAsString === null) {
|
||||
$this->assertNull($result);
|
||||
} else {
|
||||
$this->assertInstanceOf('DateTime', $result);
|
||||
$this->assertSame($expectedDateAsString, $result->format('Y-m-d H:i:s'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForTestFormatNumericCellValueWithNumbers()
|
||||
{
|
||||
return [
|
||||
[42, 42, 'integer'],
|
||||
[42.5, 42.5, 'double'],
|
||||
[-42, -42, 'integer'],
|
||||
[-42.5, -42.5, 'double'],
|
||||
['42', 42, 'integer'],
|
||||
['42.5', 42.5, 'double'],
|
||||
[865640023012945, 865640023012945, 'integer'],
|
||||
['865640023012945', 865640023012945, 'integer'],
|
||||
[865640023012945.5, 865640023012945.5, 'double'],
|
||||
['865640023012945.5', 865640023012945.5, 'double'],
|
||||
[PHP_INT_MAX, PHP_INT_MAX, 'integer'],
|
||||
[~PHP_INT_MAX + 1, ~PHP_INT_MAX + 1, 'integer'], // ~PHP_INT_MAX === PHP_INT_MIN, PHP_INT_MIN being PHP7+
|
||||
[PHP_INT_MAX + 1, PHP_INT_MAX + 1, 'double'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForTestFormatNumericCellValueWithNumbers
|
||||
*
|
||||
* @param int|float|string $value
|
||||
* @param int|float $expectedFormattedValue
|
||||
* @param string $expectedType
|
||||
* @return void
|
||||
*/
|
||||
public function testFormatNumericCellValueWithNumbers($value, $expectedFormattedValue, $expectedType)
|
||||
{
|
||||
$styleHelperMock = $this->getMockBuilder('Box\Spout\Reader\XLSX\Helper\StyleHelper')->disableOriginalConstructor()->getMock();
|
||||
$styleHelperMock
|
||||
->expects($this->once())
|
||||
->method('shouldFormatNumericValueAsDate')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$formatter = new CellValueFormatter(null, $styleHelperMock);
|
||||
$formattedValue = \ReflectionHelper::callMethodOnObject($formatter, 'formatNumericCellValue', $value, 0);
|
||||
|
||||
$this->assertEquals($expectedFormattedValue, $formattedValue);
|
||||
$this->assertEquals($expectedType, gettype($formattedValue));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\XLSX\Helper;
|
||||
|
||||
/**
|
||||
* Class StyleHelperTest
|
||||
*
|
||||
* @package Box\Spout\Reader\XLSX\Helper
|
||||
*/
|
||||
class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array $styleAttributes
|
||||
* @param array|void $customNumberFormats
|
||||
* @return StyleHelper
|
||||
*/
|
||||
private function getStyleHelperMock($styleAttributes, $customNumberFormats = [])
|
||||
{
|
||||
/** @var StyleHelper $styleHelper */
|
||||
$styleHelper = $this->getMockBuilder('\Box\Spout\Reader\XLSX\Helper\StyleHelper')
|
||||
->setMethods(['getCustomNumberFormats', 'getStylesAttributes'])
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$styleHelper->method('getStylesAttributes')->willReturn($styleAttributes);
|
||||
$styleHelper->method('getCustomNumberFormats')->willReturn($customNumberFormats);
|
||||
|
||||
return $styleHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWithDefaultStyle()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(0);
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWhenStyleIdNotListed()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([['applyNumberFormat' => true]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWhenShouldNotApplyNumberFormat()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => false]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWithBuiltinDateFormats()
|
||||
{
|
||||
$builtinNumFmtIdsForDate = [14, 15, 16, 17, 18, 19, 20, 21, 22, 45, 46, 47];
|
||||
|
||||
foreach ($builtinNumFmtIdsForDate as $builtinNumFmtIdForDate) {
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => $builtinNumFmtIdForDate]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
|
||||
$this->assertTrue($shouldFormatAsDate);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWhenCustomNumberFormatNotFound()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => 165]], [166 => []]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function dataProviderForCustomDateFormats()
|
||||
{
|
||||
return [
|
||||
// number format, expectedResult
|
||||
['[$-409]dddd\,\ mmmm\ d\,\ yy', true],
|
||||
['[$-409]d\-mmm\-yy;@', true],
|
||||
['[$-409]d\-mmm\-yyyy;@', true],
|
||||
['mm/dd/yy;@', true],
|
||||
['[$-F800]dddd\,\ mmmm\ dd\,\ yyyy', true],
|
||||
['m/d;@', true],
|
||||
['m/d/yy;@', true],
|
||||
['[$-409]d\-mmm;@', true],
|
||||
['[$-409]dd\-mmm\-yy;@', true],
|
||||
['[$-409]mmm\-yy;@', true],
|
||||
['[$-409]mmmm\-yy;@', true],
|
||||
['[$-409]mmmm\ d\,\ yyyy;@', true],
|
||||
['[$-409]m/d/yy\ h:mm\ AM/PM;@', true],
|
||||
['m/d/yy\ h:mm;@', true],
|
||||
['[$-409]mmmmm;@', true],
|
||||
['[$-409]mmmmm\-yy;@', true],
|
||||
['m/d/yyyy;@', true],
|
||||
['[$-409]m/d/yy\--h:mm;@', true],
|
||||
['GENERAL', false],
|
||||
['\ma\yb\e', false],
|
||||
['[Red]foo;', false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderForCustomDateFormats
|
||||
*
|
||||
* @param string $numberFormat
|
||||
* @param bool $expectedResult
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWithCustomDateFormats($numberFormat, $expectedResult)
|
||||
{
|
||||
$numFmtId = 165;
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => $numFmtId]], [$numFmtId => $numberFormat]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
|
||||
$this->assertEquals($expectedResult, $shouldFormatAsDate);
|
||||
}
|
||||
}
|
||||
@@ -73,6 +73,28 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testReadShouldSupportSheetsDefinitionInRandomOrder()
|
||||
{
|
||||
$allRows = $this->getAllRowsForFile('two_sheets_with_sheets_definition_in_reverse_order.xlsx');
|
||||
|
||||
$expectedRows = [
|
||||
['s1 - A1', 's1 - B1', 's1 - C1', 's1 - D1', 's1 - E1'],
|
||||
['s1 - A2', 's1 - B2', 's1 - C2', 's1 - D2', 's1 - E2'],
|
||||
['s1 - A3', 's1 - B3', 's1 - C3', 's1 - D3', 's1 - E3'],
|
||||
['s1 - A4', 's1 - B4', 's1 - C4', 's1 - D4', 's1 - E4'],
|
||||
['s1 - A5', 's1 - B5', 's1 - C5', 's1 - D5', 's1 - E5'],
|
||||
['s2 - A1', 's2 - B1', 's2 - C1', 's2 - D1', 's2 - E1'],
|
||||
['s2 - A2', 's2 - B2', 's2 - C2', 's2 - D2', 's2 - E2'],
|
||||
['s2 - A3', 's2 - B3', 's2 - C3', 's2 - D3', 's2 - E3'],
|
||||
['s2 - A4', 's2 - B4', 's2 - C4', 's2 - D4', 's2 - E4'],
|
||||
['s2 - A5', 's2 - B5', 's2 - C5', 's2 - D5', 's2 - E5'],
|
||||
];
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
@@ -113,6 +135,52 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testReadShouldSupportNumericTimestampFormattedDifferentlyAsDate()
|
||||
{
|
||||
// make sure dates are always created with the same timezone
|
||||
date_default_timezone_set('UTC');
|
||||
|
||||
$allRows = $this->getAllRowsForFile('sheet_with_same_numeric_value_date_formatted_differently.xlsx');
|
||||
|
||||
$expectedDate = \DateTime::createFromFormat('Y-m-d H:i:s', '2015-01-01 00:00:00');
|
||||
$expectedRows = [
|
||||
array_fill(0, 10, $expectedDate),
|
||||
array_fill(0, 10, $expectedDate),
|
||||
array_fill(0, 10, $expectedDate),
|
||||
array_merge(array_fill(0, 7, $expectedDate), ['', '', '']),
|
||||
];
|
||||
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testReadShouldSupportDifferentDatesAsNumericTimestamp()
|
||||
{
|
||||
// make sure dates are always created with the same timezone
|
||||
date_default_timezone_set('UTC');
|
||||
|
||||
$allRows = $this->getAllRowsForFile('sheet_with_different_numeric_value_dates.xlsx');
|
||||
|
||||
$expectedRows = [
|
||||
[
|
||||
\DateTime::createFromFormat('Y-m-d H:i:s', '2015-09-01 00:00:00'),
|
||||
\DateTime::createFromFormat('Y-m-d H:i:s', '2015-09-02 00:00:00'),
|
||||
\DateTime::createFromFormat('Y-m-d H:i:s', '2015-09-01 22:23:00'),
|
||||
],
|
||||
[
|
||||
\DateTime::createFromFormat('Y-m-d H:i:s', '1900-02-28 23:59:59'),
|
||||
\DateTime::createFromFormat('Y-m-d H:i:s', '1900-03-01 00:00:00'),
|
||||
\DateTime::createFromFormat('Y-m-d H:i:s', '1900-02-28 11:00:00'), // 1900-02-29 should be converted to 1900-02-28
|
||||
]
|
||||
];
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Box\Spout\Writer\ODS;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
use Box\Spout\TestUsingResource;
|
||||
use Box\Spout\Writer\Common\Helper\ZipHelper;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
|
||||
/**
|
||||
@@ -368,6 +369,30 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertValueWasWrittenToSheet($fileName, 1, 'a dream');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGeneratedFileShouldHaveTheCorrectMimeType()
|
||||
{
|
||||
// Only PHP7+ gives the correct mime type since it requires adding
|
||||
// uncompressed files to the final archive (which support was added in PHP7)
|
||||
if (!ZipHelper::canChooseCompressionMethod()) {
|
||||
$this->markTestSkipped(
|
||||
'The PHP version used does not support setting the compression method of archived files,
|
||||
resulting in the mime type to be detected incorrectly.'
|
||||
);
|
||||
}
|
||||
|
||||
$fileName = 'test_mime_type.ods';
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
$dataRow = ['foo'];
|
||||
|
||||
$this->writeToODSFile([$dataRow], $fileName);
|
||||
|
||||
$finfo = new \finfo(FILEINFO_MIME_TYPE);
|
||||
$this->assertEquals('application/vnd.oasis.opendocument.spreadsheet', $finfo->file($resourcePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $allRows
|
||||
* @param string $fileName
|
||||
|
||||
@@ -385,6 +385,21 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertInlineDataWasWrittenToSheet($fileName, 1, 'control's _x0015_ "character"');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGeneratedFileShouldHaveTheCorrectMimeType()
|
||||
{
|
||||
$fileName = 'test_mime_type.xlsx';
|
||||
$resourcePath = $this->getGeneratedResourcePath($fileName);
|
||||
$dataRows = [['foo']];
|
||||
|
||||
$this->writeToXLSXFile($dataRows, $fileName);
|
||||
|
||||
$finfo = new \finfo(FILEINFO_MIME_TYPE);
|
||||
$this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $finfo->file($resourcePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $allRows
|
||||
* @param string $fileName
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user