Completed
Push — master ( 2991a0...bc5f9a )
by Ryan
03:06
created

SimplePie::parseJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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
    public function __construct()
47
    {
48
        // Default logger
49
        $this->logger = new NullLogger();
50
51
        // Default middleware stack
52
        $this->middleware = new HandlerStack();
53
        $this->middleware->setLogger($this->getLogger());
54
        $this->middleware->append(new Atom(), 'atom');
55
56
        // Default libxml2 settings
57
        $this->libxml = LIBXML_HTML_NOIMPLIED // Required, or things crash.
58
            | LIBXML_BIGLINES
59
            | LIBXML_COMPACT
60
            | LIBXML_HTML_NODEFDTD
61
            | LIBXML_NOBLANKS
62
            | LIBXML_NOENT
63
            | LIBXML_NOXMLDECL
64
            | LIBXML_NSCLEAN
65
            | LIBXML_PARSEHUGE;
66
    }
67
68
    /**
69
     * Sets the libxml value to use for parsing XML.
70
     *
71
     * @param int $libxml
72
     *
73
     * @return int
74
     */
75
    public function setLibxml(int $libxml)
76
    {
77
        $this->libxml = $libxml;
78
79
        // What are the libxml2 configurations?
80
        $this->logger->debug(\sprintf(
81
            'Libxml configuration has a bitwise value of `%s`.%s',
82
            $this->libxml,
83
            (4792582 === $this->libxml)
84
                ? ' (This is the default configuration.)'
85
                : ''
86
        ));
87
88
        return $this;
89
    }
90
91
    /**
92
     * Gets the libxml value to use for parsing XML.
93
     *
94
     * @return int
95
     */
96
    public function getLibxml(): int
97
    {
98
        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
    public function setMiddlewareStack(HandlerStackInterface $handlerStack)
109
    {
110
        $this->middleware = $handlerStack;
111
        $this->middleware->setLogger($this->getLogger());
112
113
        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
    public function parseXml(StreamInterface $stream, bool $handleHtmlEntitiesInXml = false): XmlParser
140
    {
141
        $parser = new XmlParser(
142
            $stream,
143
            $this->logger,
144
            $this->middleware,
145
            $this->libxml,
146
            $handleHtmlEntitiesInXml
147
        );
148
149
        return $parser;
150
    }
151
}
152