Completed
Push — master ( fa5569...0fb0f7 )
by Nikola
03:21
created

SaxParser::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
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 4
    public function __construct(array $streamAdapters = array())
39
    {
40 4
        $this->streamAdapters = array();
41
42 4
        foreach ($streamAdapters as $streamAdapter) {
43 2
            $this->addStreamAdapter($streamAdapter);
44 4
        }
45 4
    }
46
47
    /**
48
     * Register stream adapter to parser.
49
     *
50
     * @param StreamAdapterInterface $streamAdapter Stream adapter to register.
51
     * @return SaxParser $this Fluent interface.
52
     */
53 4
    public function addStreamAdapter(StreamAdapterInterface $streamAdapter)
54
    {
55 4
        $this->streamAdapters[] = $streamAdapter;
56 4
        return $this;
57
    }
58
59
    /**
60
     * Parse XML document using provided SAX handler.
61
     *
62
     * @param SaxHandlerInterface $saxHandler Handler to user for parsing document.
63
     * @param mixed $xmlDocument XML document source.
64
     * @param callable|null $onResult Callable to execute when parsing is done.
65
     */
66 4
    public function parse(SaxHandlerInterface $saxHandler, $xmlDocument, callable $onResult = null)
67
    {
68 4
        $xmlDocument = $this->getDocumentStream($xmlDocument);
69 4
        $saxHandler->parse($xmlDocument, $onResult);
70 4
    }
71
72
    /**
73
     * Convert XML document to stream source.
74
     *
75
     * @param mixed $xmlDocument XML document source.
76
     * @return StreamInterface Converted XML document to stream.
77
     */
78 4
    private function getDocumentStream($xmlDocument)
79
    {
80 4
        if ($xmlDocument instanceof StreamInterface) {
81
            return $xmlDocument;
82
        }
83
84
        /**
85
         * @var StreamAdapterInterface $streamAdapter
86
         */
87 4
        foreach ($this->streamAdapters as $streamAdapter) {
88
89 4
            if ($streamAdapter->supports($xmlDocument)) {
90 4
                return $streamAdapter->convert($xmlDocument);
91
            }
92 4
        }
93
94
        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)));
95
    }
96
97
    /**
98
     * Default SAX parser factory.
99
     *
100
     * @param string $streamClass FQCN to use when converting to XML document source to stream.
101
     * @return SaxParser New SAX parser instance.
102
     */
103 2
    public static function factory($streamClass = 'GuzzleHttp\\Psr7\\Stream')
104
    {
105 2
        return new SaxParser(array(
106 2
            new ResourceAdapter($streamClass),
107 2
            new DomDocumentAdapter($streamClass),
108 2
            new SimpleXmlAdapter($streamClass)
109 2
        ));
110
    }
111
}
112