Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reader XML Properties - Eliminate strtotime #2134

Merged
merged 8 commits into from
May 31, 2021
Merged
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
2 changes: 1 addition & 1 deletion samples/templates/excel2003.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Description>Some comments about the PHPExcel Gnumeric Reader</Description>
<LastAuthor>Owen Leibman</LastAuthor>
<Created>2010-09-02T20:48:39Z</Created>
<LastSaved>2010-09-02T20:48:39Z</LastSaved>
<LastSaved>2010-09-03T21:48:39Z</LastSaved>
<Category>PHPExcel Xml Reader Test Category</Category>
<Manager>Maarten Balliauw</Manager>
<Company>PHPExcel</Company>
Expand Down
13 changes: 3 additions & 10 deletions src/PhpSpreadsheet/Reader/Xml/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ protected function processStandardProperty(

break;
case 'Created':
$docProps->setCreated($this->processTimestampValue($stringValue));
$docProps->setCreated($stringValue);

break;
case 'LastAuthor':
$docProps->setLastModifiedBy($stringValue);

break;
case 'LastSaved':
$docProps->setModified($this->processTimestampValue($stringValue));
$docProps->setModified($stringValue);

break;
case 'Company':
Expand Down Expand Up @@ -135,7 +135,7 @@ protected function processCustomProperty(
break;
case 'dateTime.tz':
$propertyType = DocumentProperties::PROPERTY_TYPE_DATE;
$propertyValue = $this->processTimestampValue(trim((string) $propertyValue));
$propertyValue = trim((string) $propertyValue);

break;
}
Expand All @@ -154,11 +154,4 @@ private static function getAttributes(?SimpleXMLElement $simple, string $node):
? new SimpleXMLElement('<xml></xml>')
: ($simple->attributes($node) ?? new SimpleXMLElement('<xml></xml>'));
}

protected function processTimestampValue(string $dateTimeValue): int
{
$dateTime = strtotime($dateTimeValue);

return $dateTime === false ? time() : $dateTime;
}
}
11 changes: 11 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xml/XmlLoadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xml;

use DateTimeZone;
use PhpOffice\PhpSpreadsheet\Reader\Xml;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PHPUnit\Framework\TestCase;

class XmlLoadTest extends TestCase
Expand All @@ -21,8 +23,17 @@ public function testLoad(): void
self::assertEquals('BCD', $sheet->getCell('A4')->getValue());
$props = $spreadsheet->getProperties();
self::assertEquals('Mark Baker', $props->getCreator());
$creationDate = $props->getCreated();
$result = Date::formattedDateTimeFromTimestamp("$creationDate", 'Y-m-d\\TH:i:s\\Z', new DateTimeZone('UTC'));
self::assertEquals('2010-09-02T20:48:39Z', $result);
$creationDate = $props->getModified();
$result = Date::formattedDateTimeFromTimestamp("$creationDate", 'Y-m-d\\TH:i:s\\Z', new DateTimeZone('UTC'));
self::assertEquals('2010-09-03T21:48:39Z', $result);
self::assertEquals('AbCd1234', $props->getCustomPropertyValue('my_API_Token'));
self::assertEquals('2', $props->getCustomPropertyValue('myאInt'));
$creationDate = $props->getCustomPropertyValue('my_API_Token_Expiry');
$result = Date::formattedDateTimeFromTimestamp("$creationDate", 'Y-m-d\\TH:i:s\\Z', new DateTimeZone('UTC'));
self::assertEquals('2019-01-31T07:00:00Z', $result);

$sheet = $spreadsheet->getSheet(0);
self::assertEquals('Sample Data', $sheet->getTitle());
Expand Down