/** * @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()); } }
/** * @param SimpleXMLElement $item * * @return Article * @throws \imnotjames\Syndicator\Exceptions\InvalidURIException */ private function parseArticle(SimpleXMLElement $item) { $article = new Article(); $article->setTitle((string) $item->title); $article->setURI((string) $item->link); $article->setDescription((string) $item->description); if (isset($item->author)) { $article->setAuthor($this->parseContact($item->author)); } if (isset($item->pubDate)) { $article->setDatePublished($this->parseDateTime($item->pubDate)); } if (isset($item->category)) { foreach ($item->category as $category) { $article->addCategory($this->parseCategory($category)); } } if (isset($item->enclosure)) { foreach ($item->enclosure as $enclosure) { $article->addAttachment($this->parseEnclosure($enclosure)); } } if (isset($item->guid)) { $article->setID((string) $item->guid); } if (isset($item->source)) { $article->setSource($this->parseSource($item->source)); } return $article; }