wp-all-import
/
classes
/
XmlStreamReader
/
prewk
/
xml-string-streamer
/
src
/
XmlStringStreamer.php
XmlStringStreamer.php
77 lines
| 1 | <?php |
| 2 | /** |
| 3 | * xml-string-streamer base class |
| 4 | * |
| 5 | * @package xml-string-streamer |
| 6 | * @author Oskar Thornblad <oskar.thornblad@gmail.com> |
| 7 | */ |
| 8 | |
| 9 | namespace Prewk; |
| 10 | |
| 11 | use Prewk\XmlStringStreamer\ParserInterface; |
| 12 | use Prewk\XmlStringStreamer\StreamInterface; |
| 13 | use Prewk\XmlStringStreamer\Parser; |
| 14 | use Prewk\XmlStringStreamer\Stream; |
| 15 | |
| 16 | /** |
| 17 | * The base class for the xml-string-streamer |
| 18 | */ |
| 19 | class XmlStringStreamer |
| 20 | { |
| 21 | /** |
| 22 | * The current parser |
| 23 | * @var ParserInterface |
| 24 | */ |
| 25 | protected $parser; |
| 26 | /** |
| 27 | * The current stream |
| 28 | * @var StreamInterface |
| 29 | */ |
| 30 | protected $stream; |
| 31 | |
| 32 | /** |
| 33 | * Constructs the XML streamer |
| 34 | * @param ParserInterface $parser A parser with options set |
| 35 | * @param StreamInterface $stream A stream for the parser to use |
| 36 | */ |
| 37 | public function __construct(ParserInterface $parser, StreamInterface $stream) |
| 38 | { |
| 39 | $this->parser = $parser; |
| 40 | $this->stream = $stream; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Convenience method for creating a StringWalker parser with a File stream |
| 45 | * @param string|resource $file File path or handle |
| 46 | * @param array $options Parser configuration |
| 47 | * @return XmlStringStreamer A streamer ready for use |
| 48 | */ |
| 49 | public static function createStringWalkerParser($file, $options = array()) |
| 50 | { |
| 51 | $stream = new Stream\File($file, 16384); |
| 52 | $parser = new Parser\StringWalker($options); |
| 53 | return new XmlStringStreamer($parser, $stream); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Convenience method for creating a UniqueNode parser with a File stream |
| 58 | * @param string|resource $file File path or handle |
| 59 | * @param array $options Parser configuration |
| 60 | * @return XmlStringStreamer A streamer ready for use |
| 61 | */ |
| 62 | public static function createUniqueNodeParser($file, $options = array()) |
| 63 | { |
| 64 | $stream = new Stream\File($file, 16384); |
| 65 | $parser = new Parser\UniqueNode($options); |
| 66 | return new XmlStringStreamer($parser, $stream); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Gets the next node from the parser |
| 71 | * @return bool|string The xml string or false |
| 72 | */ |
| 73 | public function getNode() |
| 74 | { |
| 75 | return $this->parser->getNodeFrom($this->stream); |
| 76 | } |
| 77 | } |