SaxParser   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 86
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A addStreamAdapter() 0 5 1
A parse() 0 5 2
A factory() 0 9 1
A getDocumentStream() 0 14 4
1
<?php
2
/*
3
 * This file is part of the runopencode/sax, an RunOpenCode project.
4
 *
5
 * (c) 2017 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\Exception\RuntimeException;
16
use RunOpenCode\Sax\StreamAdapter\DomDocumentAdapter;
17
use RunOpenCode\Sax\StreamAdapter\ResourceAdapter;
18
use RunOpenCode\Sax\StreamAdapter\SimpleXmlAdapter;
19
use RunOpenCode\Sax\StreamAdapter\StringAdapter;
20
21
/**
22
 * Class SaxParser
23
 *
24
 * Utility class for working with SAX handler and XML document.
25
 *
26
 * @package RunOpenCode\Sax
27
 */
28
class SaxParser
29
{
30
    /**
31
     * @var StreamAdapterInterface[]
32
     */
33
    private $streamAdapters;
34
35
    /**
36
     * SaxParser constructor.
37
     *
38
     * @param StreamAdapterInterface[] $streamAdapters Stream adapters to register to parser.
39
     */
40 3
    public function __construct(array $streamAdapters = array())
41
    {
42 3
        $this->streamAdapters = array();
43
44 3
        foreach ($streamAdapters as $streamAdapter) {
45 1
            $this->addStreamAdapter($streamAdapter);
46
        }
47 3
    }
48
49
    /**
50
     * Register stream adapter to parser.
51
     *
52
     * @param StreamAdapterInterface $streamAdapter Stream adapter to register.
53
     * @return SaxParser $this Fluent interface.
54
     */
55 3
    public function addStreamAdapter(StreamAdapterInterface $streamAdapter)
56
    {
57 3
        $this->streamAdapters[] = $streamAdapter;
58 3
        return $this;
59
    }
60
61
    /**
62
     * Parse XML document using provided SAX handler.
63
     *
64
     * @param SaxHandlerInterface $saxHandler Handler to user for parsing document.
65
     * @param mixed $xmlDocument XML document source.
66
     *
67
     * @return mixed Parsing result.
68
     */
69 3
    public function parse(SaxHandlerInterface $saxHandler, $xmlDocument)
70
    {
71 3
        $xmlDocument = ($xmlDocument instanceof StreamInterface) ? $xmlDocument : $this->getDocumentStream($xmlDocument);
72 2
        return $saxHandler->parse($xmlDocument);
73
    }
74
75
    /**
76
     * Default SAX parser factory.
77
     *
78
     * @param string $streamClass FQCN to use when converting to XML document source to stream.
79
     * @return SaxParser New SAX parser instance.
80
     */
81 1
    public static function factory($streamClass = 'GuzzleHttp\\Psr7\\Stream')
82
    {
83 1
        return new static([
84 1
            new ResourceAdapter($streamClass),
85 1
            new DomDocumentAdapter($streamClass),
86 1
            new SimpleXmlAdapter($streamClass),
87 1
            new StringAdapter($streamClass),
88
        ]);
89
    }
90
91
    /**
92
     * Convert XML document to stream source.
93
     *
94
     * @param mixed $xmlDocument XML document source.
95
     * @return StreamInterface Converted XML document to stream.
96
     *
97
     * @throws \RuntimeException
98
     */
99 3
    private function getDocumentStream($xmlDocument)
100
    {
101
        /**
102
         * @var StreamAdapterInterface $streamAdapter
103
         */
104 3
        foreach ($this->streamAdapters as $streamAdapter) {
105
106 3
            if ($streamAdapter->supports($xmlDocument)) {
107 3
                return $streamAdapter->convert($xmlDocument);
108
            }
109
        }
110
111 1
        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)));
112
    }
113
}
114