Example #1
0
 /**
  * @param SimpleXMLElement $parent
  * @param Article          $article
  *
  * @throws \imnotjames\Syndicator\Exceptions\SerializationException
  */
 private function serializeArticle(SimpleXMLElement $parent, Article $article)
 {
     $itemXML = $parent->addChild('item');
     $title = $article->getTitle();
     $link = $article->getURI();
     $description = $article->getDescription();
     if (is_null($title) && is_null($description)) {
         throw new SerializationException('Articles must have at least a title or description');
     }
     if (!is_null($title)) {
         $itemXML->addChild('title', $title);
     }
     if (!is_null($link)) {
         $itemXML->addChild('link', $link);
     }
     if (!is_null($description)) {
         $itemXML->addChild('description', $description);
     }
     $categories = $article->getCategories();
     foreach ($categories as $category) {
         $categoryXML = $itemXML->addChild('category', $category->getName());
         if (!is_null($category->getTaxonomy())) {
             $categoryXML['domain'] = $category->getTaxonomy();
         }
     }
     $publishedDate = $article->getDatePublished();
     if (!is_null($publishedDate)) {
         $itemXML->addChild('pubDate', $publishedDate->format(self::DATE_FORMAT));
     }
     $guid = $article->getID();
     if (!empty($guid)) {
         $itemXML->addChild('guid', $guid);
     } else {
         $permalinkXML = $itemXML->addChild('guid', hash('sha256', $article->getTitle() . $article->getURI() . $article->getDescription()));
         $permalinkXML->addAttribute('isPermaLink', 'false');
     }
     $attachments = $article->getAttachments();
     $enclosures = array_filter($attachments, function ($a) {
         return $a instanceof Link && $a->getLinkType() === Link::TYPE_ENCLOSURE;
     });
     $comments = array_filter($attachments, function ($a) {
         return $a instanceof Link && $a->getLinkType() === Link::TYPE_COMMENT;
     });
     foreach ($enclosures as $enclosure) {
         $enclosureXML = $itemXML->addChild('enclosure');
         $enclosureXML->addAttribute('url', $enclosure->getURI());
         $enclosureXML->addAttribute('length', $enclosure->getLength());
         $enclosureXML->addAttribute('type', $enclosure->getMediaType());
     }
     $author = $article->getAuthor();
     if (!is_null($author)) {
         $email = $author->getEmail();
         $name = $author->getName();
         if (!empty($name)) {
             $itemXML->addChild('author', sprintf('%s (%s)', $email, $name));
         } else {
             $itemXML->addChild('author', $email);
         }
     }
     foreach ($comments as $comment) {
         $itemXML->addChild('comments', $comment->getURI());
     }
     $source = $article->getSource();
     if (!is_null($source)) {
         $sourceXML = $itemXML->addChild('source', $source->getTitle());
         $sourceXML->addAttribute('url', $source->getURI());
     }
 }