예제 #1
0
 /**
  * @param String $url
  * @param int $id
  * @return Photo[]
  */
 public static function getPhotos($url)
 {
     $xh = new XMLHandler($url);
     $photoItems = $xh->getNodes("photo");
     $photoList = array();
     foreach ($photoItems as $photoNode) {
         $p = new Photo();
         $p->setId($photoNode->getElementsByTagName("id")->item(0)->textContent);
         $p->setAlt($photoNode->getElementsByTagName("htmlAlt")->item(0)->textContent);
         $p->setOrientation($photoNode->getElementsByTagName("orientation")->item(0)->textContent);
         //set thumbnail pic and large pic
         $photoInstancesNode = $photoNode->getElementsByTagName("instance");
         foreach ($photoInstancesNode as $pi) {
             $type = $pi->getElementsByTagName("type")->item(0)->textContent;
             /* @var $pi DomElement */
             if ($type == "Thumbnail") {
                 $p->getThumb()->parsePhotoInstance($pi);
             } elseif ($type == "Large") {
                 $p->getLarge()->parsePhotoInstance($pi);
             } elseif ($type == "HighRes") {
                 $p->getHiRes()->parsePhotoInstance($pi);
             } elseif ($type == "Custom") {
                 $p->getCustom()->parsePhotoInstance($pi);
             }
         }
         $photoList[] = $p;
     }
     return $photoList;
 }
 /**
  * @param String $url
  * @return array[int]Category
  */
 public static function getCategories($url)
 {
     $xh = new XMLHandler($url);
     $nl = $xh->getNodes("category");
     $catList = array();
     foreach ($nl as $n) {
         $c = new NewsCategory();
         $c->id = $n->getElementsByTagName("id")->item(0)->textContent;
         $c->name = $n->getElementsByTagName("name")->item(0)->textContent;
         $catList[] = $c;
     }
     return $catList;
 }
 /**
  * @param String $url
  * @return array[int]Category
  */
 public static function getCategories($url)
 {
     $xh = new XMLHandler($url);
     $nl = $xh->getNodes("category");
     $catList = array();
     foreach ($nl as $n) {
         $c = new NewsCategory();
         $c->id = $n->getElementsByTagName("id")->item(0)->textContent;
         $c->name = $n->getElementsByTagName("name")->item(0)->textContent;
         //JLog::add('category name:'. $c->name, JLog::INFO, 'com_braftonarticles');
         $catList[] = $c;
     }
     return $catList;
 }
 /**
  * @param string $url
  * @param int $id
  * @return Comment[]
  */
 public static function getComments($url)
 {
     $xh = new XMLHandler($url);
     $nl = $xh->getNodes("commentListItem");
     $commentList = array();
     foreach ($nl as $n) {
         $c = new NewsComment();
         $c->setID($n->getElementsByTagName("id")->item(0)->textContent);
         $c->setLocation($n->getElementsByTagName("location")->item(0)->textContent);
         $c->setUser($n->getElementsByTagName("name")->item(0)->textContent);
         $c->setCommentTxt($n->getElementsByTagName("text")->item(0)->textContent);
         $c->setPostDate($n->getElementsByTagName("postDate")->item(0)->textContent);
         $commentList[] = $c;
     }
     return $commentList;
 }
 /**
  * @param String $url
  * @return NewsItem[]
  */
 public static function getNewsList($url, $format)
 {
     //Exception thrown in XMLHandler constructor if url is incorrect
     $xh = new XMLHandler($url);
     $newsList = array();
     if (isset($xh)) {
         $news = $xh->getNodes(NEWS_LIST_ITEM);
         $exceptionList = array();
         foreach ($news as $n) {
             /* @var $n DomElement */
             $ni = new NewsItem();
             try {
                 //Check if all required nodes exist, throw exception if not!
                 if ($n->getElementsByTagName(ID)->length == 0) {
                     throw new XMLNodeException("Element " . ID . " for " . NEWS_LIST_ITEM);
                 }
                 //set value of ID here to use in debugging!
                 $ni->id = $n->getElementsByTagName(ID)->item(0)->textContent;
                 if ($n->getElementsByTagName(PUBLISH_DATE)->length == 0) {
                     throw new XMLNodeException("Element " . PUBLISH_DATE . " for " . NEWS_LIST_ITEM . " with id: " . $ni->id);
                 }
                 if (!$n->getAttribute(HREF)) {
                     throw new XMLNodeException("Attribute " . HREF . " for " . NEWS_LIST_ITEM . " with id: " . $ni->id);
                 }
                 if ($n->getElementsByTagName(HEADLINE)->length == 0) {
                     throw new XMLNodeException("Element " . HEADLINE . " for " . NEWS_LIST_ITEM . " with id: " . $ni->id);
                 }
                 //Check if date is valid if not throw exception
                 $ni->publishDate = $n->getElementsByTagName(PUBLISH_DATE)->item(0)->textContent;
                 $dateIsValid = date_parse($ni->publishDate);
                 if (!$dateIsValid) {
                     throw new DateParseException("Invalid Date for " . PUBLISH_DATE . "  on " . NEWS_LIST_ITEM . " with id: " . $ni->id . "<br />\n");
                 }
                 //Set the value of all other required elements
                 $ni->href = $n->getAttribute(HREF);
                 $ni->headline = $n->getElementsByTagName(HEADLINE)->item(0)->textContent;
                 $ni->format = $format;
                 //Add to newslist array
                 $newsList[] = $ni;
             } catch (XMLException $e) {
                 $exceptionList[] = $e;
                 //Add exception to a list
             } catch (DateParseException $e) {
                 $exceptionList[] = $e;
             }
         }
         //If exception list contains any exceptions throw a new exception which relays all exceptions to the user
         if (!empty($exceptionList)) {
             echo implode("<br />", $exceptionList) . "<br /><br />";
         }
     }
     return $newsList;
 }