/** * @param SimpleXMLElement $parent * @param Feed $feed * * @throws \imnotjames\Syndicator\Exceptions\SerializationException */ private function serializeFeed(SimpleXMLElement $parent, Feed $feed) { $channelXML = $parent->addChild('channel'); $title = $feed->getTitle(); $subtitle = $feed->getSubtitle(); if (!empty($subtitle)) { $title .= $this->subtitleSeparator; $title .= $subtitle; } // Required $channelXML->addChild('title', $title); $channelXML->addChild('link', $feed->getURI()); $channelXML->addChild('description', $feed->getDescription()); // optional $language = $feed->getLanguage(); if (!is_null($language)) { $channelXML->addChild('language', $language); } $datePublished = $feed->getDatePublished(); if (!is_null($datePublished)) { $channelXML->addChild('pubDate', $datePublished->format(self::DATE_FORMAT)); } $dateUpdated = $feed->getDateUpdated(); if (!is_null($dateUpdated)) { $channelXML->addChild('lastBuildDate', $dateUpdated->format(self::DATE_FORMAT)); } if (is_null($feed->getGenerator())) { $channelXML->addChild('generator', $this->generator); } else { $channelXML->addChild('generator', $feed->getGenerator()); } $logo = $feed->getLogo(); if (!is_null($logo)) { $this->serializeLogo($channelXML, $logo); } $categories = $feed->getCategories(); foreach ($categories as $category) { $this->serializeCategory($channelXML, $category); } // Generate the feed items $articles = $feed->getArticles(); foreach ($articles as $article) { $this->serializeArticle($channelXML, $article); } }
/** * Parse a Feed object from xml * * @param SimpleXMLElement $xml * * @return Feed */ private function parseFeed(SimpleXMLElement $xml) { $title = (string) $xml->channel->title; $link = (string) $xml->channel->link; $description = (string) $xml->channel->description; $feed = new Feed($title, $description, $link); if (isset($xml->channel->item)) { foreach ($xml->channel->item as $item) { $article = $this->parseArticle($item); if (!is_null($article)) { $feed->addArticle($article); } } } if (isset($xml->channel->category)) { foreach ($xml->channel->category as $category) { $feed->addCategory($this->parseCategory($category)); } } if (isset($xml->channel->generator)) { $feed->setGenerator((string) $xml->channel->generator); } if (isset($xml->channel->cloud)) { $feed->setSubscription($this->parseSubscription($xml->channel->cloud)); } if (isset($xml->channel->image)) { $feed->setLogo($this->parseLogo($xml->channel->image)); } if (isset($xml->channel->managingEditor)) { $feed->setEditorContact($this->parseContact($xml->channel->managingEditor)); } if (isset($xml->channel->webMaster)) { $feed->setWebmasterContact($this->parseContact($xml->channel->webMaster)); } if (isset($xml->channel->docs)) { $feed->setDocumentationURI((string) $xml->channel->docs); } if (isset($xml->channel->language)) { $feed->setLanguage((string) $xml->channel->language); } if (isset($xml->channel->ttl)) { $feed->setCacheTimeToLive(intval($xml->channel->ttl)); } if (isset($xml->channel->pubDate)) { $feed->setDatePublished($this->parseDateTime($xml->channel->pubDate)); } if (isset($xml->channel->lastBuildDate)) { $feed->setDateUpdated($this->parseDateTime($xml->channel->lastBuildDate)); } if (isset($xml->channel->skipHours)) { foreach ($xml->channel->skipHours as $skip) { $feed->addSkip(new SkipHour(intval((string) $skip))); } } if (isset($xml->channel->skipDays)) { foreach ($xml->channel->skipDays as $skip) { $feed->addSkip(new SkipWeekday((string) $skip)); } } return $feed; }