1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77:
<?php
namespace Inteve\FeedGenerator\Feeds\PostFeed;
use Inteve\FeedGenerator\Feed;
use Inteve\FeedGenerator\IOutput;
use Inteve\FeedGenerator\InvalidItemException;
use Nette\Utils\Json;
class PostFeed extends Feed
{
public function getContentType()
{
return 'application/json';
}
public function generate(IOutput $output)
{
$output->open();
$output->output('[');
$skipSeparator = TRUE;
$timezone = new \DateTimezone('UTC');
foreach ($this->items as $item) {
if (!($item instanceof PostFeedItem)) {
throw new InvalidItemException('Feed item must be instance of PostFeedItem.');
}
if ($skipSeparator) {
$skipSeparator = FALSE;
} else {
$output->output(',');
}
$output->output("\n");
$date = clone $item->getDate();
$date->setTimezone($timezone);
$data = array(
'id' => $item->getId(),
'title' => $item->getTitle(),
'date' => $date->format('Y-m-d H:i:s'),
'text' => $item->getText(),
'url' => $item->getUrl(),
'image' => $item->getImage(),
);
if ($data['text'] === NULL) {
unset($data['text']);
}
if ($data['url'] === NULL) {
unset($data['url']);
}
if ($data['image'] === NULL) {
unset($data['image']);
}
$output->output(Json::encode($data));
}
$output->output("\n]\n");
$output->close();
}
}