Completed
Push — master ( 1baea9...3a6ccc )
by Joschi
02:50
created

HtmlDocumentFactory::processParsingErrors()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
 * rdfa-lite-microdata
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\RdfaLiteMicrodata
8
 * @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\RdfaLiteMicrodata\Infrastructure\Factories;
38
39
use Jkphl\RdfaLiteMicrodata\Application\Contract\DocumentFactoryInterface;
40
use Jkphl\RdfaLiteMicrodata\Infrastructure\Exceptions\InvalidArgumentException;
41
42
/**
43
 * HTML document factory
44
 *
45
 * @package Jkphl\RdfaLiteMicrodata
46
 * @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure
47
 */
48
class HtmlDocumentFactory implements DocumentFactoryInterface
49
{
50
    /**
51
     * HTML5 elements
52
     *
53
     * @var array
54
     */
55
    protected static $html5 = [
56
        'a',
57
        'abbr',
58
        'acronym',
59
        'address',
60
        'applet',
61
        'area',
62
        'article',
63
        'aside',
64
        'audio',
65
        'b',
66
        'base',
67
        'basefont',
68
        'bdi',
69
        'bdo',
70
        'bgsound',
71
        'big',
72
        'blink',
73
        'blockquote',
74
        'body',
75
        'br',
76
        'button',
77
        'canvas',
78
        'caption',
79
        'center',
80
        'cite',
81
        'code',
82
        'col',
83
        'colgroup',
84
        'content',
85
        'data',
86
        'datalist',
87
        'dd',
88
        'decorator',
89
        'del',
90
        'details',
91
        'dfn',
92
        'dir',
93
        'div',
94
        'dl',
95
        'dt',
96
        'element',
97
        'em',
98
        'embed',
99
        'fieldset',
100
        'figcaption',
101
        'figure',
102
        'font',
103
        'footer',
104
        'form',
105
        'frame',
106
        'frameset',
107
        'h1',
108
        'h2',
109
        'h3',
110
        'h4',
111
        'h5',
112
        'h6',
113
        'head',
114
        'header',
115
        'hgroup',
116
        'hr',
117
        'html',
118
        'i',
119
        'iframe',
120
        'img',
121
        'input',
122
        'ins',
123
        'isindex',
124
        'kbd',
125
        'keygen',
126
        'label',
127
        'legend',
128
        'li',
129
        'link',
130
        'listing',
131
        'main',
132
        'map',
133
        'mark',
134
        'marquee',
135
        'menu',
136
        'menuitem',
137
        'meta',
138
        'meter',
139
        'nav',
140
        'nobr',
141
        'noframes',
142
        'noscript',
143
        'object',
144
        'ol',
145
        'optgroup',
146
        'option',
147
        'output',
148
        'p',
149
        'param',
150
        'plaintext',
151
        'pre',
152
        'progress',
153
        'q',
154
        'rp',
155
        'rt',
156
        'ruby',
157
        's',
158
        'samp',
159
        'script',
160
        'section',
161
        'select',
162
        'shadow',
163
        'small',
164
        'source',
165
        'spacer',
166
        'span',
167
        'strike',
168
        'strong',
169
        'style',
170
        'sub',
171
        'summary',
172
        'sup',
173
        'table',
174
        'tbody',
175
        'td',
176
        'template',
177
        'textarea',
178
        'tfoot',
179
        'th',
180
        'thead',
181
        'time',
182
        'title',
183
        'tr',
184
        'track',
185
        'tt',
186
        'u',
187
        'ul',
188
        'var',
189
        'video',
190
        'wbr',
191
        'xmp'
192
    ];
193
194
    /**
195
     * Create a DOM document from a source
196
     *
197
     * @param mixed $source Source
198
     * @return \DOMDocument DOM document
199
     */
200 14
    public function createDocumentFromSource($source)
201
    {
202 14
        $dom = new \DOMDocument();
203 14
        libxml_use_internal_errors(true);
204 14
        $dom->loadHTML($source, LIBXML_NOWARNING);
205 14
        $errors = libxml_get_errors();
206 14
        libxml_use_internal_errors(false);
207 14
        $this->processParsingErrors($errors);
208 13
        return $dom;
209
    }
210
211
    /**
212
     * Process parsing errors
213
     *
214
     * @param \LibXMLError[] $errors Parsing errors
215
     * @throws InvalidArgumentException If it's not a HTML5 error
216
     */
217 14
    protected function processParsingErrors(array $errors)
218
    {
219 14
        foreach ($errors as $error) {
220 5
            if ($this->isNotInvalidHtml5TagError($error)) {
221 1
                throw new InvalidArgumentException($error->message, $error->code);
222
            }
223 13
        }
224 13
    }
225
226
    /**
227
     * Test if a parsing error is not because of an "invalid" HTML5 tag
228
     *
229
     * @param \LibXMLError $error Parsing error
230
     * @return bool Error is not because of an "invalid" HTML5 tag
231
     */
232 5
    protected function isNotInvalidHtml5TagError(\LibXMLError $error)
233
    {
234 5
        return ($error->code != 801) ||
235
            (
236 5
                preg_match('/^tag\s+(\S+)\s+invalid$/', strtolower($error->message), $tag) &&
237 5
                !in_array($tag[1], self::$html5)
238 5
            );
239
    }
240
}
241