Passed
Push — master ( 9576e2...0c2f2c )
by Ryan
46:26 queued 06:02
created

SimplePie::setLibxml()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 1
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) 2017–2018 Ryan Parman <http://ryanparman.com>.
4
 * Copyright (c) 2017–2018 Contributors.
5
 *
6
 * http://opensource.org/licenses/Apache2.0
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimplePie;
12
13
use Psr\Http\Message\StreamInterface;
14
use Psr\Log\NullLogger;
15
use SimplePie\Configuration as C;
16
use SimplePie\Middleware\Xml\Atom;
17
use SimplePie\Mixin as Tr;
18
use SimplePie\Parser\Xml as XmlParser;
19
20
\define('SIMPLEPIE_ROOT', __DIR__);
21
22
/**
23
 * `SimplePie\SimplePie` is the primary entry point for SimplePie NG.
24
 */
25
class SimplePie implements C\SetLoggerInterface
26
{
27
    use Tr\LoggerTrait;
28
29
    /**
30
     * Bitwise libxml options to use for parsing XML.
31
     *
32
     * @var int
33
     */
34
    protected $libxml;
35
36
    /**
37
     * The handler stack which contains registered middleware.
38
     *
39
     * @var HandlerStackInterface
40
     */
41
    protected $middleware;
42
43
    /**
44
     * Constructs a new instance of this class.
45
     */
46 561
    public function __construct()
47
    {
48
        // Default logger
49 561
        $this->logger = new NullLogger();
50
51
        // Default middleware stack
52 561
        $this->middleware = new HandlerStack();
53 561
        $this->middleware->setLogger($this->getLogger());
54 561
        $this->middleware->append(new Atom(), 'atom');
55
56
        // Default libxml2 settings
57 561
        $this->libxml = LIBXML_HTML_NOIMPLIED // Required, or things crash.
58 561
            | LIBXML_BIGLINES
59 561
            | LIBXML_COMPACT
60 561
            | LIBXML_HTML_NODEFDTD
61 561
            | LIBXML_NOBLANKS
62 561
            | LIBXML_NOENT
63 561
            | LIBXML_NOXMLDECL
64 561
            | LIBXML_NSCLEAN
65 561
            | LIBXML_PARSEHUGE;
66 561
    }
67
68
    /**
69
     * Sets the libxml value to use for parsing XML.
70
     *
71
     * @param int $libxml
72
     *
73
     * @return int
74
     */
75 1
    public function setLibxml(int $libxml)
76
    {
77 1
        $this->libxml = $libxml;
78
79
        // What are the libxml2 configurations?
80 1
        $this->logger->debug(\sprintf(
81 1
            'Libxml configuration has a bitwise value of `%s`.%s',
82 1
            $this->libxml,
83 1
            (4792582 === $this->libxml)
84 1
                ? ' (This is the default configuration.)'
85 1
                : ''
86
        ));
87
88 1
        return $this;
89
    }
90
91
    /**
92
     * Gets the libxml value to use for parsing XML.
93
     *
94
     * @return int
95
     */
96 1
    public function getLibxml(): int
97
    {
98 1
        return $this->libxml;
99
    }
100
101
    /**
102
     * Sets the handler stack which contains registered middleware.
103
     *
104
     * @param HandlerStackInterface $handlerStack
105
     *
106
     * @return self
107
     */
108 560
    public function setMiddlewareStack(HandlerStackInterface $handlerStack)
109
    {
110 560
        $this->middleware = $handlerStack;
111 560
        $this->middleware->setLogger($this->getLogger());
112
113 560
        return $this;
114
    }
115
116
    /**
117
     * Gets the handler stack which contains registered middleware.
118
     *
119
     * @return HandlerStackInterface
120
     */
121
    public function getMiddlewareStack(): HandlerStackInterface
122
    {
123
        return $this->middleware;
124
    }
125
126
    /**
127
     * Parses content which is known to be valid XML and is encoded as UTF-8.
128
     *
129
     * @param StreamInterface $stream                  A PSR-7 `StreamInterface` which is typically returned by the
130
     *                                                 `getBody()` method of a `ResponseInterface` class.
131
     * @param bool            $handleHtmlEntitiesInXml Whether or not SimplePie should pre-parse the XML as HTML to
132
     *                                                 resolve the entities. A value of `true` means that SimplePie
133
     *                                                 should inject the entity definitions. A value of `false` means
134
     *                                                 that SimplePie should NOT inject the entity definitions. The
135
     *                                                 default value is `false`.
136
     *
137
     * @return XmlParser
138
     */
139 560
    public function parseXml(StreamInterface $stream, bool $handleHtmlEntitiesInXml = false): XmlParser
140
    {
141 560
        $parser = new XmlParser(
142 560
            $stream,
143 560
            $this->logger,
144 560
            $this->middleware,
145 560
            $this->libxml,
146
            $handleHtmlEntitiesInXml
147
        );
148
149 560
        return $parser;
150
    }
151
}
152