Skip to content

Commit

Permalink
Accepting DateTimeInterface for Date and DateTime Literals (foreign c…
Browse files Browse the repository at this point in the history
…ode by @madbob). (#9)

* Accepting DateTimeInterface for Date and DateTime Literals.

Author: @madbob
Source: https://github.com/madbob/easyrdf/tree/datetimeinterface

Added/made a few refinements (coding styles, PHPStan related)

* fixed undefined class constant ATOM error

when using PHP 7.1 or below, constant ATOM is not available
for DateTimeInterface.

* More stable workaround for "undefined class constant error"

* fixed coding style issue
  • Loading branch information
k00ni authored May 6, 2021
1 parent 628d1dc commit c690f70
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
8 changes: 5 additions & 3 deletions lib/Literal/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
* @copyright Copyright (c) 2009-2014 Nicholas J Humfrey
* @license https://www.opensource.org/licenses/bsd-license.php
*/

use DateTimeInterface;
use EasyRdf\Literal;

/**
Expand All @@ -50,7 +52,7 @@ class Date extends Literal
{
/** Constructor for creating a new date literal
*
* If the value is a DateTime object, then it will be converted to the xsd:date format.
* If the value is a DateTimeInterface object, then it will be converted to the xsd:date format.
* If no value is given or is is null, then the current date is used.
*
* @see \DateTime
Expand All @@ -72,8 +74,8 @@ public function __construct($value = null, $lang = null, $datatype = null)
$value = new \DateTime('today');
}

// Convert DateTime object into string
if ($value instanceof \DateTime) {
// Convert DateTimeInterface object into string
if ($value instanceof DateTimeInterface) {
$value = $value->format('Y-m-d');
}

Expand Down
16 changes: 12 additions & 4 deletions lib/Literal/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace EasyRdf\Literal;

use DateTimeInterface;

/*
* EasyRdf
*
Expand Down Expand Up @@ -36,7 +38,6 @@
* @copyright Copyright (c) 2009-2014 Nicholas J Humfrey
* @license https://www.opensource.org/licenses/bsd-license.php
*/
use EasyRdf\Literal;

/**
* Class that represents an RDF Literal of datatype xsd:dateTime
Expand All @@ -50,7 +51,7 @@ class DateTime extends Date
{
/** Constructor for creating a new date and time literal
*
* If the value is a DateTime object, then it will be converted to the xsd:dateTime format.
* If the value is a DateTimeInterface object, then it will be converted to the xsd:dateTime format.
* If no value is given or is is null, then the current time is used.
*
* @see \DateTime
Expand All @@ -72,9 +73,16 @@ public function __construct($value = null, $lang = null, $datatype = null)
$value = new \DateTime('now');
}

// workaround for PHP < 7.2: if not used, script will throw "Undefined class constant 'ATOM'"
if (\defined('\DateTimeInterface::ATOM')) {
$format = DateTimeInterface::ATOM;
} else {
$format = \DateTime::ATOM;
}

// Convert DateTime objects into string
if ($value instanceof \DateTime) {
$atom = $value->format(\DateTime::ATOM);
if ($value instanceof DateTimeInterface) {
$atom = $value->format($format);
$value = preg_replace('/[\+\-]00(\:?)00$/', 'Z', $atom);
}

Expand Down
11 changes: 11 additions & 0 deletions test/EasyRdf/Literal/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ public function testConstructFromDateTime()
$this->assertSame('xsd:date', $literal->getDatatype());
}

public function testConstructFromDateTimeImmutable()
{
$dt = new \DateTimeImmutable('2011-07-18');
$literal = new Date($dt);
$this->assertStringEquals('2011-07-18', $literal);
$this->assertClass('DateTime', $literal->getValue());
$this->assertEquals($dt, $literal->getValue());
$this->assertStringEquals('', $literal->getLang());
$this->assertSame('xsd:date', $literal->getDatatype());
}

public function testConstructNoValue()
{
// Would be very unlucky if this ran at midnight and failed
Expand Down
22 changes: 17 additions & 5 deletions test/EasyRdf/Literal/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Test\EasyRdf\Literal;

use DateTimeImmutable;
use EasyRdf\Literal\DateTime;
use Test\TestCase;

Expand Down Expand Up @@ -45,6 +46,11 @@ class DateTimeTest extends TestCase
/** @var DateTime */
private $dt;

protected function setUp()
{
$this->dt = new DateTime('2010-09-08T07:06:05Z');
}

public function testConstruct()
{
$literal = new DateTime('2011-07-18T18:45:43Z');
Expand Down Expand Up @@ -84,6 +90,17 @@ public function testConstructFromDateTimeUTC()
$this->assertSame('xsd:dateTime', $literal->getDatatype());
}

public function testConstructFromDateTimeImmutableUTC()
{
$dt = new DateTimeImmutable('2010-09-08T07:06:05Z');
$literal = new DateTime($dt);
$this->assertStringEquals('2010-09-08T07:06:05Z', $literal);
$this->assertClass('DateTime', $literal->getValue());
$this->assertEquals($dt, $literal->getValue());
$this->assertStringEquals('', $literal->getLang());
$this->assertSame('xsd:dateTime', $literal->getDatatype());
}

public function testParseUTC()
{
$literal = DateTime::parse('Mon 18 Jul 2011 18:45:43 UTC');
Expand All @@ -102,11 +119,6 @@ public function testParseBST()
$this->assertSame('xsd:dateTime', $literal->getDatatype());
}

protected function setUp()
{
$this->dt = new DateTime('2010-09-08T07:06:05Z');
}

public function testFormat()
{
$this->assertSame(
Expand Down

0 comments on commit c690f70

Please sign in to comment.