Skip to content

Commit

Permalink
Pass parameter to get feed items() (#38)
Browse files Browse the repository at this point in the history
* pass parameter to getFeedItems()

* refactor tests to include a filter being passed to a feed

* refactor following pr feedback
  • Loading branch information
leedriscoll authored and freekmurze committed May 13, 2017
1 parent f1cb45b commit 2630e06
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `laravel-feed` will be documented in this file

## 1.4.0 - 2017-05-12
- allow a filter to be passed with items in config

## 1.3.1 - 2017-05-12
- add a tag to publish views

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ return [
/*
* Here you can specify which class and method will return
* the items that should appear in the feed. For example:
* '\App\Model@getAllFeedItems'
* '\App\Model@getAllFeedItems' or ['\App\Model@getAllFeedItems', 'filter']
*/
'items' => '',

Expand Down
18 changes: 14 additions & 4 deletions src/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Feed
public function __construct(array $feedConfiguration)
{
$this->feedConfiguration = $feedConfiguration;
if (! str_contains($feedConfiguration['items'], '@')) {
throw InvalidConfiguration::delimiterNotPresent($feedConfiguration['items']);
if (! str_contains($this->getFeedMethod(), '@')) {
throw InvalidConfiguration::delimiterNotPresent($this->getFeedMethod());
}
}

Expand All @@ -26,15 +26,25 @@ public function getFeedResponse()

public function getFeedContent()
{
list($class, $method) = explode('@', $this->feedConfiguration['items']);
list($class, $method) = explode('@', $this->getFeedMethod());

$items = app($class)->{$method}();
$items = app($class)->{$method}($this->getFeedArguments());

$meta = ['id' => url($this->feedConfiguration['url']), 'link' => url($this->feedConfiguration['url']), 'title' => $this->feedConfiguration['title'], 'updated' => $this->getLastUpdatedDate($items)];

return view('laravel-feed::feed', compact('meta', 'items'))->render();
}

protected function getFeedMethod()
{
return is_array($this->feedConfiguration['items']) ? $this->feedConfiguration['items'][0] : $this->feedConfiguration['items'];
}

protected function getFeedArguments()
{
return is_array($this->feedConfiguration['items']) ? $this->feedConfiguration['items'][1] : null;
}

protected function getLastUpdatedDate(Collection $items)
{
if (! count($items)) {
Expand Down
17 changes: 17 additions & 0 deletions tests/DummyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,21 @@ public function getAll()
new DummyItem(),
]);
}

public function getAllWithArguments($filter = '')
{
if ($filter != '') {
return collect([
new DummyItem(),
]);
}

return collect([
new DummyItem(),
new DummyItem(),
new DummyItem(),
new DummyItem(),
new DummyItem(),
]);
}
}
2 changes: 1 addition & 1 deletion tests/FeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class FeedTest extends TestCase
{
protected $feedNames = ['feed1', 'feed2'];
protected $feedNames = ['feed1', 'feed2', 'feed3'];

/** @test */
public function all_feeds_are_available_on_their_registered_routes()
Expand Down
6 changes: 6 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ protected function getEnvironmentSetUp($app)
'title' => 'Feed 2',
'description' => 'This is feed 2 from the unit tests',
],
[
'items' => ['Spatie\Feed\Test\DummyRepository@getAllWithArguments', 'filter'],
'url' => '/feed3',
'title' => 'Feed 3',
'description' => 'This is feed 3 from the unit tests',
],
];

$app['config']->set('laravel-feed.feeds', $feed);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>http://localhost/feedBaseUrl/feed3</id>
<link href="http://localhost/feedBaseUrl/feed3"/>
<title><![CDATA[Feed 3]]></title>
<updated>2016-01-01T00:00:00+01:00</updated>
<entry>
<title><![CDATA[feedItemTitle]]></title>
<link rel="alternate" href="https://localhost/news/testItem1"/>
<id>http://localhost/1</id>
<author>
<name><![CDATA[feedItemAuthor]]></name>
</author>
<summary type="html"><![CDATA[feedItemSummary]]></summary>
<updated>2016-01-01T00:00:00+01:00</updated>
</entry>
</feed>
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return '<link rel="alternate" type="application/rss+xml" href="http://localhost/feedBaseUrl/feed1" title="Feed 1">
<link rel="alternate" type="application/rss+xml" href="http://localhost/feedBaseUrl/feed2" title="Feed 2">
<link rel="alternate" type="application/rss+xml" href="http://localhost/feedBaseUrl/feed3" title="Feed 3">
';

0 comments on commit 2630e06

Please sign in to comment.