web/Zend/Feed/Builder/Entry.php
changeset 0 4eba9c11703f
equal deleted inserted replaced
-1:000000000000 0:4eba9c11703f
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Zend Framework
       
     5  *
       
     6  * LICENSE
       
     7  *
       
     8  * This source file is subject to the new BSD license that is bundled
       
     9  * with this package in the file LICENSE.txt.
       
    10  * It is also available through the world-wide-web at this URL:
       
    11  * http://framework.zend.com/license/new-bsd
       
    12  * If you did not receive a copy of the license and are unable to
       
    13  * obtain it through the world-wide-web, please send an email
       
    14  * to license@zend.com so we can send you a copy immediately.
       
    15  *
       
    16  * @category   Zend
       
    17  * @package    Zend_Feed
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Entry.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 
       
    24 /**
       
    25  * An entry of a custom build feed
       
    26  *
       
    27  * Classes implementing the Zend_Feed_Builder_Interface interface
       
    28  * uses this class to describe an entry of a feed
       
    29  *
       
    30  * @category   Zend
       
    31  * @package    Zend_Feed
       
    32  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    34  */
       
    35 class Zend_Feed_Builder_Entry extends ArrayObject
       
    36 {
       
    37     /**
       
    38      * Create a new builder entry
       
    39      *
       
    40      * @param  string $title
       
    41      * @param  string $link
       
    42      * @param  string $description short version of the entry, no html
       
    43      * @return void
       
    44      */
       
    45     public function __construct($title, $link, $description)
       
    46     {
       
    47         $this->offsetSet('title', $title);
       
    48         $this->offsetSet('link', $link);
       
    49         $this->offsetSet('description', $description);
       
    50         $this->setLastUpdate(time());
       
    51     }
       
    52 
       
    53     /**
       
    54      * Read only properties accessor
       
    55      *
       
    56      * @param  string $name property to read
       
    57      * @return mixed
       
    58      */
       
    59     public function __get($name)
       
    60     {
       
    61         if (!$this->offsetExists($name)) {
       
    62             return NULL;
       
    63         }
       
    64 
       
    65         return $this->offsetGet($name);
       
    66     }
       
    67 
       
    68     /**
       
    69      * Write properties accessor
       
    70      *
       
    71      * @param  string $name name of the property to set
       
    72      * @param  mixed $value value to set
       
    73      * @return void
       
    74      */
       
    75     public function __set($name, $value)
       
    76     {
       
    77         $this->offsetSet($name, $value);
       
    78     }
       
    79 
       
    80     /**
       
    81      * Isset accessor
       
    82      *
       
    83      * @param  string $key
       
    84      * @return boolean
       
    85      */
       
    86     public function __isset($key)
       
    87     {
       
    88         return $this->offsetExists($key);
       
    89     }
       
    90 
       
    91     /**
       
    92      * Unset accessor
       
    93      *
       
    94      * @param  string $key
       
    95      * @return void
       
    96      */
       
    97     public function __unset($key)
       
    98     {
       
    99         if ($this->offsetExists($key)) {
       
   100             $this->offsetUnset($key);
       
   101         }
       
   102     }
       
   103 
       
   104     /**
       
   105      * Sets the author of the entry
       
   106      *
       
   107      * @param  string $author
       
   108      * @return Zend_Feed_Builder_Entry
       
   109      */
       
   110     public function setAuthor($author)
       
   111     {
       
   112         $this->offsetSet('author', $author);
       
   113         return $this;
       
   114     }
       
   115 
       
   116     /**
       
   117      * Sets the id/guid of the entry
       
   118      *
       
   119      * @param  string $id
       
   120      * @return Zend_Feed_Builder_Entry
       
   121      */
       
   122     public function setId($id)
       
   123     {
       
   124         $this->offsetSet('guid', $id);
       
   125         return $this;
       
   126     }
       
   127 
       
   128     /**
       
   129      * Sets the full html content of the entry
       
   130      *
       
   131      * @param  string $content
       
   132      * @return Zend_Feed_Builder_Entry
       
   133      */
       
   134     public function setContent($content)
       
   135     {
       
   136         $this->offsetSet('content', $content);
       
   137         return $this;
       
   138     }
       
   139 
       
   140     /**
       
   141      * Timestamp of the update date
       
   142      *
       
   143      * @param  int $lastUpdate
       
   144      * @return Zend_Feed_Builder_Entry
       
   145      */
       
   146     public function setLastUpdate($lastUpdate)
       
   147     {
       
   148         $this->offsetSet('lastUpdate', $lastUpdate);
       
   149         return $this;
       
   150     }
       
   151 
       
   152     /**
       
   153      * Sets the url of the commented page associated to the entry
       
   154      *
       
   155      * @param  string $comments
       
   156      * @return Zend_Feed_Builder_Entry
       
   157      */
       
   158     public function setCommentsUrl($comments)
       
   159     {
       
   160         $this->offsetSet('comments', $comments);
       
   161         return $this;
       
   162     }
       
   163 
       
   164     /**
       
   165      * Sets the url of the comments feed link
       
   166      *
       
   167      * @param  string $commentRss
       
   168      * @return Zend_Feed_Builder_Entry
       
   169      */
       
   170     public function setCommentsRssUrl($commentRss)
       
   171     {
       
   172         $this->offsetSet('commentRss', $commentRss);
       
   173         return $this;
       
   174     }
       
   175 
       
   176     /**
       
   177      * Defines a reference to the original source
       
   178      *
       
   179      * @param  string $title
       
   180      * @param  string $url
       
   181      * @return Zend_Feed_Builder_Entry
       
   182      */
       
   183     public function setSource($title, $url)
       
   184     {
       
   185         $this->offsetSet('source', array('title' => $title,
       
   186                                          'url' => $url));
       
   187         return $this;
       
   188     }
       
   189 
       
   190     /**
       
   191      * Sets the categories of the entry
       
   192      * Format of the array:
       
   193      * <code>
       
   194      * array(
       
   195      *   array(
       
   196      *         'term' => 'first category label',
       
   197      *         'scheme' => 'url that identifies a categorization scheme' // optional
       
   198      *        ),
       
   199      *   // second category and so one
       
   200      * )
       
   201      * </code>
       
   202      *
       
   203      * @param  array $categories
       
   204      * @return Zend_Feed_Builder_Entry
       
   205      */
       
   206     public function setCategories(array $categories)
       
   207     {
       
   208         foreach ($categories as $category) {
       
   209             $this->addCategory($category);
       
   210         }
       
   211         return $this;
       
   212     }
       
   213 
       
   214     /**
       
   215      * Add a category to the entry
       
   216      *
       
   217      * @param  array $category see Zend_Feed_Builder_Entry::setCategories() for format
       
   218      * @return Zend_Feed_Builder_Entry
       
   219      * @throws Zend_Feed_Builder_Exception
       
   220      */
       
   221     public function addCategory(array $category)
       
   222     {
       
   223         if (empty($category['term'])) {
       
   224             /**
       
   225              * @see Zend_Feed_Builder_Exception
       
   226              */
       
   227             require_once 'Zend/Feed/Builder/Exception.php';
       
   228             throw new Zend_Feed_Builder_Exception("you have to define the name of the category");
       
   229         }
       
   230 
       
   231         if (!$this->offsetExists('category')) {
       
   232             $categories = array($category);
       
   233         } else {
       
   234             $categories = $this->offsetGet('category');
       
   235             $categories[] = $category;
       
   236         }
       
   237         $this->offsetSet('category', $categories);
       
   238         return $this;
       
   239     }
       
   240 
       
   241     /**
       
   242      * Sets the enclosures of the entry
       
   243      * Format of the array:
       
   244      * <code>
       
   245      * array(
       
   246      *   array(
       
   247      *         'url' => 'url of the linked enclosure',
       
   248      *         'type' => 'mime type of the enclosure' // optional
       
   249      *         'length' => 'length of the linked content in octets' // optional
       
   250      *        ),
       
   251      *   // second enclosure and so one
       
   252      * )
       
   253      * </code>
       
   254      *
       
   255      * @param  array $enclosures
       
   256      * @return Zend_Feed_Builder_Entry
       
   257      * @throws Zend_Feed_Builder_Exception
       
   258      */
       
   259     public function setEnclosures(array $enclosures)
       
   260     {
       
   261         foreach ($enclosures as $enclosure) {
       
   262             if (empty($enclosure['url'])) {
       
   263                 /**
       
   264                  * @see Zend_Feed_Builder_Exception
       
   265                  */
       
   266                 require_once 'Zend/Feed/Builder/Exception.php';
       
   267                 throw new Zend_Feed_Builder_Exception("you have to supply an url for your enclosure");
       
   268             }
       
   269             $type = isset($enclosure['type']) ? $enclosure['type'] : '';
       
   270             $length = isset($enclosure['length']) ? $enclosure['length'] : '';
       
   271             $this->addEnclosure($enclosure['url'], $type, $length);
       
   272         }
       
   273         return $this;
       
   274     }
       
   275 
       
   276     /**
       
   277      * Add an enclosure to the entry
       
   278      *
       
   279      * @param  string $url
       
   280      * @param  string $type
       
   281      * @param  string $length
       
   282      * @return Zend_Feed_Builder_Entry
       
   283      */
       
   284     public function addEnclosure($url, $type = '', $length = '')
       
   285     {
       
   286         if (!$this->offsetExists('enclosure')) {
       
   287             $enclosure = array();
       
   288         } else {
       
   289             $enclosure = $this->offsetGet('enclosure');
       
   290         }
       
   291         $enclosure[] = array('url' => $url,
       
   292                              'type' => $type,
       
   293                              'length' => $length);
       
   294         $this->offsetSet('enclosure', $enclosure);
       
   295         return $this;
       
   296     }
       
   297 }