Skip to content

Commit

Permalink
Merge pull request #97 from alexdebril/issue/96
Browse files Browse the repository at this point in the history
Issue/96
  • Loading branch information
alexdebril authored Jun 20, 2017
2 parents c580e18 + 1ec87c4 commit a775f91
Show file tree
Hide file tree
Showing 33 changed files with 1,158 additions and 320 deletions.
7 changes: 7 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ imports:
- php
tools:
external_code_coverage: true

filter:
excluded_paths:
- "examples/"
- "bin/"
- "doc/"
- "tests/"
40 changes: 40 additions & 0 deletions examples/jsonfeed.php
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;
}
2 changes: 1 addition & 1 deletion src/FeedIo/Feed/Item/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Author implements AuthorInterface
protected $uri;

/**
* @var int
* @var string
*/
protected $email;

Expand Down
53 changes: 37 additions & 16 deletions src/FeedIo/FeedIo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
use FeedIo\Reader\FixerAbstract;
use FeedIo\Rule\DateTimeBuilder;
use FeedIo\Adapter\ClientInterface;
use FeedIo\Standard\Atom;
use FeedIo\Standard\Rss;
use FeedIo\Standard\Rdf;
use FeedIo\Standard\Loader;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -145,11 +143,9 @@ public function addFilter(FilterInterface $filter)
*/
public function getCommonStandards()
{
return array(
'atom' => new Atom($this->dateTimeBuilder),
'rss' => new Rss($this->dateTimeBuilder),
'rdf' => new Rdf($this->dateTimeBuilder),
);
$loader = new Loader();

return $loader->getCommonStandards($this->getDateTimeBuilder());
}

/**
Expand All @@ -161,13 +157,28 @@ public function addStandard($name, StandardAbstract $standard)
{
$name = strtolower($name);
$this->standards[$name] = $standard;
$this->reader->addParser(
new Parser($standard, $this->logger)
);
$parser = $this->newParser($standard->getSyntaxFormat(), $standard);
$this->reader->addParser($parser);

return $this;
}

/**
* @param string $format
* @param StandardAbstract $standard
* @return object
*/
public function newParser($format, StandardAbstract $standard)
{
$reflection = new \ReflectionClass("FeedIo\\Parser\\{$format}Parser");

if ( ! $reflection->isSubclassOf('FeedIo\ParserAbstract') ) {
throw new \InvalidArgumentException();
}

return $reflection->newInstanceArgs([$standard, $this->logger]);
}

/**
* @return \FeedIo\Reader\FixerSet
*/
Expand Down Expand Up @@ -302,20 +313,20 @@ public function resetFilters()
/**
* @param FeedInterface $feed
* @param string $standard Standard's name
* @return \DomDocument
* @return string
*/
public function format(FeedInterface $feed, $standard)
{
$this->logAction($feed, "formatting a feed in $standard format");

$formatter = new Formatter($this->getStandard($standard), $this->logger);
$formatter = $this->getStandard($standard)->getFormatter();

return $formatter->toDom($feed);
return $formatter->toString($feed);
}

/**
* @param \FeedIo\FeedInterface $feed
* @return \DomDocument
* @return string
*/
public function toRss(FeedInterface $feed)
{
Expand All @@ -324,13 +335,23 @@ public function toRss(FeedInterface $feed)

/**
* @param \FeedIo\FeedInterface $feed
* @return \DomDocument
* @return string
*/
public function toAtom(FeedInterface $feed)
{
return $this->format($feed, 'atom');
}

/**
* @param \FeedIo\FeedInterface $feed
* @return string
*/
public function toJson(FeedInterface $feed)
{
return $this->format($feed, 'json');
}


/**
* @param string $name
* @return \FeedIo\StandardAbstract
Expand Down
140 changes: 140 additions & 0 deletions src/FeedIo/Formatter/JsonFormatter.php
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;
}

}
23 changes: 9 additions & 14 deletions src/FeedIo/Formatter.php → src/FeedIo/Formatter/XmlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,36 @@
* file that was distributed with this source code.
*/

namespace FeedIo;
namespace FeedIo\Formatter;

use FeedIo\Feed\NodeInterface;
use FeedIo\FeedInterface;
use FeedIo\Rule\OptionalField;
use Psr\Log\LoggerInterface;
use FeedIo\RuleSet;
use FeedIo\Standard\XmlAbstract;
use FeedIo\FormatterInterface;

/**
* Turns a FeedInterface instance into a XML document.
*
* Depends on :
* - FeedIo\StandardAbstract
* - Psr\Log\LoggerInterface
*
*/
class Formatter
class XmlFormatter implements FormatterInterface
{

/**
* @var StandardAbstract
* @var XmlAbstract
*/
protected $standard;

/**
* @var LoggerInterface
* @param XmlAbstract $standard
*/
protected $logger;

/**
* @param StandardAbstract $standard
* @param LoggerInterface $logger
*/
public function __construct(StandardAbstract $standard, LoggerInterface $logger)
public function __construct(XmlAbstract $standard)
{
$this->standard = $standard;
$this->logger = $logger;
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/FeedIo/FormatterInterface.php
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);

}
Loading

0 comments on commit a775f91

Please sign in to comment.