Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Allow DateTimeImmutable instances to be provided as entry, deletion a…
Browse files Browse the repository at this point in the history
…nd feed dates
  • Loading branch information
gsteel committed Nov 9, 2018
1 parent 8511b44 commit 82c0ec3
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 18 deletions.
25 changes: 16 additions & 9 deletions src/Writer/AbstractFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Feed\Writer;

use DateTime;
use DateTimeInterface;
use Zend\Feed\Uri;
use Zend\Validator;

Expand Down Expand Up @@ -128,17 +129,19 @@ public function setCopyright($copyright)
/**
* Set the feed creation date
*
* @param null|int|DateTime
* @param null|int|DateTimeInterface
* @throws Exception\InvalidArgumentException
* @return AbstractFeed
*/
public function setDateCreated($date = null)
{
if ($date === null) {
$date = new DateTime();
} elseif (is_int($date)) {
}
if (is_int($date)) {
$date = new DateTime('@' . $date);
} elseif (! $date instanceof DateTime) {
}
if (! $date instanceof DateTimeInterface) {
throw new Exception\InvalidArgumentException('Invalid DateTime object or UNIX Timestamp'
. ' passed as parameter');
}
Expand All @@ -150,17 +153,19 @@ public function setDateCreated($date = null)
/**
* Set the feed modification date
*
* @param null|int|DateTime
* @param null|int|DateTimeInterface
* @throws Exception\InvalidArgumentException
* @return AbstractFeed
*/
public function setDateModified($date = null)
{
if ($date === null) {
$date = new DateTime();
} elseif (is_int($date)) {
}
if (is_int($date)) {
$date = new DateTime('@' . $date);
} elseif (! $date instanceof DateTime) {
}
if (! $date instanceof DateTimeInterface) {
throw new Exception\InvalidArgumentException('Invalid DateTime object or UNIX Timestamp'
. ' passed as parameter');
}
Expand All @@ -172,17 +177,19 @@ public function setDateModified($date = null)
/**
* Set the feed last-build date. Ignored for Atom 1.0.
*
* @param null|int|DateTime
* @param null|int|DateTimeInterface
* @throws Exception\InvalidArgumentException
* @return AbstractFeed
*/
public function setLastBuildDate($date = null)
{
if ($date === null) {
$date = new DateTime();
} elseif (is_int($date)) {
}
if (is_int($date)) {
$date = new DateTime('@' . $date);
} elseif (! $date instanceof DateTime) {
}
if (! $date instanceof DateTimeInterface) {
throw new Exception\InvalidArgumentException('Invalid DateTime object or UNIX Timestamp'
. ' passed as parameter');
}
Expand Down
9 changes: 6 additions & 3 deletions src/Writer/Deleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Feed\Writer;

use DateTime;
use DateTimeInterface;
use Zend\Feed\Uri;

/**
Expand Down Expand Up @@ -132,17 +133,19 @@ public function getReference()
/**
* Set when
*
* @param null|string|DateTime $date
* @param null|int|DateTimeInterface $date
* @throws Exception\InvalidArgumentException
* @return Deleted
*/
public function setWhen($date = null)
{
if ($date === null) {
$date = new DateTime();
} elseif (is_int($date)) {
}
if (is_int($date)) {
$date = new DateTime('@' . $date);
} elseif (! $date instanceof DateTime) {
}
if (! $date instanceof DateTimeInterface) {
throw new Exception\InvalidArgumentException('Invalid DateTime object or UNIX Timestamp'
. ' passed as parameter');
}
Expand Down
17 changes: 11 additions & 6 deletions src/Writer/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Feed\Writer;

use DateTime;
use DateTimeInterface;
use Zend\Feed\Uri;

/**
Expand Down Expand Up @@ -178,17 +179,19 @@ public function setContent($content)
/**
* Set the feed creation date
*
* @param null|int|DateTime $date
* @param null|int|DateTimeInterface $date
* @throws Exception\InvalidArgumentException
* @return Entry
*/
public function setDateCreated($date = null)
{
if ($date === null) {
$date = new DateTime();
} elseif (is_int($date)) {
}
if (is_int($date)) {
$date = new DateTime('@' . $date);
} elseif (! $date instanceof DateTime) {
}
if (! $date instanceof DateTimeInterface) {
throw new Exception\InvalidArgumentException(
'Invalid DateTime object or UNIX Timestamp passed as parameter'
);
Expand All @@ -201,17 +204,19 @@ public function setDateCreated($date = null)
/**
* Set the feed modification date
*
* @param null|int|DateTime $date
* @param null|int|DateTimeInterface $date
* @throws Exception\InvalidArgumentException
* @return Entry
*/
public function setDateModified($date = null)
{
if ($date === null) {
$date = new DateTime();
} elseif (is_int($date)) {
}
if (is_int($date)) {
$date = new DateTime('@' . $date);
} elseif (! $date instanceof DateTime) {
}
if (! $date instanceof DateTimeInterface) {
throw new Exception\InvalidArgumentException(
'Invalid DateTime object or UNIX Timestamp passed as parameter'
);
Expand Down
9 changes: 9 additions & 0 deletions test/Writer/DeletedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ZendTest\Feed\Writer;

use DateTime;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Zend\Feed\Writer;
use Zend\Feed\Writer\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -89,6 +90,14 @@ public function testSetWhenUsesDateTimeObject()
$this->assertEquals($myDate, $entry->getWhen());
}

public function testSetWhenUsesDateTimeImmutableObject()
{
$myDate = new DateTimeImmutable('@' . 1234567890);
$entry = new Writer\Deleted;
$entry->setWhen($myDate);
$this->assertEquals($myDate, $entry->getWhen());
}

public function testSetWhenThrowsExceptionOnInvalidParameter()
{
$entry = new Writer\Deleted;
Expand Down
17 changes: 17 additions & 0 deletions test/Writer/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ZendTest\Feed\Writer;

use DateTime;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Zend\Feed\Writer;
use Zend\Feed\Writer\Exception\ExceptionInterface;
Expand Down Expand Up @@ -240,6 +241,14 @@ public function testSetDateCreatedUsesDateTimeObject()
$this->assertEquals($myDate, $entry->getDateCreated());
}

public function testSetDateCreatedUsesDateTimeImmutableObject()
{
$myDate = new DateTimeImmutable('@' . 1234567890);
$entry = new Writer\Entry;
$entry->setDateCreated($myDate);
$this->assertEquals($myDate, $entry->getDateCreated());
}

public function testSetDateModifiedDefaultsToCurrentTime()
{
$entry = new Writer\Entry;
Expand Down Expand Up @@ -286,6 +295,14 @@ public function testSetDateModifiedUsesDateTimeObject()
$this->assertEquals($myDate, $entry->getDateModified());
}

public function testSetDateModifiedUsesDateTimeImmutableObject()
{
$myDate = new DateTimeImmutable('@' . 1234567890);
$entry = new Writer\Entry;
$entry->setDateModified($myDate);
$this->assertEquals($myDate, $entry->getDateModified());
}

public function testSetDateCreatedThrowsExceptionOnInvalidParameter()
{
$entry = new Writer\Entry;
Expand Down
25 changes: 25 additions & 0 deletions test/Writer/FeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ZendTest\Feed\Writer;

use DateTime;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Zend\Feed\Writer;
use Zend\Feed\Writer\Deleted;
Expand Down Expand Up @@ -180,6 +181,14 @@ public function testSetDateCreatedUsesDateTimeObject()
$this->assertEquals($myDate, $writer->getDateCreated());
}

public function testSetDateCreatedUsesDateTimeImmutableObject()
{
$myDate = new DateTimeImmutable('@' . 1234567890);
$writer = new Writer\Feed;
$writer->setDateCreated($myDate);
$this->assertEquals($myDate, $writer->getDateCreated());
}

public function testSetDateModifiedDefaultsToCurrentTime()
{
$writer = new Writer\Feed;
Expand Down Expand Up @@ -226,6 +235,14 @@ public function testSetDateModifiedUsesDateTimeObject()
$this->assertEquals($myDate, $writer->getDateModified());
}

public function testSetDateModifiedUsesDateTimeImmutableObject()
{
$myDate = new DateTimeImmutable('@' . 1234567890);
$writer = new Writer\Feed;
$writer->setDateModified($myDate);
$this->assertEquals($myDate, $writer->getDateModified());
}

public function testSetDateCreatedThrowsExceptionOnInvalidParameter()
{
$writer = new Writer\Feed;
Expand Down Expand Up @@ -304,6 +321,14 @@ public function testSetLastBuildDateUsesDateTimeObject()
$this->assertEquals($myDate, $writer->getLastBuildDate());
}

public function testSetLastBuildDateUsesDateTimeImmutableObject()
{
$myDate = new DateTimeImmutable('@' . 1234567890);
$writer = new Writer\Feed;
$writer->setLastBuildDate($myDate);
$this->assertEquals($myDate, $writer->getLastBuildDate());
}

public function testSetLastBuildDateThrowsExceptionOnInvalidParameter()
{
$writer = new Writer\Feed;
Expand Down

0 comments on commit 82c0ec3

Please sign in to comment.