PHP Class FeedWriter\Feed

Generate RSS 1.0, RSS2.0 and ATOM Feeds
Author: Anis uddin Ahmad ([email protected])
Show file Open project: mibe/feedwriter Class Usage Examples

Public Methods

Method Description
addCDATAEncoding ( array $tags ) : self Add one or more tags to the list of CDATA encoded tags
addGenerator ( ) : self Add a channel element indicating the program used to generate the feed.
addItem ( Item $feedItem ) : self Add a FeedItem to the main class
addNamespace ( string $prefix, string $uri ) : self Add a XML namespace to the internal list of namespaces. After that, custom channel elements can be used properly to generate a valid feed.
createNewItem ( ) : Item Create a new Item.
filterInvalidXMLChars ( string $string, string $replacement = '_' ) : string Replace invalid XML characters.
generateFeed ( ) : string Generate the feed.
getCDATAEncoding ( ) : array Get list of CDATA encoded properties
getMIMEType ( ) : string Get the appropriate MIME type string for the current feed.
printFeed ( $useGenericContentType = false ) : void Print the actual RSS/ATOM file
removeCDATAEncoding ( array $tags ) : void Remove tags from the list of CDATA encoded tags
setAtomLink ( string $href, string $rel = null, string $type = null, string $hreflang = null, string $title = null, integer $length = null ) : self Set custom 'link' channel elements.
setChannelAbout ( string $url ) : self Set the channel 'rdf:about' attribute, which is used in RSS1 feeds only.
setChannelElement ( string $elementName, string $content, array $attributes = null, $multiple = false ) : self Add a channel element to the feed.
setChannelElementsFromArray ( array $elementArray ) : self Set multiple channel elements from an array. Array elements should be 'channelName' => 'channelContent' format.
setDate ( $date ) : self Set the date when the feed was lastly updated.
setDescription ( string $description ) : self Set a phrase or sentence describing the feed.
setEncoding ( string $encoding ) : self Set the 'encoding' attribute in the XML prolog.
setImage ( string $url, string $title = null, string $link = null ) : self Set the 'image' channel element
setLink ( string $link ) : self Set the 'link' channel element
setPagination ( string $nextURL = null, string $previousURL = null, string $firstURL = null, string $lastURL = null ) : self Set the URLs for feed pagination.
setSelfLink ( string $url ) : self Set an 'atom:link' channel element with relation=self attribute.
setTitle ( string $title ) : self Set the 'title' channel element
uuid ( string $key = null, string $prefix = '' ) : string Generate an UUID.

Protected Methods

Method Description
__construct ( string $version = Feed::RSS2 ) Constructor

Private Methods

Method Description
endItem ( ) : string Closes feed item tag
getNamespacePrefixes ( ) : array Returns all used XML namespace prefixes in this instance.
makeChannels ( ) : string Make the channels.
makeFooter ( ) : string Closes the open tags at the end of file
makeHeader ( ) : string Returns the XML header and root element, depending on the feed type.
makeItems ( ) : string Prints formatted feed items
makeNode ( string $tagName, mixed $tagContent, array $attributes = null, boolean $omitEndTag = false ) : string Creates a single node in XML format
sanitizeCDATA ( string $text ) : string Sanitizes data which will be later on returned as CDATA in the feed.
startItem ( string $about = false ) : string Make the starting tag of channels

Method Details

__construct() protected method

If no version is given, a feed in RSS 2.0 format will be generated.
protected __construct ( string $version = Feed::RSS2 )
$version string the version constant (RSS1/RSS2/ATOM).

addCDATAEncoding() public method

Add one or more tags to the list of CDATA encoded tags
public addCDATAEncoding ( array $tags ) : self
$tags array An array of tag names that are merged into the list of tags which should be encoded as CDATA
return self

addGenerator() public method

Add a channel element indicating the program used to generate the feed.
public addGenerator ( ) : self
return self

addItem() public method

Add a FeedItem to the main class
public addItem ( Item $feedItem ) : self
$feedItem Item instance of Item class
return self

addNamespace() public method

Add a XML namespace to the internal list of namespaces. After that, custom channel elements can be used properly to generate a valid feed.
public addNamespace ( string $prefix, string $uri ) : self
$prefix string namespace prefix
$uri string namespace name (URI)
return self

createNewItem() public method

Create a new Item.
public createNewItem ( ) : Item
return Item instance of Item class

filterInvalidXMLChars() public static method

Replace invalid XML characters.
public static filterInvalidXMLChars ( string $string, string $replacement = '_' ) : string
$string string string which should be filtered
$replacement string replace invalid characters with this string
return string the filtered string

generateFeed() public method

Generate the feed.
public generateFeed ( ) : string
return string The complete feed XML.

getCDATAEncoding() public method

Get list of CDATA encoded properties
public getCDATAEncoding ( ) : array
return array Return an array of CDATA properties that are to be encoded as CDATA

getMIMEType() public method

Get the appropriate MIME type string for the current feed.
public getMIMEType ( ) : string
return string The MIME type string.

printFeed() public method

Sets a Content-Type header and echoes the contents of the feed. Should only be used in situations where direct output is desired; if you need to pass a string around, use generateFeed() instead.
public printFeed ( $useGenericContentType = false ) : void
return void

removeCDATAEncoding() public method

Remove tags from the list of CDATA encoded tags
public removeCDATAEncoding ( array $tags ) : void
$tags array An array of tag names that should be removed.
return void

setChannelAbout() public method

Set the channel 'rdf:about' attribute, which is used in RSS1 feeds only.
public setChannelAbout ( string $url ) : self
$url string value of 'rdf:about' attribute of the channel element
return self

setChannelElement() public method

Add a channel element to the feed.
public setChannelElement ( string $elementName, string $content, array $attributes = null, $multiple = false ) : self
$elementName string name of the channel tag
$content string content of the channel tag
$attributes array
return self

setChannelElementsFromArray() public method

Set multiple channel elements from an array. Array elements should be 'channelName' => 'channelContent' format.
public setChannelElementsFromArray ( array $elementArray ) : self
$elementArray array
return self

setDate() public method

This adds the 'updated' element to the feed. The value of the date parameter can be either an instance of the DateTime class, an integer containing a UNIX timestamp or a string which is parseable by PHP's 'strtotime' function. Not supported in RSS1 feeds.
public setDate ( $date ) : self
return self

setDescription() public method

Set a phrase or sentence describing the feed.
public setDescription ( string $description ) : self
$description string Description of the feed.
return self

setEncoding() public method

Set the 'encoding' attribute in the XML prolog.
public setEncoding ( string $encoding ) : self
$encoding string value of 'encoding' attribute
return self

setImage() public method

Set the 'image' channel element
public setImage ( string $url, string $title = null, string $link = null ) : self
$url string URL of the image
$title string Title of the image. RSS only.
$link string Link target URL of the image. RSS only.
return self

setPagination() public method

See RFC 5005, chapter 3. At least one page URL must be specified.
public setPagination ( string $nextURL = null, string $previousURL = null, string $firstURL = null, string $lastURL = null ) : self
$nextURL string The URL to the next page of this feed. Optional.
$previousURL string The URL to the previous page of this feed. Optional.
$firstURL string The URL to the first page of this feed. Optional.
$lastURL string The URL to the last page of this feed. Optional.
return self

setTitle() public method

Set the 'title' channel element
public setTitle ( string $title ) : self
$title string value of 'title' channel tag
return self

uuid() public static method

The UUID is based on an MD5 hash. If no key is given, a unique ID as the input for the MD5 hash is generated.
Author: Anis uddin Ahmad ([email protected])
public static uuid ( string $key = null, string $prefix = '' ) : string
$key string optional key on which the UUID is generated
$prefix string an optional prefix
return string the formatted UUID