123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace PicoFeed\Serialization;
- use PicoFeed\Parser\MalformedXmlException;
- use PicoFeed\Parser\XmlParser;
- use SimpleXMLElement;
- /**
- * Class SubscriptionListParser
- *
- * @package PicoFeed\Serialization
- * @author Frederic Guillot
- */
- class SubscriptionListParser
- {
- /**
- * @var SubscriptionList
- */
- protected $subscriptionList;
- /**
- * @var string
- */
- protected $data;
- /**
- * Constructor
- *
- * @access public
- * @param string $data
- */
- public function __construct($data)
- {
- $this->subscriptionList = new SubscriptionList();
- $this->data = trim($data);
- }
- /**
- * Get object instance
- *
- * @static
- * @access public
- * @param string $data
- * @return SubscriptionListParser
- */
- public static function create($data)
- {
- return new static($data);
- }
- /**
- * Parse a subscription list entry
- *
- * @access public
- * @throws MalformedXmlException
- * @return SubscriptionList
- */
- public function parse()
- {
- $xml = XmlParser::getSimpleXml($this->data);
- if (! $xml || !isset($xml->head) || !isset($xml->body)) {
- throw new MalformedXmlException('Unable to parse OPML file: invalid XML');
- }
- $this->parseTitle($xml->head);
- $this->parseEntries($xml->body);
- return $this->subscriptionList;
- }
- /**
- * Parse title
- *
- * @access protected
- * @param SimpleXMLElement $xml
- */
- protected function parseTitle(SimpleXMLElement $xml)
- {
- $this->subscriptionList->setTitle((string) $xml->title);
- }
- /**
- * Parse entries
- *
- * @access protected
- * @param SimpleXMLElement $body
- */
- private function parseEntries(SimpleXMLElement $body)
- {
- foreach ($body->outline as $outlineElement) {
- if (isset($outlineElement->outline)) {
- $this->parseEntries($outlineElement);
- } else {
- $this->subscriptionList->subscriptions[] = SubscriptionParser::create($body, $outlineElement)->parse();
- }
- }
- }
- }
|