00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class FeedReader
00017 {
00018 private $xml = null;
00019 private $formats = array();
00020
00024 public static function create()
00025 {
00026 return new self;
00027 }
00028
00029 public function __construct()
00030 {
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 ?>