Passed
Push — main ( d61a4b...a3ea10 )
by Oscar
12:17
created

Html5Util   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
dl 0
loc 44
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A loadHtml() 0 17 2
A toHtml() 0 16 3
1
<?php
2
3
/*
4
 * This file is part of ocubom/twig-svg-extension
5
 *
6
 * © Oscar Cubo Medina <https://ocubom.github.io>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ocubom\Twig\Extension\Svg\Util;
13
14
use Masterminds\HTML5;
15
use Ocubom\Twig\Extension\Svg\Exception\Html5Exception;
16
use Ocubom\Twig\Extension\Svg\Exception\Html5ParseException;
17
18
/** @internal */
19
final class Html5Util
20
{
21
    // @codeCoverageIgnoreStart
22
    private function __construct()
23
    {
24
    }
25
26
    // @codeCoverageIgnoreEnd
27
28 13
    public static function loadHtml(string $html, array $options = []): \DOMDocument
29
    {
30 13
        $parser = new HTML5();
31
32
        // Load Document
33 13
        $doc = $parser->loadHTML($html, $options);
34 13
        if ($parser->hasErrors()) {
35 1
            throw new Html5Exception(null, 0, array_map(
36 1
                function ($message) use ($html) {
37 1
                    return new Html5ParseException($message, $html);
38 1
                },
39
40 1
                $parser->getErrors(),
41 1
            ));
42
        }
43
44 12
        return $doc;
45
    }
46
47 8
    public static function toHtml(\DOMDocument $doc, array $options = []): string
48
    {
49
        // Normalize final doc
50 8
        if ($options['normalize_document'] ?? true) {
51 8
            $doc->normalize();
52
        }
53
54
        // Generate output
55 8
        $html = (new HTML5())->saveHTML($doc, $options);
56
57
        // Fix EOL lines
58 8
        if (($options['normalize_eol'] ?? "\n") !== \PHP_EOL) {
59
            $html = str_replace(\PHP_EOL, $options['normalize_eol'] ?? "\n", $html); // @codeCoverageIgnore
60
        }
61
62 8
        return $html;
63
    }
64
}
65