-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from alexdebril/issue/96
Issue/96
- Loading branch information
Showing
33 changed files
with
1,158 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/* | ||
* This file is part of the feed-io package. | ||
* | ||
* (c) Alexandre Debril <alex.debril@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
require __DIR__.DIRECTORY_SEPARATOR.'bootstrap.php'; | ||
|
||
$client = new \FeedIo\Adapter\Guzzle\Client(new GuzzleHttp\Client()); | ||
|
||
$logger = new \Psr\Log\NullLogger(); | ||
|
||
$feedIo = new \FeedIo\FeedIo($client, $logger); | ||
|
||
$items = [ | ||
getItem('item 1', 'Lorem Ipsum'), | ||
getItem('item 2', '<p>Foo Bar</p>'), | ||
]; | ||
|
||
$feed = new \FeedIo\Feed(); | ||
$feed->setTitle('feed title'); | ||
|
||
foreach($items as $item) { | ||
$feed->add($item); | ||
} | ||
|
||
echo $feedIo->format($feed, 'json'); | ||
|
||
function getItem($title, $description) | ||
{ | ||
$item = new \FeedIo\Feed\Item(); | ||
$item->setTitle($title); | ||
$item->setDescription($description); | ||
|
||
return $item; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
/* | ||
* This file is part of the feed-io package. | ||
* | ||
* (c) Alexandre Debril <alex.debril@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FeedIo\Formatter; | ||
|
||
use FeedIo\Feed; | ||
use FeedIo\FeedInterface; | ||
use FeedIo\FormatterInterface; | ||
|
||
class JsonFormatter implements FormatterInterface | ||
{ | ||
|
||
/** | ||
* @param FeedInterface $feed | ||
* @return string | ||
*/ | ||
public function toString(FeedInterface $feed) | ||
{ | ||
return json_encode($this->toArray($feed)); | ||
} | ||
|
||
/** | ||
* @param FeedInterface $feed | ||
* @return array | ||
*/ | ||
public function toArray(FeedInterface $feed) | ||
{ | ||
return array_filter([ | ||
'version' => 'https://jsonfeed.org/version/1', | ||
'title' => $feed->getTitle(), | ||
'description' => $feed->getDescription(), | ||
'home_page_url' => $feed->getLink(), | ||
'feed_url' => $feed->getUrl(), | ||
'id' => $feed->getPublicId(), | ||
'items' => iterator_to_array($this->itemsToArray($feed)), | ||
]); | ||
} | ||
|
||
/** | ||
* @param FeedInterface $feed | ||
* @return \Generator | ||
*/ | ||
public function itemsToArray(FeedInterface $feed) | ||
{ | ||
foreach($feed as $item) { | ||
yield $this->itemToArray($item); | ||
} | ||
} | ||
|
||
/** | ||
* @param Feed\ItemInterface $item | ||
* @return array | ||
*/ | ||
public function itemToArray(Feed\ItemInterface $item) | ||
{ | ||
$array = $this->itemToBaseArray($item); | ||
$this->handleAuthor($item, $array); | ||
$this->handleMedia($item, $array); | ||
$this->handleDate($item, $array); | ||
|
||
return array_filter($array); | ||
} | ||
|
||
/** | ||
* @param Feed\ItemInterface $item | ||
* @return array | ||
*/ | ||
public function itemToBaseArray(Feed\ItemInterface $item) | ||
{ | ||
$offset = $this->isHtml($item->getDescription()) ? 'content_html':'content_txt'; | ||
return [ | ||
'id' => $item->getPublicId(), | ||
'title' => $item->getTitle(), | ||
$offset => $item->getDescription(), | ||
'url' => $item->getLink(), | ||
]; | ||
} | ||
|
||
/** | ||
* @param $string | ||
* @return bool | ||
*/ | ||
public function isHtml($string) | ||
{ | ||
return $string !== strip_tags($string); | ||
} | ||
|
||
/** | ||
* @param Feed\ItemInterface $item | ||
* @param array $array | ||
* @return array | ||
*/ | ||
public function handleAuthor(Feed\ItemInterface $item, array &$array) | ||
{ | ||
if ( ! is_null($item->getAuthor()) ) { | ||
$array['author'] = array_filter([ | ||
'name' => $item->getAuthor()->getName(), | ||
'url' => $item->getAuthor()->getUri(), | ||
]); | ||
} | ||
|
||
return $array; | ||
} | ||
|
||
/** | ||
* @param Feed\ItemInterface $item | ||
* @param array $array | ||
* @return array | ||
*/ | ||
public function handleMedia(Feed\ItemInterface $item, array &$array) | ||
{ | ||
if ( $item->hasMedia() ) { | ||
$array['image'] = $item->getMedias()->current()->getUrl(); | ||
} | ||
|
||
return $array; | ||
} | ||
|
||
/** | ||
* @param Feed\ItemInterface $item | ||
* @param array $array | ||
* @return array | ||
*/ | ||
public function handleDate(Feed\ItemInterface $item, array &$array) | ||
{ | ||
if( ! is_null($item->getLastModified()) ) { | ||
$array['date_published'] = $item->getLastModified()->format(\DateTime::RFC3339); | ||
} | ||
|
||
return $array; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/* | ||
* This file is part of the feed-io package. | ||
* | ||
* (c) Alexandre Debril <alex.debril@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FeedIo; | ||
|
||
|
||
interface FormatterInterface | ||
{ | ||
|
||
/** | ||
* @param FeedInterface $feed | ||
* @return string | ||
*/ | ||
public function toString(FeedInterface $feed); | ||
|
||
} |
Oops, something went wrong.