Completed
Push — master ( 91bb83...325361 )
by Nikola
05:28
created

SaxParser::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 3
crap 2
1
<?php
2
/*
3
 * This file is part of the runopencode/sax, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Sax;
11
12
use Psr\Http\Message\StreamInterface;
13
use RunOpenCode\Sax\Contract\SaxHandlerInterface;
14
use RunOpenCode\Sax\Contract\StreamAdapterInterface;
15
use RunOpenCode\Sax\StreamAdapter\DomDocumentAdapter;
16
use RunOpenCode\Sax\StreamAdapter\ResourceAdapter;
17
use RunOpenCode\Sax\StreamAdapter\SimpleXmlAdapter;
18
19
/**
20
 * Class SaxParser
21
 *
22
 * Utility class for working with SAX handler and XML document.
23
 *
24
 * @package RunOpenCode\Sax
25
 */
26
final class SaxParser
27
{
28
    /**
29
     * @var StreamAdapterInterface[]
30
     */
31
    private $streamAdapters;
32
33
    /**
34
     * SaxParser constructor.
35
     *
36
     * @param StreamAdapterInterface[] $streamAdapters Stream adapters to register to parser.
37
     */
38
    public function __construct(array $streamAdapters = array())
39
    {
40
        $this->streamAdapters = array();
41
42
        foreach ($streamAdapters as $streamAdapter) {
43
            $this->addStreamAdapter($streamAdapter);
44
        }
45
    }
46
47
    /**
48
     * Register stream adapter to parser.
49
     *
50
     * @param StreamAdapterInterface $streamAdapter Stream adapter to register.
51
     */
52
    public function addStreamAdapter(StreamAdapterInterface $streamAdapter)
53
    {
54
        $this->streamAdapters[] = $streamAdapter;
55
    }
56
57
    /**
58
     * Parse XML document using provided SAX handler.
59
     *
60
     * @param SaxHandlerInterface $saxHandler Handler to user for parsing document.
61
     * @param mixed $xmlDocument XML document source.
62
     * @param callable|null $onResult Callable to execute when parsing is done.
63
     */
64
    public function parse(SaxHandlerInterface $saxHandler, $xmlDocument, callable $onResult = null)
65
    {
66
        $xmlDocument = $this->getDocumentStream($xmlDocument);
67
        $saxHandler->parse($xmlDocument, $onResult);
68
    }
69
70
    /**
71
     * Convert XML document to stream source.
72
     *
73
     * @param mixed $xmlDocument XML document source.
74
     * @return StreamInterface Converted XML document to stream.
75
     */
76
    private function getDocumentStream($xmlDocument)
77
    {
78
        if ($xmlDocument instanceof StreamInterface) {
79
            return $xmlDocument;
80
        }
81
82
        /**
83
         * @var StreamAdapterInterface $streamAdapter
84
         */
85
        foreach ($this->streamAdapters as $streamAdapter) {
86
87
            if ($streamAdapter->supports($xmlDocument)) {
88
                return $streamAdapter->convert($xmlDocument);
89
            }
90
        }
91
92
        throw new \RuntimeException(sprintf('Suitable XML document stream adapter is not registered for XML document of type "%s".', is_object($xmlDocument) ? get_class($xmlDocument) : gettype($xmlDocument)));
93
    }
94
95
    /**
96
     * Default SAX parser factory.
97
     *
98
     * @param string $streamClass FQCN to use when converting to XML document source to stream.
99
     * @return SaxParser New SAX parser instance.
100
     */
101
    public static function factory($streamClass = 'GuzzleHttp\\Stream\\Stream')
102
    {
103
        return new SaxParser(array(
104
            new ResourceAdapter($streamClass),
105
            new DomDocumentAdapter($streamClass),
106
            new SimpleXmlAdapter($streamClass)
107
        ));
108
    }
109
}
110