From 82c0ec3478358a2a75b74be71186abdcb4bc01d1 Mon Sep 17 00:00:00 2001 From: George Steel Date: Thu, 8 Nov 2018 13:22:05 +0000 Subject: [PATCH 1/3] Allow DateTimeImmutable instances to be provided as entry, deletion and feed dates --- src/Writer/AbstractFeed.php | 25 ++++++++++++++++--------- src/Writer/Deleted.php | 9 ++++++--- src/Writer/Entry.php | 17 +++++++++++------ test/Writer/DeletedTest.php | 9 +++++++++ test/Writer/EntryTest.php | 17 +++++++++++++++++ test/Writer/FeedTest.php | 25 +++++++++++++++++++++++++ 6 files changed, 84 insertions(+), 18 deletions(-) diff --git a/src/Writer/AbstractFeed.php b/src/Writer/AbstractFeed.php index 5e45c48c..b7b135d3 100644 --- a/src/Writer/AbstractFeed.php +++ b/src/Writer/AbstractFeed.php @@ -10,6 +10,7 @@ namespace Zend\Feed\Writer; use DateTime; +use DateTimeInterface; use Zend\Feed\Uri; use Zend\Validator; @@ -128,7 +129,7 @@ public function setCopyright($copyright) /** * Set the feed creation date * - * @param null|int|DateTime + * @param null|int|DateTimeInterface * @throws Exception\InvalidArgumentException * @return AbstractFeed */ @@ -136,9 +137,11 @@ 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'); } @@ -150,7 +153,7 @@ public function setDateCreated($date = null) /** * Set the feed modification date * - * @param null|int|DateTime + * @param null|int|DateTimeInterface * @throws Exception\InvalidArgumentException * @return AbstractFeed */ @@ -158,9 +161,11 @@ 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'); } @@ -172,7 +177,7 @@ 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 */ @@ -180,9 +185,11 @@ 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'); } diff --git a/src/Writer/Deleted.php b/src/Writer/Deleted.php index 6d94997f..118355f4 100644 --- a/src/Writer/Deleted.php +++ b/src/Writer/Deleted.php @@ -10,6 +10,7 @@ namespace Zend\Feed\Writer; use DateTime; +use DateTimeInterface; use Zend\Feed\Uri; /** @@ -132,7 +133,7 @@ public function getReference() /** * Set when * - * @param null|string|DateTime $date + * @param null|int|DateTimeInterface $date * @throws Exception\InvalidArgumentException * @return Deleted */ @@ -140,9 +141,11 @@ 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'); } diff --git a/src/Writer/Entry.php b/src/Writer/Entry.php index 390521fe..b8ec25c7 100644 --- a/src/Writer/Entry.php +++ b/src/Writer/Entry.php @@ -10,6 +10,7 @@ namespace Zend\Feed\Writer; use DateTime; +use DateTimeInterface; use Zend\Feed\Uri; /** @@ -178,7 +179,7 @@ public function setContent($content) /** * Set the feed creation date * - * @param null|int|DateTime $date + * @param null|int|DateTimeInterface $date * @throws Exception\InvalidArgumentException * @return Entry */ @@ -186,9 +187,11 @@ 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' ); @@ -201,7 +204,7 @@ 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 */ @@ -209,9 +212,11 @@ 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' ); diff --git a/test/Writer/DeletedTest.php b/test/Writer/DeletedTest.php index 70cf3598..350278d3 100644 --- a/test/Writer/DeletedTest.php +++ b/test/Writer/DeletedTest.php @@ -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; @@ -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; diff --git a/test/Writer/EntryTest.php b/test/Writer/EntryTest.php index 59be94cf..c823dad5 100644 --- a/test/Writer/EntryTest.php +++ b/test/Writer/EntryTest.php @@ -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; @@ -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; @@ -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; diff --git a/test/Writer/FeedTest.php b/test/Writer/FeedTest.php index b2971b04..28a2d799 100644 --- a/test/Writer/FeedTest.php +++ b/test/Writer/FeedTest.php @@ -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; @@ -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; @@ -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; @@ -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; From 83ac98fc0a261dc8412bd66d774ea88a489a1f98 Mon Sep 17 00:00:00 2001 From: George Steel Date: Thu, 8 Nov 2018 13:35:52 +0000 Subject: [PATCH 2/3] Add changelog entry for pull #93 --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d217ef50..1ace6f46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,9 @@ All notable changes to this project will be documented in this file, in reverse ### Changed -- Nothing. +- [#93](https://github.com/zendframework/zend-feed/pull/93) `Writer\Feed`, `Writer\Entry` and `Writer\Deleted` all now + accept `DateTimeImmutable` instances as an arguments to methods that previously only accepted `DateTime` or Unix + Timestamps, such as `Writer\Feed::setDateModified()` ### Deprecated From 6c47babfda7eb233b18b3b6cc96c623a9ac8e7c6 Mon Sep 17 00:00:00 2001 From: George Steel Date: Thu, 8 Nov 2018 14:07:15 +0000 Subject: [PATCH 3/3] Update writer documentation stating that DateTimeInterface implementors are acceptable --- docs/book/writer.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/book/writer.md b/docs/book/writer.md index 810b6096..ac6ccb51 100644 --- a/docs/book/writer.md +++ b/docs/book/writer.md @@ -213,9 +213,9 @@ Method | Description `setFeedLink()` | Set a URI to an XML feed, whether it is to the feed being generated, or an alternate URI pointing to the same feed but in a different format. At a minimum, it is recommended to include a link to the feed being generated so it has an identifiable final URI allowing a client to track its location changes without necessitating constant redirects. The parameters are the feed URI and type (one of "atom", "rss", or "rdf"). `addAuthors()` | Sets the data for authors. The parameter is an array of array,s where each sub-array may contain the keys "name", "email", and "uri". The "uri" value is only applicable for Atom feeds, since RSS contains no facility to show it. For RSS 2.0, rendering will create two elements: an author element containing the email reference with the name in brackets, and a Dublin Core creator element only containing the name. `addAuthor()` | Sets the data for a single author following the same array format as described above for a single sub-array. -`setDateCreated()` | Sets the date on which this feed was created. Generally only applicable to Atom, where it represents the date the resource described by an Atom 1.0 document was created. The expected parameter may be a UNIX timestamp or a `DateTime` object. -`setDateModified()` | Sets the date on which this feed was last modified. The expected parameter may be a UNIX timestamp or a `DateTime` object. -`setLastBuildDate()` | Sets the date on which this feed was last build. The expected parameter may be a UNIX timestamp or a `DateTime` object. This will only be rendered for RSS 2.0 feeds, and is automatically rendered as the current date by default when not explicitly set. +`setDateCreated()` | Sets the date on which this feed was created. Generally only applicable to Atom, where it represents the date the resource described by an Atom 1.0 document was created. The expected parameter may be a UNIX timestamp or an object implementing `DateTimeInterface` such as `DateTime` or `DateTimeImmutable`. +`setDateModified()` | Sets the date on which this feed was last modified. The expected parameter may be a UNIX timestamp or an object implementing `DateTimeInterface` such as `DateTime` or `DateTimeImmutable`. +`setLastBuildDate()` | Sets the date on which this feed was last build. The expected parameter may be a UNIX timestamp or an object implementing `DateTimeInterface` such as `DateTime` or `DateTimeImmutable`. This will only be rendered for RSS 2.0 feeds, and is automatically rendered as the current date by default when not explicitly set. `setLanguage()` | Sets the language of the feed. This will be omitted unless set. `setGenerator()` | Allows the setting of a generator. The parameter should be an array containing the keys "name", "version", and "uri". If omitted a default generator will be added referencing `Zend\Feed\Writer`, the current zend-version version, and the Framework's URI. `setCopyright()` | Sets a copyright notice associated with the feed. @@ -257,8 +257,8 @@ Method | Description `setLink()` | Set a URI to the HTML website containing the same or similar information as this entry (i.e. if the feed is from a blog, it should provide the blog article's URI where the HTML version of the entry can be read). `addAuthors()` | Sets the data for authors. The parameter is an array of array,s where each sub-array may contain the keys "name", "email", and "uri". The "uri" value is only applicable for Atom feeds, since RSS contains no facility to show it. For RSS 2.0, rendering will create two elements: an author element containing the email reference with the name in brackets, and a Dublin Core creator element only containing the name. `addAuthor()` | Sets the data for a single author following the same format as described above for a single sub-array. -`setDateCreated()` | Sets the date on which this entry was created. Generally only applicable to Atom where it represents the date the resource described by an Atom 1.0 document was created. The expected parameter may be a UNIX timestamp or a `DateTime` object. If omitted, the date used will be the current date and time. -`setDateModified()` | Sets the date on which this entry was last modified. The expected parameter may be a UNIX timestamp or a `DateTime` object. If omitted, the date used will be the current date and time. +`setDateCreated()` | Sets the date on which this entry was created. Generally only applicable to Atom where it represents the date the resource described by an Atom 1.0 document was created. The expected parameter may be a UNIX timestamp or an object implementing `DateTimeInterface` such as `DateTime` or `DateTimeImmutable`. If omitted, the date used will be the current date and time. +`setDateModified()` | Sets the date on which this entry was last modified. The expected parameter may be a UNIX timestamp or an object implementing `DateTimeInterface` such as `DateTime` or `DateTimeImmutable`. If omitted, the date used will be the current date and time. `setCopyright()` | Sets a copyright notice associated with the entry. `addCategories()` | Accepts an array of categories for rendering, where each element is itself an array whose possible keys include "term", "label", and "scheme". The "term" is a typically a category name suitable for inclusion in a URI. The "label" may be a human readable category name supporting special characters (it is encoded during rendering) and is a required key. The "scheme" (called the domain in RSS) is optional but must be a valid URI. `addCategory()` | Sets the data for a single category following the same format as described above for a single sub-array.