Completed
Push — SOLID ( ba508c...d6070b )
by Wouter
01:49
created

HtmlToJson::convert()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
crap 3.0416
1
<?php
2
3
namespace Sioen;
4
5
use Sioen\HtmlToJson\BlockquoteConverter;
6
use Sioen\HtmlToJson\HeadingConverter;
7
use Sioen\HtmlToJson\IframeConverter;
8
use Sioen\HtmlToJson\ImageConverter;
9
use Sioen\HtmlToJson\ListConverter;
10
use Sioen\HtmlToJson\ParagraphConverter;
11
use Sioen\HtmlToJson\BaseConverter;
12
13
/**
14
 * Class HtmlToJson
15
 *
16
 * Converts html to a json object that can be understood by Sir Trevor
17
 *
18
 * @version 1.1.0
19
 * @author Wouter Sioen <[email protected]>
20
 * @license http://www.opensource.org/licenses/mit-license.php MIT
21
 */
22
class HtmlToJson
23
{
24
    /** @var array */
25
    private $converters;
26
27 1
    public function __construct()
28
    {
29 1
        $this->converters[] = new HeadingConverter();
30 1
        $this->converters[] = new ListConverter();
31 1
        $this->converters[] = new BlockquoteConverter();
32 1
        $this->converters[] = new IframeConverter();
33 1
        $this->converters[] = new ImageConverter();
34 1
        $this->converters[] = new BaseConverter();
35 1
    }
36
37
    /**
38
     * Converts html to the json Sir Trevor requires
39
     *
40
     * @param  string $html
41
     * @return string The json string
42
     */
43 1
    public function toJson($html)
44
    {
45
        // Strip white space between tags to prevent creation of empty #text nodes
46 1
        $html = preg_replace('~>\s+<~', '><', $html);
47 1
        $document = new \DOMDocument();
48
49
        // Load UTF-8 HTML hack (from http://bit.ly/pVDyCt)
50 1
        $document->loadHTML('<?xml encoding="UTF-8">' . $html);
51 1
        $document->encoding = 'UTF-8';
52
53
        // fetch the body of the document. All html is stored in there
54 1
        $body = $document->getElementsByTagName("body")->item(0);
55
56 1
        $data = array();
57
58
        // loop trough the child nodes and convert them
59 1
        if ($body) {
60 1
            foreach ($body->childNodes as $node) {
61 1
                $data[] = $this->convert($node);
62 1
            }
63 1
        }
64
65 1
        return json_encode(array('data' => $data));
66
    }
67
68
    /**
69
     * Converts one node to json
70
     *
71
     * @param \DOMElement $node
72
     * @return array
73
     */
74 1
    private function convert(\DOMElement $node)
75
    {
76 1
        foreach ($this->converters as $converter) {
77 1
            if ($converter->matches($node)) {
78 1
                return $converter->toJson($node);
79
            }
80 1
        }
81
    }
82
}
83