Split PHPToExcel logic for different input types

This commit is contained in:
MarkBaker 2016-08-14 22:36:30 +01:00
parent aa97bb3e45
commit 91573b5c93
12 changed files with 215 additions and 158 deletions

View File

@ -209,41 +209,57 @@ class Date
->format('U');
}
/**
* Convert a date from PHP to Excel
* Convert a date from PHP to an MS Excel serialized date/time value
*
* @param mixed $dateValue PHP serialized date/time or date object
* @param boolean $adjustToTimezone Flag indicating whether $dateValue should be treated as
* a UST timestamp, or adjusted to UST
* @param string $timezone The timezone for finding the adjustment from UST
* @return mixed Excel date/time value
* @return float|boolean Excel date/time value
* or boolean FALSE on failure
*/
public static function PHPToExcel($dateValue = 0, $adjustToTimezone = false, $timezone = null)
public static function PHPToExcel($dateValue = 0)
{
$saveTimeZone = date_default_timezone_get();
date_default_timezone_set('UTC');
$timezoneAdjustment = ($adjustToTimezone) ?
PHPExcel_Shared_TimeZone::getTimezoneAdjustment($timezone ? $timezone : $saveTimeZone, $dateValue) :
0;
$retValue = false;
if ((is_object($dateValue)) && ($dateValue instanceof \DateTimeInterface)) {
$dateValue->add(new \DateInterval('PT' . $timezoneAdjustment . 'S'));
$retValue = self::formattedPHPToExcel($dateValue->format('Y'), $dateValue->format('m'), $dateValue->format('d'), $dateValue->format('H'), $dateValue->format('i'), $dateValue->format('s'));
return self::DateTimeToExcel($dateValue);
} elseif (is_numeric($dateValue)) {
$dateValue += $timezoneAdjustment;
$retValue = self::formattedPHPToExcel(date('Y', $dateValue), date('m', $dateValue), date('d', $dateValue), date('H', $dateValue), date('i', $dateValue), date('s', $dateValue));
return self::TimestampToExcel($dateValue);
} elseif (is_string($dateValue)) {
$retValue = self::stringToExcel($dateValue);
}
date_default_timezone_set($saveTimeZone);
return $retValue;
return self::stringToExcel($dateValue);
}
return false;
}
/**
* Convert a DateTime object to an MS Excel serialized date/time value
*
* @param \DateTimeInterface $dateValue PHP DateTime object
* @return float MS Excel serialized date/time value
*/
public static function dateTimeToExcel(\DateTimeInterface $dateValue = null)
{
return self::formattedPHPToExcel(
$dateValue->format('Y'),
$dateValue->format('m'),
$dateValue->format('d'),
$dateValue->format('H'),
$dateValue->format('i'),
$dateValue->format('s')
);
}
/**
* Convert a Unix timestamp to an MS Excel serialized date/time value
*
* @param \DateTimeInterface $dateValue PHP DateTime object
* @return float MS Excel serialized date/time value
*/
public static function timestampToExcel($dateValue = 0)
{
if (!is_numeric($dateValue)) {
return false;
}
return self::DateTimeToExcel(new \DateTime('@' . $dateValue));
}
/**
* formattedPHPToExcel

View File

@ -48,9 +48,9 @@ class DateTest extends \PHPUnit_Framework_TestCase
}
/**
* @dataProvider providerDateTimePHPToExcel1900
* @dataProvider providerDateTimeTimestampToExcel1900
*/
public function testDateTimePHPToExcel1900()
public function testDateTimeTimestampToExcel1900()
{
$result = call_user_func(
array(Date::class,'setExcelCalendar'),
@ -59,13 +59,34 @@ class DateTest extends \PHPUnit_Framework_TestCase
$args = func_get_args();
$expectedResult = array_pop($args);
$result = call_user_func_array(array(Date::class,'PHPToExcel'), $args);
$result = call_user_func_array(array(Date::class,'timestampToExcel'), $args);
$this->assertEquals($expectedResult, $result, null, 1E-5);
}
public function providerDateTimePHPToExcel1900()
public function providerDateTimeTimestampToExcel1900()
{
return include 'rawTestData/Shared/Date/PHPToExcel1900.php';
return include 'rawTestData/Shared/Date/TimestampToExcel1900.php';
}
/**
* @dataProvider providerDateTimeDateTimeToExcel
*/
public function testDateTimeDateTimeToExcel()
{
$result = call_user_func(
array(Date::class,'setExcelCalendar'),
Date::CALENDAR_WINDOWS_1900
);
$args = func_get_args();
$expectedResult = array_pop($args);
$result = call_user_func_array(array(Date::class,'dateTimeToExcel'), $args);
$this->assertEquals($expectedResult, $result, null, 1E-5);
}
public function providerDateTimeDateTimeToExcel()
{
return include 'rawTestData/Shared/Date/DateTimeToExcel.php';
}
/**
@ -111,9 +132,9 @@ class DateTest extends \PHPUnit_Framework_TestCase
}
/**
* @dataProvider providerDateTimePHPToExcel1904
* @dataProvider providerDateTimeTimestampToExcel1904
*/
public function testDateTimePHPToExcel1904()
public function testDateTimeTimestampToExcel1904()
{
$result = call_user_func(
array(Date::class,'setExcelCalendar'),
@ -122,13 +143,13 @@ class DateTest extends \PHPUnit_Framework_TestCase
$args = func_get_args();
$expectedResult = array_pop($args);
$result = call_user_func_array(array(Date::class,'PHPToExcel'), $args);
$result = call_user_func_array(array(Date::class,'timestampToExcel'), $args);
$this->assertEquals($expectedResult, $result, null, 1E-5);
}
public function providerDateTimePHPToExcel1904()
public function providerDateTimeTimestampToExcel1904()
{
return include 'rawTestData/Shared/Date/PHPToExcel1904.php';
return include 'rawTestData/Shared/Date/TimestampToExcel1904.php';
}
/**

View File

@ -0,0 +1,20 @@
<?php
// DateTime object Result Comments
return [
[ new DateTime('1900-01-01'), 1.0 ], // Excel 1900 base calendar date
[ new DateTime('1900-02-28'), 59.0 ], // This and next test show gap for the mythical
[ new DateTime('1900-03-01'), 61.0 ], // MS Excel 1900 Leap Year
[ new DateTime('1901-12-14'), 714.0 ], // Unix Timestamp 32-bit Earliest Date
[ new DateTime('1903-12-31'), 1461.0 ],
[ new DateTime('1904-01-01'), 1462.0 ], // Excel 1904 Calendar Base Date
[ new DateTime('1904-01-02'), 1463.0 ],
[ new DateTime('1960-12-19'), 22269.0 ],
[ new DateTime('1970-01-01'), 25569.0 ], // Unix Timestamp Base Date
[ new DateTime('1982-12-07'), 30292.0 ],
[ new DateTime('2008-06-12'), 39611.0 ],
[ new DateTime('2038-01-19'), 50424.0 ], // Unix Timestamp 32-bit Latest Date
[ new DateTime('1903-05-18 13:37:46'), 1234.56789 ],
[ new DateTime('1933-10-18 16:17:37'), 12345.6789 ],
[ new DateTime('2099-12-31'), 73050.0 ],
];

View File

@ -1,16 +0,0 @@
<?php
// Excel DateTimeStamp Result Comments
return [
[ -2147472000, 714 ], // PHP 32-bit Earliest Date 14-Dec-1901
[ -2082931200, 1461 ], // 31-Dec-1903
[ -2082844800, 1462 ], // Excel 1904 Calendar Base Date 01-Jan-1904
[ -2082758400, 1463 ], // 02-Jan-1904
[ -285120000, 22269 ], // 19-Dec-1960
[ 0, 25569 ], // PHP Base Date 01-Jan-1970
[ 408067200, 30292 ], // 07-Dec-1982
[ 1213228800, 39611 ], // 12-Jun-2008
[ 2147472000, 50424 ], // PHP 32-bit Latest Date 19-Jan-2038
[ -2102494934, 1234.56789 ], // 18-May-1903 13:37:46
[ -1142494943, 12345.6789 ], // 18-Oct-1933 16:17:37
];

View File

@ -1,11 +0,0 @@
<?php
// Excel DateTimeStamp Result
return [
[ -1956528000, 1462 ],
[ -1956441600, 1463 ],
[ -158803200, 22269 ],
[ 126316800, 25569 ],
[ 534384000, 30292 ],
[ 1339545600, 39611 ],
];

View File

@ -0,0 +1,16 @@
<?php
// Unix TimeStamp Result Comments
return [
[ -2147472000, 714 ], // PHP 32-bit Earliest Date 14-Dec-1901
[ -2082931200, 1461 ], // 31-Dec-1903
[ -2082844800, 1462 ], // Excel 1904 Calendar Base Date 01-Jan-1904
[ -2082758400, 1463 ], // 02-Jan-1904
[ -285120000, 22269 ], // 19-Dec-1960
[ 0, 25569 ], // PHP Base Date 01-Jan-1970
[ 408067200, 30292 ], // 07-Dec-1982
[ 1213228800, 39611 ], // 12-Jun-2008
[ 2147472000, 50424 ], // PHP 32-bit Latest Date 19-Jan-2038
[ -2102494934, 1234.56789 ], // 18-May-1903 13:37:46
[ -1142494943, 12345.6789 ], // 18-Oct-1933 16:17:37
];

View File

@ -0,0 +1,11 @@
<?php
// Excel DateTimeStamp Result
return [
[ -1956528000, 1462 ],
[ -1956441600, 1463 ],
[ -158803200, 22269 ],
[ 126316800, 25569 ],
[ 534384000, 30292 ],
[ 1339545600, 39611 ],
];