FeedReader.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Dmitry E. Pismenny, Dmitry A. Lomash            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class FeedReader
00016     {
00017         private $xml            = null;
00018         private $formats        = array();
00019         
00023         public static function create()
00024         {
00025             return new self;
00026         }
00027         
00028         public function __construct()
00029         {
00030             $this->formats[] = YandexRssFeedFormat::me();
00031             $this->formats[] = AtomFeedFormat::me();
00032             $this->formats[] = RssFeedFormat::me();
00033         }
00034         
00038         public function getXml()
00039         {
00040             return $this->xml;
00041         }
00042         
00046         public function parseFile($file)
00047         {
00048             try {
00049                 $this->xml = simplexml_load_file($file);
00050             } catch (BaseException $e) {
00051                 throw new WrongArgumentException(
00052                     'Invalid link or content: '.$e->getMessage()
00053                 );
00054             }
00055             
00056             if (!$this->xml)
00057                 throw new WrongStateException('simplexml_load_file failed.');
00058             
00059             return $this->parse();
00060         }
00061         
00065         public function parseXml($xml)
00066         {
00067             $this->xml = new SimpleXMLElement($xml);
00068             
00069             return $this->parse();
00070         }
00071         
00075         private function parse()
00076         {
00077             foreach ($this->formats as $format)
00078                 if ($format->isAcceptable($this->xml))
00079                     return $format->parse($this->xml);
00080             
00081             throw new WrongStateException(
00082                 'you\'re using unsupported format of feed'
00083             );
00084         }
00085     }
00086 ?>