Author: Derick Rethans
Date: 2006-06-19 16:13:21 +0200 (Mon, 19 Jun 2006)
New Revision: 3143

Log:
- Added support for modules for items (read and write).
- Added ezcFeed->item() that returns an item from a feed.

Modified:
   trunk/Feed/src/feed.php
   trunk/Feed/src/feed_autoload.php
   trunk/Feed/src/feed_item.php
   trunk/Feed/src/interfaces/module.php
   trunk/Feed/src/interfaces/processor.php
   trunk/Feed/src/modules/dublin_core.php
   trunk/Feed/src/processors/rss2.php
   trunk/Feed/src/structs/item_module_data.php
   trunk/Feed/src/structs/module_data.php
   trunk/Feed/tests/feed_test.php
   trunk/Feed/tests/rss2_test.php

Modified: trunk/Feed/src/feed.php
===================================================================
--- trunk/Feed/src/feed.php     2006-06-19 12:48:26 UTC (rev 3142)
+++ trunk/Feed/src/feed.php     2006-06-19 14:13:21 UTC (rev 3143)
@@ -90,6 +90,11 @@
         return array_keys( self::$supportedFeedTypes );
     }
 
+    static public function getSupportedModules()
+    {
+        return self::$supportedModules;
+    }
+
     static public function getModule( $moduleName, $feedType )
     {
         return new self::$supportedModules[$moduleName]( $feedType );
@@ -160,6 +165,16 @@
         return $this->feedProcessor->generate();
     }
 
+    public function item( $nr )
+    {
+        $items = $this->feedProcessor->getItems();
+        if ( $nr < 0 || $nr > count( $items ) -1 )
+        {
+            throw new ezcFeedItemNrOutOfRangeException( $nr, count( $items ) );
+        }
+        return $items[$nr];
+    }
+
     public function rewind()
     {
         $this->iteratorItems = $this->feedProcessor->getItems();

Modified: trunk/Feed/src/feed_autoload.php
===================================================================
--- trunk/Feed/src/feed_autoload.php    2006-06-19 12:48:26 UTC (rev 3142)
+++ trunk/Feed/src/feed_autoload.php    2006-06-19 14:13:21 UTC (rev 3143)
@@ -27,6 +27,7 @@
 
     'ezcFeedException'                   => 'Feed/exceptions/exception.php',
     'ezcFeedCanNotParseException'        => 
'Feed/exceptions/can_not_parse.php',
+    'ezcFeedItemNrOutOfRangeException'   => 
'Feed/exceptions/item_nr_out_of_range.php',
     'ezcFeedParseErrorException'         => 'Feed/exceptions/parse_error.php',
     'ezcFeedRequiredItemDataMissingException' => 
'Feed/exceptions/item_data_missing.php',
     'ezcFeedRequiredMetaDataMissingException' => 
'Feed/exceptions/meta_data_missing.php',

Modified: trunk/Feed/src/feed_item.php
===================================================================
--- trunk/Feed/src/feed_item.php        2006-06-19 12:48:26 UTC (rev 3142)
+++ trunk/Feed/src/feed_item.php        2006-06-19 14:13:21 UTC (rev 3143)
@@ -70,7 +70,6 @@
             case 'published': // pubDate in RSS2
             case 'source': // original rss source
                 return $this->feedProcessor->getFeedItemElement( $this, 
$property );
-                break;
 
             default:
                 return false;

Modified: trunk/Feed/src/interfaces/module.php
===================================================================
--- trunk/Feed/src/interfaces/module.php        2006-06-19 12:48:26 UTC (rev 
3142)
+++ trunk/Feed/src/interfaces/module.php        2006-06-19 14:13:21 UTC (rev 
3143)
@@ -12,15 +12,12 @@
  * @version //autogentag//
  * @access private
  */
-abstract class ezcFeedModule
+interface ezcFeedModule
 {
-    abstract public function getNamespace();
-    abstract public function getNamespacePrefix();
-    abstract public function feedMetaHook( $element, $value );
-
-    public function isElementAllowed( $element )
-    {
-        return in_array( $element, $this->supportedElements );
-    }
+    public static function getModuleName();
+    public static function getNamespace();
+    public static function getNamespacePrefix();
+    public function feedMetaHook( $element, $value );
+    public function isElementAllowed( $element );
 }
 ?>

Modified: trunk/Feed/src/interfaces/processor.php
===================================================================
--- trunk/Feed/src/interfaces/processor.php     2006-06-19 12:48:26 UTC (rev 
3142)
+++ trunk/Feed/src/interfaces/processor.php     2006-06-19 14:13:21 UTC (rev 
3143)
@@ -46,7 +46,7 @@
         return new ezcFeedItemModuleData( $moduleName, $moduleObj, $item );
     }
 
-    public function getModuleMetaData( $module )
+    public function getAllModuleMetaData( $module )
     {
         if ( isset( $this->moduleMetaData[$module] ) )
         {
@@ -55,6 +55,24 @@
         return array();
     }
 
+    public function getModuleMetaData( $moduleName, $moduleObj, $element )
+    {
+        if ( isset( $this->moduleMetaData[$moduleName][$element] ) )
+        {
+            return $this->moduleMetaData[$moduleName][$element];
+        }
+        return NULL;
+    }
+
+    public function getModuleItemData( $moduleName, $moduleObj, $item, 
$element )
+    {
+        if ( isset( $item->moduleMetaData[$moduleName][$element] ) )
+        {
+            return $item->moduleMetaData[$moduleName][$element];
+        }
+        return NULL;
+    }
+
     public function setModuleMetaData( $moduleName, $moduleObj, $element, 
$value )
     {
         $value = $moduleObj->prepareMetaData( $element, $value );

Modified: trunk/Feed/src/modules/dublin_core.php
===================================================================
--- trunk/Feed/src/modules/dublin_core.php      2006-06-19 12:48:26 UTC (rev 
3142)
+++ trunk/Feed/src/modules/dublin_core.php      2006-06-19 14:13:21 UTC (rev 
3143)
@@ -12,7 +12,7 @@
  * @version //autogentag//
  * @access private
  */
-class ezcFeedModuleDublinCore extends ezcFeedModule
+class ezcFeedModuleDublinCore implements ezcFeedModule
 {
     private $feedType;
     protected $supportedElements = array(
@@ -26,16 +26,26 @@
         $this->feedType = $feedType;
     }
 
-    public function getNamespace()
+    public static function getModuleName()
     {
+        return 'DublinCore';
+    }
+
+    public static function getNamespace()
+    {
         return 'http://purl.org/dc/elements/1.1/';
     }
 
-    public function getNamespacePrefix()
+    public static function getNamespacePrefix()
     {
         return 'dc';
     }
 
+    public function isElementAllowed( $element )
+    {
+        return in_array( $element, $this->supportedElements );
+    }
+
     public function prepareDate( $date )
     {
         if ( is_int( $date ) || is_numeric( $date ) )

Modified: trunk/Feed/src/processors/rss2.php
===================================================================
--- trunk/Feed/src/processors/rss2.php  2006-06-19 12:48:26 UTC (rev 3142)
+++ trunk/Feed/src/processors/rss2.php  2006-06-19 14:13:21 UTC (rev 3143)
@@ -55,6 +55,8 @@
         'published' => 'pubDate',
     );
 
+    private $usedPrefixes = array();
+
     public function __construct()
     {
         // set default values
@@ -211,7 +213,7 @@
             $namespace = $moduleDescription->moduleObj->getNamespace();
             $this->generateNamespace( $prefix, $namespace );
 
-            foreach ( $this->getModuleMetaData( $moduleName ) as $element => 
$value )
+            foreach ( $this->getAllModuleMetaData( $moduleName ) as $element 
=> $value )
             {
                 $moduleDescription->moduleObj->generateMetaData( $this, 
$element, $value );
             }
@@ -269,6 +271,15 @@
                     case 'category':
                         $this->handleArrayValue( $feedItem, $tagName, 
$itemChild->textContent );
                         break;
+                    default:
+                        // check if it's part of a known module/namespace
+                        $parts = split( ':', $tagName );
+                        if ( count( $parts ) == 2 && in_array( $parts[0], 
array_keys( $this->usedPrefixes ) ) )
+                        {
+                            $moduleName = $this->usedPrefixes[$parts[0]];
+                            $element = $parts[1];
+                            $feedItem->$moduleName->$element = 
$itemChild->textContent;
+                        }
                 }
             }
         }
@@ -279,6 +290,26 @@
         $feed = ezcFeed::create( 'rss2' );
         $rssChildren = $xml->documentElement->childNodes;
         $channel = null;
+
+        // figure out modules
+        $supportedModules = ezcFeed::getSupportedModules();
+        $this->usedPrefixes = array();
+        $xp = new DOMXpath( $xml );
+        $set = $xp->query( './namespace::*', $xml->documentElement );
+        $this->usedNamespaces = array();
+        foreach ( $set as $node )
+        {
+            foreach ( $supportedModules as $moduleName => $moduleClass )
+            {
+                $moduleNamespace = call_user_func( array( $moduleClass, 
'getNamespace' ) );
+                if ( $moduleNamespace == $node->nodeValue )
+                {
+                    $feed->addModule( $moduleName );
+                    $this->usedPrefixes[call_user_func( array( $moduleClass, 
'getNamespacePrefix' ) )] = $moduleName;
+                }
+            }
+        }
+
         foreach ( $rssChildren as $rssChild )
         {
             if ( $rssChild->nodeType == XML_ELEMENT_NODE && $rssChild->tagName 
=== 'channel' )
@@ -321,6 +352,15 @@
                     case 'item':
                         $this->parseItem( $feed, $channelChild );
                         break;
+                    default:
+                        // check if it's part of a known module/namespace
+                        $parts = split( ':', $tagName );
+                        if ( count( $parts ) == 2 && in_array( $parts[0], 
array_keys( $this->usedPrefixes ) ) )
+                        {
+                            $moduleName = $this->usedPrefixes[$parts[0]];
+                            $element = $parts[1];
+                            $feed->$moduleName->$element = 
$channelChild->textContent;
+                        }
                 }
             }
         }

Modified: trunk/Feed/src/structs/item_module_data.php
===================================================================
--- trunk/Feed/src/structs/item_module_data.php 2006-06-19 12:48:26 UTC (rev 
3142)
+++ trunk/Feed/src/structs/item_module_data.php 2006-06-19 14:13:21 UTC (rev 
3143)
@@ -36,5 +36,10 @@
             $this->item->feedProcessor->setModuleItemData( $this->moduleName, 
$this->moduleObj, $this->item, $element, $value );
         }
     }
+
+    function __get( $element )
+    {
+        return $this->item->feedProcessor->getModuleItemData( 
$this->moduleName, $this->moduleObj, $this->item, $element );
+    }
 }
 ?>

Modified: trunk/Feed/src/structs/module_data.php
===================================================================
--- trunk/Feed/src/structs/module_data.php      2006-06-19 12:48:26 UTC (rev 
3142)
+++ trunk/Feed/src/structs/module_data.php      2006-06-19 14:13:21 UTC (rev 
3143)
@@ -36,5 +36,10 @@
             $this->feedProcessor->setModuleMetaData( $this->moduleName, 
$this->moduleObj, $element, $value );
         }
     }
+
+    function __get( $element )
+    {
+        return $this->feedProcessor->getModuleMetaData( $this->moduleName, 
$this->moduleObj, $element );
+    }
 }
 ?>

Modified: trunk/Feed/tests/feed_test.php
===================================================================

Modified: trunk/Feed/tests/rss2_test.php
===================================================================
--- trunk/Feed/tests/rss2_test.php      2006-06-19 12:48:26 UTC (rev 3142)
+++ trunk/Feed/tests/rss2_test.php      2006-06-19 14:13:21 UTC (rev 3143)
@@ -289,6 +289,44 @@
         self::assertEquals( $expected, $feed->generate() );
     }
 
+    public function testParseComplexWithModule1()
+    {
+        xdebug_break();
+        $feed = ezcFeed::parse( dirname( __FILE__ ) . "/data/rss2-09.xml" );
+        self::assertEquals( "<p>This is a richer <i>description</i> supported 
by dublin code.</p>", $feed->DublinCore->description );
+        self::assertEquals( "CreativeCommons", 
$feed->item(0)->DublinCore->rights );
+        self::assertEquals( "This is the second item", 
$feed->item(1)->description );
+        self::assertEquals( "Copyright only.", 
$feed->item(1)->DublinCore->rights );
+    }
+
+    public function testParseFeedItemIndexOutOfRange1()
+    {
+        $feed = ezcFeed::parse( dirname( __FILE__ ) . "/data/rss2-09.xml" );
+        try
+        {
+            $item = $feed->item(2);
+            self::fail( 'Expected exception not thrown' );
+        }
+        catch ( ezcFeedItemNrOutOfRangeException $e )
+        {
+            self::assertEquals( 'The given item number <2> is out of range 
<0..1>.', $e->getMessage() );
+        }
+    }
+
+    public function testParseFeedItemIndexOutOfRange2()
+    {
+        $feed = ezcFeed::parse( dirname( __FILE__ ) . "/data/rss2-09.xml" );
+        try
+        {
+            $item = $feed->item(-1);
+            self::fail( 'Expected exception not thrown' );
+        }
+        catch ( ezcFeedItemNrOutOfRangeException $e )
+        {
+            self::assertEquals( 'The given item number <-1> is out of range 
<0..1>.', $e->getMessage() );
+        }
+    }
+
     public static function suite()
     {
          return new ezcTestSuite( "ezcFeedRss2Test" );

-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to