/**
  * Read an item from a SimpleXMLElement.
  *
  * @return	SpoonAtomRSSItem				An instance of SpoonAtomRSSItem
  * @param	SimpleXMLElement $item			The XML-element that represents a single item in the feed.
  */
 public static function readFromXML(SimpleXMLElement $item)
 {
     // get title, id and summary
     $title = (string) $item->title;
     $id = (string) $item->id;
     $summary = (string) $item->summary;
     // create instance
     $rssItem = new SpoonFeedAtomRSSItem($title, $id, $summary);
     // set updated date
     if (isset($item->updated)) {
         $rssItem->setUpdatedDate((int) strtotime($item->updated));
     }
     // add authors
     if (isset($item->author)) {
         foreach ($item->author as $author) {
             // get the values
             $author['name'] = (string) $item->author->name;
             if (isset($item->author->email)) {
                 $author['email'] = (string) $item->author->email;
             }
             if (isset($item->author->uri)) {
                 $author['uri'] = (string) $item->author->uri;
             }
             // set the values
             $rssItem->addAuthor($author);
         }
     }
     // set content
     if (isset($item->content)) {
         $rssItem->setContent((string) $item->content);
     }
     // add links
     if (isset($item->link)) {
         foreach ($item->link as $link) {
             // build link
             $aLink['href'] = $link['href'];
             if (isset($link['rel'])) {
                 $aLink['rel'] = $link['rel'];
             }
             if (isset($link['type'])) {
                 $aLink['type'] = $link['type'];
             }
             if (isset($link['title'])) {
                 $aLink['title'] = $link['title'];
             }
             if (isset($link['hreflang'])) {
                 $aLink['hreflang'] = $link['hreflang'];
             }
             if (isset($link['length'])) {
                 $aLink['length'] = $link['length'];
             }
             // set property
             $rssItem->addLink($aLink);
         }
     }
     // add categories
     if (isset($item->category)) {
         foreach ($item->category as $category) {
             // build category
             $cat['term'] = (string) $category['term'];
             if (isset($category['scheme'])) {
                 $cat['scheme'] = (string) $category['scheme'];
             }
             if (isset($category['label'])) {
                 $cat['label'] = (string) $category['label'];
             }
             // set property
             $rssItem->addCategory($cat);
         }
     }
     // add contributors
     if (isset($item->contributor)) {
         foreach ($item->contributor as $contributor) {
             $name = (string) $contributor['name'];
             $email = isset($contributor['scheme']) ? (string) $contributor['email'] : null;
             $uri = isset($contributor['label']) ? (string) $contributor['uri$contributor'] : null;
             // set property
             $rssItem->addContributor($name, $email, $uri);
         }
     }
     // set publication date
     if (isset($item->published)) {
         $rssItem->setPublicationDate((int) strtotime($item->published));
     }
     // set rights
     if (isset($XML->rights)) {
         $rssItem->setRights((string) $XML->rights);
     }
     return $rssItem;
 }
Exemple #2
0
 /**
  * Reads an feed into a SpoonRSS object.
  *
  * @return	SpoonAtomRSS				Returns as an instance of SpoonAtomRSS.
  * @param	string $URL					An URL where the feed is located or the XML of the feed.
  * @param	string[optional] $type		The type of feed, possible values are: url, string.
  * @param	bool[optional] $force		Force to read this feed without validation.
  */
 public static function readFromFeed($URL, $type = 'url', $force = false)
 {
     // redefine var
     $URL = (string) $URL;
     $type = (string) SpoonFilter::getValue($type, array('url', 'string'), 'url');
     // validate
     if ($type == 'url' && !SpoonFilter::isURL($URL)) {
         throw new SpoonFeedException('This (' . SpoonFilter::htmlentities($URL) . ') isn\'t a valid URL.');
     }
     if (!$force) {
         if (!self::isValid($URL, $type)) {
             throw new SpoonFeedException('Invalid feed');
         }
     }
     // load xmlstring
     if ($type == 'url') {
         $xmlString = SpoonHTTP::getContent($URL);
     } else {
         $xmlString = $URL;
     }
     // convert to simpleXML
     $XML = @simplexml_load_string($xmlString);
     // validate the feed
     if ($XML === false) {
         throw new SpoonFeedException('Invalid rss-string.');
     }
     // get title, link and description
     $title = (string) $XML->title;
     $id = (string) $XML->id;
     // create instance
     $RSS = new SpoonFeedAtomRSS($title, $id);
     // add authors
     if (isset($XML->author)) {
         foreach ($XML->author as $author) {
             // get the values
             $author['name'] = (string) $XML->author->name;
             $author['email'] = isset($XML->author->email) ? (string) $XML->author->email : null;
             $author['uri'] = isset($XML->author->uri) ? (string) $XML->author->uri : null;
             // set the values
             $RSS->addAuthor($author);
         }
     }
     // add contributors
     if (isset($XML->contributor)) {
         foreach ($XML->contributor as $contributor) {
             $name = (string) $contributor['name'];
             $email = isset($contributor['scheme']) ? (string) $contributor['email'] : null;
             $uri = isset($contributor['label']) ? (string) $contributor['uri$contributor'] : null;
             // set property
             $RSS->addContributor($name, $email, $uri);
         }
     }
     // add categories
     if (isset($XML->category)) {
         foreach ($XML->category as $category) {
             // build category
             $cat['term'] = (string) $category['term'];
             if (isset($category['scheme'])) {
                 $cat['scheme'] = (string) $category['scheme'];
             }
             if (isset($category['label'])) {
                 $cat['label'] = (string) $category['label'];
             }
             // set property
             $RSS->addCategory($cat);
         }
     }
     // add links
     if (isset($XML->link)) {
         foreach ($XML->link as $link) {
             // build link
             $aLink['href'] = $link['href'];
             if (isset($link['rel'])) {
                 $aLink['rel'] = $link['rel'];
             }
             if (isset($link['type'])) {
                 $aLink['type'] = $link['type'];
             }
             if (isset($link['title'])) {
                 $aLink['title'] = $link['title'];
             }
             if (isset($link['hreflang'])) {
                 $aLink['hreflang'] = $link['hreflang'];
             }
             if (isset($link['length'])) {
                 $aLink['length'] = $link['length'];
             }
             // set property
             $RSS->addLink($aLink);
         }
     }
     // add items
     foreach ($XML->entry as $item) {
         // try to read
         try {
             // read xml
             $item = SpoonFeedAtomRSSItem::readFromXML($item);
             $RSS->addItem($item);
         } catch (Exception $e) {
             // ignore exceptions
         }
     }
     // set updated date
     if (isset($XML->updated)) {
         $RSS->setUpdatedDate((int) strtotime($XML->updated));
     }
     // set generator
     if (isset($XML->generator)) {
         $RSS->setGenerator((string) $XML->generator);
     }
     // set icon
     if (isset($XML->icon)) {
         $RSS->setIcon((string) $XML->icon);
     }
     // set logo
     if (isset($XML->logo)) {
         $RSS->setLogo((string) $XML->logo);
     }
     // set rights
     if (isset($XML->rights)) {
         $RSS->setRights((string) $XML->rights);
     }
     // return
     return $RSS;
 }