Skip to content
This repository was archived by the owner on Jun 6, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions php-export-data.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function generateRow($row) {
return $output;
}

private function generateCell($item) {
protected function generateCell($item) {
$output = '';
$style = '';

Expand All @@ -211,7 +211,14 @@ private function generateCell($item) {
// Note we want to be very strict in what we consider a date. There is the possibility
// of really screwing up the data if we try to reformat a string that was not actually
// intended to represent a date.
elseif(preg_match("/^(\d{1,2}|\d{4})[\/\-]\d{1,2}[\/\-](\d{1,2}|\d{4})([^\d].+)?$/",$item) &&
elseif (preg_match('_^
# dates: 2010-07-14 or 7/14/2010
(?:\d{1,2}|\d{4})[/-]\d{1,2}[/-](?:\d{1,2}|\d{4})
# optional time
(?:[T\s]+ # separated by space or T
[\d:,]+ # hours:minutes
)?
$_x', $item) &&
($timestamp = strtotime($item)) &&
($timestamp > 0) &&
($timestamp < strtotime('+500 years'))) {
Expand All @@ -237,4 +244,4 @@ function sendHttpHeaders() {
header("Content-Disposition: inline; filename=\"" . basename($this->filename) . "\"");
}

}
}
54 changes: 54 additions & 0 deletions test/ExportDataExcelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
require_once dirname(__FILE__) . '/../php-export-data.class.php';

class ExportDataExcelWrap extends ExportDataExcel {
public function wrapGenerateCell($item) {
return $this->generateCell($item);
}
}

/**
*
*/
class ExportDataExcelTest extends PHPUnit_Framework_TestCase {
protected $output = 'browser';
/**
* @var ExportDataExcel
*/
protected $object;

/**
* Sets up the fixture
*/
protected function setUp() {
$this->object = new ExportDataExcelWrap($this->output);
}

/**
* @covers ExportDataExcel::generateCell
* @dataProvider dateProvider
*/
public function testDates($exp, $date) {
$res = $this->object->wrapGenerateCell($date);
$this->assertNotEmpty($res);
$res = trim($res);
$this->assertEquals($exp, $res);
}

public function dateProvider() {
return array(
// iso dates: https://en.wikipedia.org/wiki/ISO_8601
array('<Cell ss:StyleID="sDT"><Data ss:Type="DateTime">2010-01-02T00:00:00</Data></Cell>',"2010-01-02"),
array('<Cell ss:StyleID="sDT"><Data ss:Type="DateTime">2010-01-02T09:52:00</Data></Cell>', "2010-01-02 9:52"),
array('<Cell ss:StyleID="sDT"><Data ss:Type="DateTime">2010-01-02T10:00:00</Data></Cell>', "2010-01-02T10:00"),
array('<Cell ss:StyleID="sDT"><Data ss:Type="DateTime">2010-01-02T10:00:00</Data></Cell>', "2010-01-02T10:00:00"),
# microseconds not supported by strtotime
# array('<Cell ss:StyleID="sDT"><Data ss:Type="DateTime">2010-01-02T06:12:00</Data></Cell>', "2010-01-02T06:12:00,000"),
# array('<Cell ss:StyleID="sDT"><Data ss:Type="DateTime">2010-01-02T00:00:00</Data></Cell>', "2010-01-02T10:00:00,000"),
// US dates
array('<Cell ss:StyleID="sDT"><Data ss:Type="DateTime">2010-02-08T00:00:00</Data></Cell>', "02/08/2010 00:00"),
array('<Cell ss:StyleID="sDT"><Data ss:Type="DateTime">2010-02-08T00:00:00</Data></Cell>', "02/08/2010"),
array('<Cell ss:StyleID="sDT"><Data ss:Type="DateTime">2010-02-09T00:00:00</Data></Cell>', "2/9/2010"),
);
}
}