* @package rss * @uses CComponent, CUrlValidator * @throws CException */ class EFeed extends CComponent{ /** * * supported Feed formats * * @var string RSS1 * @var string RSS2 * @var string ATOM */ const RSS1 = 'RSS1'; const RSS2 = 'RSS2'; const ATOM = 'Atom'; /** * * Holds all information and elements * to generate the feed * @var CMap $feedElements */ private $feedElements; /** * * Holds stylesheet associated to the feed * http://www.w3.org/TR/xml-stylesheet/#dt-xml-stylesheet */ private $stylesheets = array(); /** * * Type of Feed Format * @var string $type */ private $type; /** * Constructor * * @param constant the type constant (RSS1/RSS2/ATOM). */ function __construct($type = self::RSS2) { if( $type != self::RSS1 && $type != self::RSS2 && $type != self::ATOM ) throw new CException( Yii::t('EFeed', 'Feed version not supported') ); $this->type = $type; // Initiate Feed holder $this->feedElements = new CMap(); // Setting default value for essential channel elements $this->addChannelTag('title', $this->type. ' Feed'); $this->addChannelTag('link', 'http://www.ramirezcobos.com/' ); // Tag elements that we need to CDATA encode $this->feedElements->add('CDATAEncoded', array('description', 'content:encoded', 'summary') ); } /** * * Adds stylesheet support * @param array $htmlOptions */ public function addStylesheetTag( $htmlOptions ) { if( !is_array( $htmlOptions )) throw new CException( Yii::t( 'EFeed', __FUNCTION__.' parameter must be an array.' ) ); $this->stylesheets[] = ''; } /** * * Adds a channel element * @param string $tag name of the element * @param string $content of the element */ public function addChannelTag( $tag, $content ){ if( null === $this->feedElements->itemAt('channels') ) $this->feedElements->add('channels', new CMap()); $this->feedElements->itemAt('channels')->add( $tag, $content ); } /** * * Adds an array of channel elements * They should be on the format: *
* array('tagname'=>'tagcontent')
*
* @param unknown_type $tags
* @throws CException
*/
public function addChannelTagsArray( $tags ){
if( !is_array( $tags ) )
throw new CException( Yii::t( 'EFeed', __FUNCTION__.' parameter must be an array.' ) );
foreach( $tags as $tag=>$content ){
$this->addChannelTag($tag, $content);
}
}
/**
* RSS1, RSS2 or ATOM
* @return EFeedItemAbstract Item
*/
public function createNewItem( ){
// create EFeedItem based on selected version type
$class = "EFeedItem".$this->type;
return new $class;
}
/**
* Property setter the 'title' channel element
*
* @param string value of 'title' channel tag
*/
public function setTitle($title)
{
$this->addChannelTag('title', $title);
}
/**
*
* Property getter 'title'
*
* @return value of title channel tag | null
*/
public function getTitle( ){
if( null !== $this->feedElements->itemAt('channels')){
return $this->feedElements->itemAt('channels')->itemAt('title');
}
return null;
}
/**
* Property setter 'description' channel element
*
* @param string value of 'description' channel tag
*/
public function setDescription($description)
{
$this->addChannelTag('description', $description);
}
/**
*
* Property getter 'description'
*
* @return value of description channel tag | null
*/
public function getDescription( ){
if( null !== $this->feedElements->itemAt('channels')){
return $this->feedElements->itemAt('channels')->itemAt('description');
}
return null;
}
/**
* Property setter 'link' channel element
*
* @param string value of 'link' channel tag
* @throws CException
*/
public function setLink($link)
{
$validator = new CUrlValidator();
if(!$validator->validateValue($link))
throw new CException( Yii::t('EFeed', $link. ' does not seem to be a valid URL') );
$this->addChannelTag('link', $link);
}
/**
*
* Property getter 'link'
*
* @return value of link channel tag | null
*/
public function getLink( ){
if( null !== $this->feedElements->itemAt('channels')){
return $this->feedElements->itemAt('channels')->itemAt('link');
}
return null;
}
/**
* Set the 'image' channel element
*
* Cannot be used as property setter
*
* @param string title of image
* @param string link url of the image
* @param string path url of the image
*/
public function setImage($title, $link, $url)
{
$validator = new CUrlValidator();
if(!$validator->validateValue($link))
throw new CException( Yii::t('EFeed', $link. ' does not seem to be a valid URL') );
$this->addChannelTag('image', array('title'=>$title, 'link'=>$link, 'url'=>$url));
}
/**
*
* Property getter image
* @return value of the image channel tag | null
*/
public function getImage(){
if( null !== $this->feedElements->itemAt('channels'))
return $this->feedElements->itemAt('channels')->itemAt('image');
return null;
}
/**
*
* Property setter the 'about' RSS 1.0 channel element
*
* @param string value of 'about' channel tag
*/
public function setRSS1ChannelAbout($url)
{
$validator = new CUrlValidator();
if(!$validator->validateValue($url))
throw new CException( Yii::t('EFeed', $url. ' does not seem to be a valid URL') );
$this->addChannelTag('ChannelAbout', $url);
}
/**
*
* Property getter the 'about' RSS 1.0 channel
* @return value of 'about' channel tag | null
*/
public function getRSS1ChannelAbout(){
if( null !== $this->feedElements->itemAt('channels'))
return $this->feedElements->itemAt('channels')->itemAt('ChannelAbout');
return null;
}
/**
*
* Add a FeedItem to the main class
*
* @param object instance of EFeedItemAbstract class
*/
public function addItem(EFeedItemAbstract $item)
{
if( null === $this->feedElements->itemAt('items') )
$this->feedElements->add( 'items', new CTypedList('EFeedItemAbstract') );
$this->feedElements->itemAt('items')->add( $item );
}
/**
* Generates an UUID
*
* @author Anis uddin Ahmad