generate RSS feed

rss

RSS stands for Really Simple Syndication, which translates as "really fast distribution." RSS gives the user the opportunity to subscribe to the publication of the site - in whole or in part. Thus, the user gets the opportunity to follow the appearance of new publications on the site and read their summary without visiting the source site.Besides this you can do different useful things,for example create rss feed parser and move your articles to other site or steal articles from other sites that support rss:).
But if serious, theft is a sign of bad intention, do not do it. 

In order to subscribe to RSS-Feed, you will need an RSS-Reader program. This program monitors the automatic update and distribution of publications. There are many RSS readers in Google extensions you can install them.
You can read Wikipedia or other articles on the Internet in more detail. And so, let's start programming).

RSS feed, file in XML format, an example from Wikipedia.

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
 <title>RSS Title</title>
 <description>This is an example of an RSS feed</description>
 <link>http://www.example.com/main.html</link>
 <lastBuildDate>Mon, 06 Sep 2010 00:01:00 +0000 </lastBuildDate>
 <pubDate>Sun, 06 Sep 2009 16:20:00 +0000</pubDate>
 <ttl>1800</ttl>

 <item>
  <title>Example entry</title>
  <description>Here is some text containing an interesting description.</description>
  <link>http://www.example.com/blog/post/1</link>
  <guid isPermaLink="false">7bd204c6-1655-4c27-aeee-53f933c5395f</guid>
  <pubDate>Sun, 06 Sep 2009 16:20:00 +0000</pubDate>
 </item>

</channel>
</rss>

In order to generate a file in XML format or something like that, it’s convenient to use a design pattern` builder .
There is already a solution in the packagist, you can use one of them for example suin rss writer. 

RssMap
class RssMap
{
    private $feed;
    private $channel;
    private $item;

    public function __construct(Feed $feed, Channel $channel, RssItem $item)
    {
        $this->feed = $feed;
        $this->channel = $channel;
        $this->item = $item;

    }

Set channel

    public function setChannel($channel)
    {
        $this->channel
            ->title($channel->title)
            ->description($channel->description)
            ->url($channel->url)
            ->lastBuildDate(strtotime($channel->lastBuildDate))
            ->pubDate(strtotime($channel->pubDate))
            ->ttl($channel->ttl)
            ->appendTo($this->feed);

        return $this;
    }

Set rss items

    public function setItems(iterable $dataItems)
    {
        foreach ($dataItems as $data) {
            $item = clone $this->item;

            $item
                ->title($data->title)
                ->description('<div>'.$data->description.'</div>')
                ->url($data->link)
                ->guid($data->guid, true)
                ->pubDate(strtotime($data->pubDate))
                ->author($data->author)
                ->appendTo($this->channel);
        }

        return $this;
    }

But if we started programming on the OOP approach, then we will create two objects MapChannel, MapItem

MapChannel

class MapChannel
{
    public $title;
    public $description;
    public $url;
    public $pubDate;
    public $lastBuildDate;
    public $ttl;

    public function __construct($title, $description, $url, $pubDate, $lastBuildDate, $ttl)
    {
        $this->title = $title;
        $this->description = $description;
        $this->url = $url;
        $this->pubDate = $pubDate;
        $this->lastBuildDate = $lastBuildDate;
        $this->ttl = $ttl;
    }
}

MapItem

class MapItem
{
    public $title;
    public $description;
    public $link;
    public $guid;
    public $pubDate;

   
    public function __construct($title, $description, $link, $guid, $pubDate)
    {
        $this->title       = $title;
        $this->description = $description;
        $this->link        = $link;
        $this->guid        = $guid;
        $this->pubDate     = $pubDate;
    }
}

And the last example from the Rss Controller.

class RssController
{
    private $service;
    private $repository;

    public function __construct(RssMap $rssMap, ArticleRepository $repository)
    {
        $this->service = $rssMap;
        $this->repository = $repository;
    }

    public function create(Page $page)
    {
        $articles = $this->repository->findAll();

        return $this->service
            ->setChannel(
                new MapChannel(
                    $page->title,
                    $page->description,
                    $page->url,        
                    $page->pubDate,
                    $page->lastBuildDate,
                    1800
                ))
            ->setItems($articles->map(function ($article) use ($page) {
                return new MapItem(
                    $article->title,
                    $article->description,
                    $article->link,
                    $article->guid,
                    $article->pubDate
                );
            }))->generate();
    }
}

As you can see in the constructor is written ArticleRepository.Simply, in this example uses the design pattern repository.
Read more in this article.

Comment