/** * Read an item from a SimpleXMLElement. * * @return SpoonRSSItem An instance of SpoonRSS. * @param SimpleXMLElement $item The XML-element that represents a single item in the feed. */ public static function readFromXML(SimpleXMLElement $item) { // get title, link and description $title = (string) $item->title; $link = (string) $item->link; $description = (string) $item->description; // create instance $rssItem = new SpoonFeedRSSItem($title, $link, $description); // add categories if (isset($item->category)) { foreach ($item->category as $category) { // set property $rssItem->addCategory((string) $category, $category['domain']); } } // set author if (isset($item->author)) { $rssItem->setAuthor((string) $item->author); } // set commentslink if (isset($item->comments)) { // try to set the commentslink try { // set commentslink $rssItem->setCommentsLink((string) $item->comments); } catch (Exception $e) { // ignore exceptions } } // set enclosure if (isset($item->enclosure['url']) && isset($item->enclosure['length']) && isset($item->enclosure['type'])) { // read data $URL = (string) $item->enclosure['url']; $length = (int) $item->enclosure['length']; $type = (string) $item->enclosure['type']; // try to set enclosure try { // set enclosure $rssItem->setEnclosure($URL, $length, $type); } catch (Exception $e) { // ignore exceptions } } // set guid if (isset($item->guid)) { // read data $URL = (string) $item->guid; $isPermaLink = (bool) $item->guid['isPermaLink']; // try to set GUID try { // set GUID $rssItem->setGuid($URL, $isPermaLink); } catch (Exception $e) { // ignore exceptions } } // set publication date if (isset($item->pubDate)) { $rssItem->setPublicationDate((int) strtotime($item->pubDate)); } // set source if (isset($item->source)) { // read data $name = (string) $item->source; $URL = (string) $item->source['url']; // try to set source try { // set source $rssItem->setSource($name, $URL); } catch (Exception $e) { // ignore exceptions } } return $rssItem; }