/** * Formats the specified response. * @param Response $response the response to be formatted. */ public function format(Response $response) { $charset = $this->encoding === null ? $response->charset : $this->encoding; if (stripos($this->contentType, 'charset') === false) { $this->contentType .= '; charset=' . $charset; } $response->getHeaders()->set('Content-Type', $this->contentType); $data = $response->data; if (isset($data['language'])) { $this->_feedWriter->setChannelElement('language', $data['language']); } if (!isset($data['pubDate'])) { $this->_feedWriter->setChannelElement('pubDate', date(DATE_RSS, time())); } if (isset($data['title'])) { $this->_feedWriter->setTitle($data['title']); } if (isset($data['link'])) { $this->_feedWriter->setLink($data['link']); } if (isset($data['description'])) { $this->_feedWriter->setDescription($data['description']); } if (isset($data['items']) && is_array($data['items'])) { if ($data['items']) { foreach ($data['items'] as $item) { $newItem = $this->_feedWriter->createNewItem(); $newItem->addElementArray($item); $this->_feedWriter->addItem($newItem); } } } $response->content = $this->_feedWriter->generateFeed(); }
/** * Make the channels. * * @access private * @return string The feed header as XML containing all the feed metadata. */ private function makeChannels() { $out = ''; //Start channel tag switch ($this->version) { case Feed::RSS2: $out .= '<channel>' . PHP_EOL; break; case Feed::RSS1: $out .= isset($this->data['ChannelAbout']) ? "<channel rdf:about=\"{$this->data['ChannelAbout']}\">" : "<channel rdf:about=\"{$this->channels['link']}\">"; break; } //Print Items of channel foreach ($this->channels as $key => $value) { // In ATOM feeds, strip all ATOM namespace prefixes from the tag name. They are not needed here, // because the ATOM namespace name is set as default namespace. if ($this->version == Feed::ATOM && strncmp($key, 'atom', 4) == 0) { $key = substr($key, 5); } // The channel element can occur multiple times, when the key 'content' is not in the array. if (!isset($value['content'])) { // If this is the case, iterate through the array with the multiple elements. foreach ($value as $singleElement) { $out .= $this->makeNode($key, $singleElement['content'], $singleElement['attributes']); } } else { $out .= $this->makeNode($key, $value['content'], $value['attributes']); } } if ($this->version == Feed::RSS1) { //RSS 1.0 have special tag <rdf:Seq> with channel $out .= "<items>" . PHP_EOL . "<rdf:Seq>" . PHP_EOL; foreach ($this->items as $item) { $thisItems = $item->getElements(); $out .= "<rdf:li resource=\"{$thisItems['link']['content']}\"/>" . PHP_EOL; } $out .= "</rdf:Seq>" . PHP_EOL . "</items>" . PHP_EOL . "</channel>" . PHP_EOL; } else { if ($this->version == Feed::ATOM) { // ATOM feeds have a unique feed ID. This is generated from the 'link' channel element. $out .= $this->makeNode('id', Feed::uuid($this->channels['link']['attributes']['href'], 'urn:uuid:')); } } return $out; }
/** * Make the channels. * * @access private * @return void */ private function makeChannels() { $out = ''; //Start channel tag switch ($this->version) { case Feed::RSS2: $out .= '<channel>' . PHP_EOL; break; case Feed::RSS1: $out .= isset($this->data['ChannelAbout']) ? "<channel rdf:about=\"{$this->data['ChannelAbout']}\">" : "<channel rdf:about=\"{$this->channels['link']}\">"; break; } //Print Items of channel foreach ($this->channels as $key => $value) { // ATOM feed needs some special handling if ($this->version == Feed::ATOM) { // Strip all ATOM namespace prefixes from tags. Not needed here, because the ATOM namespace name is // used as default namespace. if (strncmp($key, 'atom', 4) == 0) { $key = substr($key, 5); } if ($key == 'link') { if (is_array($value)) { // $value is an array containing multiple atom:link element attributes foreach ($value as $attributes) { // $attributes contains actually the node attributes, not the value. $out .= $this->makeNode($key, '', $attributes); } } else { // ATOM prints link element as href attribute $out .= $this->makeNode($key, '', array('href' => $value)); //Add the id for ATOM $out .= $this->makeNode('id', Feed::uuid($value, 'urn:uuid:')); } } else { $out .= $this->makeNode($key, $value); } } else { if ($key == 'atom:link') { // $value is an array containing multiple atom:link element attributes foreach ($value as $attributes) { // $attributes contains actually the node attributes, not the value. $out .= $this->makeNode($key, '', $attributes); } } else { $out .= $this->makeNode($key, $value); } } } //RSS 1.0 have special tag <rdf:Seq> with channel if ($this->version == Feed::RSS1) { $out .= "<items>" . PHP_EOL . "<rdf:Seq>" . PHP_EOL; foreach ($this->items as $item) { $thisItems = $item->getElements(); $out .= "<rdf:li resource=\"{$thisItems['link']['content']}\"/>" . PHP_EOL; } $out .= "</rdf:Seq>" . PHP_EOL . "</items>" . PHP_EOL . "</channel>" . PHP_EOL; } return $out; }
/** * Set the unique identifier of the feed item * * @access public * @param string The unique identifier of this item * @param boolean The value of the 'isPermaLink' attribute in RSS 2 feeds. * @return void */ public function setId($id, $permaLink = false) { if ($this->version == Feed::RSS2) { if (!is_bool($permaLink)) { die('The permaLink parameter must be boolean.'); } $permaLink = $permaLink ? 'true' : 'false'; $this->addElement('guid', $id, array('isPermaLink' => $permaLink)); } else { if ($this->version == Feed::ATOM) { $this->addElement('id', Feed::uuid($id, 'urn:uuid:'), NULL, TRUE); } else { die('A unique ID is not supported in RSS1 feeds.'); } } return $this; }
/** * Set the 'link' element of feed item * * @access public * @param string The content of 'link' element * @return void */ public function setLink($link) { if ($this->version == Feed::RSS2 || $this->version == Feed::RSS1) { $this->addElement('link', $link); } else { $this->addElement('link', '', array('href' => $link)); $this->addElement('id', Feed::uuid($link, 'urn:uuid:')); } return $this; }
/** * {@inheritdoc} */ public function __construct() { parent::__construct(Feed::ATOM); }