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

Patch for <itunes:image> in entries #77

Merged
merged 2 commits into from
May 24, 2018
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
28 changes: 28 additions & 0 deletions src/Writer/Extension/ITunes/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Zend\Feed\Writer\Extension\ITunes;

use Zend\Feed\Uri;
use Zend\Feed\Writer;
use Zend\Stdlib\StringUtils;
use Zend\Stdlib\StringWrapper\StringWrapperInterface;
Expand Down Expand Up @@ -217,6 +218,33 @@ public function setItunesSummary($value)
return $this;
}

/**
* Set entry image (icon)
*
* @param string $value
* @return Feed
* @throws Writer\Exception\InvalidArgumentException
*/
public function setItunesImage($value)
{
if (! is_string($value) || ! Uri::factory($value)->isValid()) {
throw new Writer\Exception\InvalidArgumentException(
'invalid parameter: "image" may only be a valid URI/IRI'
);
}

if (! in_array(substr($value, -3), ['jpg', 'png'])) {
throw new Writer\Exception\InvalidArgumentException(
'invalid parameter: "image" may only use file extension "jpg"'
. ' or "png" which must be the last three characters of the URI'
. ' (i.e. no query string or fragment)'
);
}

$this->data['image'] = $value;
return $this;
}

/**
* Overloading to itunes specific setters
*
Expand Down
2 changes: 1 addition & 1 deletion src/Writer/Extension/ITunes/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function setItunesCategories(array $values)
*/
public function setItunesImage($value)
{
if (! Uri::factory($value)->isValid()) {
if (! is_string($value) || ! Uri::factory($value)->isValid()) {
throw new Writer\Exception\InvalidArgumentException('invalid parameter: "image" may only'
. ' be a valid URI/IRI');
}
Expand Down
22 changes: 22 additions & 0 deletions src/Writer/Extension/ITunes/Renderer/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function render()
$this->_setAuthors($this->dom, $this->base);
$this->_setBlock($this->dom, $this->base);
$this->_setDuration($this->dom, $this->base);
$this->_setImage($this->dom, $this->base);
$this->_setExplicit($this->dom, $this->base);
$this->_setKeywords($this->dom, $this->base);
$this->_setSubtitle($this->dom, $this->base);
Expand Down Expand Up @@ -128,6 +129,27 @@ protected function _setDuration(DOMDocument $dom, DOMElement $root)
$this->called = true;
}

/**
* Set feed image (icon)
*
* @param DOMDocument $dom
* @param DOMElement $root
* @return void
*/
// @codingStandardsIgnoreStart
protected function _setImage(DOMDocument $dom, DOMElement $root)
{
// @codingStandardsIgnoreEnd
$image = $this->getDataContainer()->getItunesImage();
if (! $image) {
return;
}
$el = $dom->createElement('itunes:image');
$el->setAttribute('href', $image);
$root->appendChild($el);
$this->called = true;
}

/**
* Set explicit flag
*
Expand Down
50 changes: 50 additions & 0 deletions test/Writer/Extension/ITunes/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,54 @@ public function testSetSummaryThrowsExceptionWhenValueExceeds255Chars()
$entry = new Writer\Entry;
$entry->setItunesSummary(str_repeat('a', 4001));
}

public function invalidImageUrls()
{
return [
'null' => [null],
'true' => [true],
'false' => [false],
'zero' => [0],
'int' => [1],
'zero-float' => [0.0],
'float' => [1.1],
'string' => ['scheme:/host.path'],
'invalid-extension-gif' => ['https://example.com/image.gif', 'file extension'],
'invalid-extension-uc' => ['https://example.com/image.PNG', 'file extension'],
'array' => [['https://example.com/image.png']],
'object' => [(object) ['image' => 'https://example.com/image.png']],
];
}

/**
* @dataProvider invalidImageUrls
* @param mixed $url
* @param string $expectedMessage
*/
public function testSetItunesImageRaisesExceptionForInvalidUrl($url, $expectedMessage = 'valid URI')
{
$entry = new Writer\Entry();
$this->expectException(ExceptionInterface::class);
$this->expectExceptionMessage($expectedMessage);
$entry->setItunesImage($url);
}

public function validImageUrls()
{
return [
'jpg' => ['https://example.com/image.jpg'],
'png' => ['https://example.com/image.png'],
];
}

/**
* @dataProvider validImageUrls
* @param string $url
*/
public function testSetItunesImageSetsInternalDataWithValidUrl($url)
{
$entry = new Writer\Entry();
$entry->setItunesImage($url);
$this->assertEquals($url, $entry->getItunesImage());
}
}
50 changes: 50 additions & 0 deletions test/Writer/Extension/ITunes/FeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,54 @@ public function testSetSummaryThrowsExceptionWhenValueExceeds4000Chars()
$feed = new Writer\Feed;
$feed->setItunesSummary(str_repeat('a', 4001));
}

public function invalidImageUrls()
{
return [
'null' => [null],
'true' => [true],
'false' => [false],
'zero' => [0],
'int' => [1],
'zero-float' => [0.0],
'float' => [1.1],
'string' => ['scheme:/host.path'],
'invalid-extension-gif' => ['https://example.com/image.gif', 'file extension'],
'invalid-extension-uc' => ['https://example.com/image.PNG', 'file extension'],
'array' => [['https://example.com/image.png']],
'object' => [(object) ['image' => 'https://example.com/image.png']],
];
}

/**
* @dataProvider invalidImageUrls
* @param mixed $url
* @param string $expectedMessage
*/
public function testSetItunesImageRaisesExceptionForInvalidUrl($url, $expectedMessage = 'valid URI')
{
$feed = new Writer\Feed();
$this->expectException(ExceptionInterface::class);
$this->expectExceptionMessage($expectedMessage);
$feed->setItunesImage($url);
}

public function validImageUrls()
{
return [
'jpg' => ['https://example.com/image.jpg'],
'png' => ['https://example.com/image.png'],
];
}

/**
* @dataProvider validImageUrls
* @param string $url
*/
public function testSetItunesImageSetsInternalDataWithValidUrl($url)
{
$feed = new Writer\Feed();
$feed->setItunesImage($url);
$this->assertEquals($url, $feed->getItunesImage());
}
}