Ticket #174: rss_annotation.patch

File rss_annotation.patch, 9.6 kB (added by mikey, 23 months ago)

Initial rough draft of implementation

  • src/main/php/net/stubbles/xml/rss/stubRSSFeedGenerator.php

     
    185185    } 
    186186 
    187187    /** 
     188     * adds an entity as item to the rss feed 
     189     * 
     190     * @param   object           $entity 
     191     * @return  stubRssFeedItem  the item created from $entity 
     192     */ 
     193    public function addEntity($entity) 
     194    { 
     195        $rssFeedItem = stubRssFeedItem::fromEntity($entity); 
     196        array_push($this->items, $rssFeedItem); 
     197        return $rssFeedItem; 
     198    } 
     199 
     200    /** 
    188201     * checks whether an item is present at given position 
    189202     * 
    190203     * @param   int   $pos 
  • src/main/php/net/stubbles/xml/rss/stubRSSFeedItem.php

     
    77 * @subpackage  xml_rss 
    88 */ 
    99stubClassLoader::load('net::stubbles::lang::exceptions::stubIllegalArgumentException', 
    10                       'net::stubbles::xml::stubXMLStreamWriter' 
     10                      'net::stubbles::xml::stubXMLStreamWriter', 
     11                      'net::stubbles::xml::rss::stubRSSFeedItemAnnotation' 
    1112); 
    1213/** 
    1314 * Class for a rss 2.0 feed item. 
     
    120121    } 
    121122 
    122123    /** 
     124     * creates a new stubRssFeedItem from given entity 
     125     * 
     126     * @param   object  $entity 
     127     * @throws  stubIllegalArgumentException 
     128     * @throws  stubXMLException 
     129     */ 
     130    public static function fromEntity($entity) 
     131    { 
     132        if (is_object($entity) === false) { 
     133            throw new stubIllegalArgumentException('$entity must be an object.'); 
     134        } 
     135         
     136        $entityClass = (($entity instanceof stubObject) ? ($entity->getClass()) : (new stubReflectionObject($entity))); 
     137        if ($entityClass->hasAnnotation('RSSFeedItem') === false) { 
     138            throw new stubXMLException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not annotated with @RSSFeedItem.'); 
     139        } 
     140         
     141        $rssFeedItemAnnotation = $entityClass->getAnnotation('RSSFeedItem'); 
     142        if ($entityClass->hasMethod($rssFeedItemAnnotation->getTitleMethod()) === false) { 
     143            throw new stubXMLException('RSSFeedItem ' . $entityClass->getFullQualifiedClassName() . ' does not offer a method to return the title, but title is required.'); 
     144        } 
     145        $title = $entityClass->getMethod($rssFeedItemAnnotation->getTitleMethod())->invoke($entity); 
     146         
     147        if ($entityClass->hasMethod($rssFeedItemAnnotation->getLinkMethod()) === false) { 
     148            throw new stubXMLException('RSSFeedItem ' . $entityClass->getFullQualifiedClassName() . ' does not offer a method to return the link, but link is required.'); 
     149        } 
     150        $link = $entityClass->getMethod($rssFeedItemAnnotation->getLinkMethod())->invoke($entity); 
     151         
     152        if ($entityClass->hasMethod($rssFeedItemAnnotation->getDescriptionMethod()) === false) { 
     153            throw new stubXMLException('RSSFeedItem ' . $entityClass->getFullQualifiedClassName() . ' does not offer a method to return the description, but description is required.'); 
     154        } 
     155        $description = $entityClass->getMethod($rssFeedItemAnnotation->getDescriptionMethod())->invoke($entity); 
     156         
     157        $item        = new self($title, $link, $description); 
     158        if ($entityClass->hasMethod($rssFeedItemAnnotation->getAuthorMethod()) === true) { 
     159            $item->byAuthor($entityClass->getMethod($rssFeedItemAnnotation->getAuthorMethod())->invoke($entity)); 
     160        } 
     161         
     162        if ($entityClass->hasMethod($rssFeedItemAnnotation->getPubDateMethod()) === true) { 
     163            $item->publishedOn($entityClass->getMethod($rssFeedItemAnnotation->getPubDateMethod())->invoke($entity)); 
     164        } 
     165         
     166        return $item; 
     167    } 
     168 
     169    /** 
    123170     * set the email address of the author of the item who created the item 
    124171     * 
    125172     * @param   string           $author  author of rss feed item 
  • src/main/php/net/stubbles/xml/rss/stubRSSFeedItemAnnotation.php

     
     1<?php 
     2/** 
     3 * Annotation to mark an entity as an item of an RSS feed. 
     4 * 
     5 * @author      Frank Kleine <mikey@stubbles.net> 
     6 * @package     stubbles 
     7 * @subpackage  xml_rss 
     8 * @version     $Id$ 
     9 */ 
     10stubClassLoader::load('net::stubbles::reflection::annotations::stubAbstractAnnotation'); 
     11/** 
     12 * Annotation to mark an entity as an item of an RSS feed. 
     13 * 
     14 * @package     stubbles 
     15 * @subpackage  xml_rss 
     16 */ 
     17class stubRSSFeedItemAnnotation extends stubAbstractAnnotation 
     18{ 
     19    /** 
     20     * name of method to return the title of the item 
     21     * 
     22     * @var  string 
     23     */ 
     24    protected $titleMethod       = 'getTitle'; 
     25    /** 
     26     * name of method to return the URL of the item 
     27     * 
     28     * @var  string 
     29     */ 
     30    protected $linkMethod        = 'getLink'; 
     31    /** 
     32     * name of method to return the item synopsis 
     33     * 
     34     * @var  string 
     35     */ 
     36    protected $descriptionMethod = 'getDescription'; 
     37    /** 
     38     * name of method to return the email address of the author of the item 
     39     * 
     40     * @var  string 
     41     */ 
     42    protected $authorMethod      = 'getAuthor'; 
     43    /** 
     44     * name of method to return the categories where the item is included 
     45     * 
     46     * @var  array 
     47     */ 
     48    protected $categoriesMethod  = 'getCategories'; 
     49    /** 
     50     * name of method to return the URL of a page for comments relating to the item 
     51     * 
     52     * @var  string 
     53     */ 
     54    protected $commentsMethod    = 'getCommentsURL'; 
     55    /** 
     56     * name of method to return the describes a media object that is attached to the item 
     57     * 
     58     * @var  array 
     59     */ 
     60    protected $enclosuresMethod  = 'getEnclosures'; 
     61    /** 
     62     * name of method to return the unique identifier for the item 
     63     * 
     64     * @var  string 
     65     */ 
     66    protected $guidMethod        = 'getGuid'; 
     67    /** 
     68     * name of method to return the whether the id may be interpreted as a permanent link or not 
     69     * 
     70     * @var  bool 
     71     */ 
     72    protected $isPermaLinkMethod = 'isPermaLink'; 
     73    /** 
     74     * name of method to return the date when the item was published 
     75     * 
     76     * @var  string 
     77     */ 
     78    protected $pubDateMethod     = 'getPubDate'; 
     79    /** 
     80     * name of method to return the where that the item came from 
     81     * 
     82     * @var  array 
     83     */ 
     84    protected $sourcesMethod     = 'getSources'; 
     85    /** 
     86     * name of method to return the content of rss feed item 
     87     * 
     88     * @var  string 
     89     */ 
     90    protected $contentMethod     = 'getContent'; 
     91 
     92    /** 
     93     * returns the target of the annotation as bitmap 
     94     * 
     95     * @return  int 
     96     */ 
     97    public function getAnnotationTarget() 
     98    { 
     99        return stubAnnotation::TARGET_CLASS; 
     100    } 
     101 
     102    /** 
     103     * sets the name of method to return the title of the item 
     104     * 
     105     * @param  string  $titleMethod 
     106     */ 
     107    public function setTitleMethod($titleMethod) 
     108    { 
     109        $this->titleMethod = $titleMethod; 
     110    } 
     111 
     112    /** 
     113     * returns the name of method to return the title of the item 
     114     * 
     115     * @return  string 
     116     */ 
     117    public function getTitleMethod() 
     118    { 
     119        return $this->titleMethod; 
     120    } 
     121 
     122    /** 
     123     * sets the name of method to return the URL of the item 
     124     * 
     125     * @param  string  $linkMethod 
     126     */ 
     127    public function setLinkMethod($linkMethod) 
     128    { 
     129        $this->linkMethod = $linkMethod; 
     130    } 
     131 
     132    /** 
     133     * returns the name of method to return the URL of the item 
     134     * 
     135     * @return  string 
     136     */ 
     137    public function getLinkMethod() 
     138    { 
     139        return $this->linkMethod; 
     140    } 
     141 
     142    /** 
     143     * sets the name of method to return the item synopsis 
     144     * 
     145     * @param  string  $descriptionMethod 
     146     */ 
     147    public function setDescriptionMethod($descriptionMethod) 
     148    { 
     149        $this->descriptionMethod = $descriptionMethod; 
     150    } 
     151 
     152    /** 
     153     * returns the name of method to return the item synopsis 
     154     * 
     155     * @return  string 
     156     */ 
     157    public function getDescriptionMethod() 
     158    { 
     159        return $this->descriptionMethod; 
     160    } 
     161 
     162    /** 
     163     * sets the name of method to return the email address of the author of the item 
     164     * 
     165     * @param  string  $authorMethod 
     166     */ 
     167    public function setAuthorMethod($authorMethod) 
     168    { 
     169        $this->authorMethod = $authorMethod; 
     170    } 
     171 
     172    /** 
     173     * returns the name of method to return the email address of the author of the item 
     174     * 
     175     * @return  string 
     176     */ 
     177    public function getAuthorMethod() 
     178    { 
     179        return $this->authorMethod; 
     180    } 
     181 
     182    /** 
     183     * sets the name of method to return the date when the item was published 
     184     * 
     185     * @param  string  $pubDateMethod 
     186     */ 
     187    public function setPubDateMethod($pubDateMethod) 
     188    { 
     189        $this->pubDateMethod = $pubDateMethod; 
     190    } 
     191 
     192    /** 
     193     * returns the name of method to return the date when the item was published 
     194     * 
     195     * @return  string 
     196     */ 
     197    public function getPubDateMethod() 
     198    { 
     199        return $this->pubDateMethod; 
     200    } 
     201} 
     202?> 
     203 No newline at end of file