JsonToHtml   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.5%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 6
c 6
b 0
f 1
lcom 1
cbo 1
dl 0
loc 46
rs 10
ccs 14
cts 16
cp 0.875

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addConverter() 0 4 1
A toHtml() 0 13 2
A convert() 0 8 3
1
<?php
2
3
namespace Sioen;
4
5
use Sioen\JsonToHtml\Converter;
6
7
/**
8
 * Class JsonToHtml
9
 *
10
 * Converts a json object received from Sir Trevor to an html representation
11
 *
12
 * @version 1.1.0
13
 * @author Wouter Sioen <[email protected]>
14
 * @license http://www.opensource.org/licenses/mit-license.php MIT
15
 */
16
final class JsonToHtml
17
{
18
    /** @var array */
19
    private $converters = array();
20
21 5
    public function addConverter(Converter $converter)
22
    {
23 5
        $this->converters[] = $converter;
24 5
    }
25
26
    /**
27
     * Converts the outputted json from Sir Trevor to html
28
     *
29
     * @param  string $json
30
     * @return string
31
     */
32 5
    public function toHtml($json)
33
    {
34
        // convert the json to an associative array
35 5
        $input = json_decode($json, true);
36 5
        $html = '';
37
38
        // loop trough the data blocks
39 5
        foreach ($input['data'] as $block) {
40 5
            $html .= $this->convert(new SirTrevorBlock($block['type'], $block['data']));
41 5
        }
42
43 5
        return $html;
44
    }
45
46
47
    /**
48
     * Converts on array to an html string
49
     *
50
     * @param SirTrevorBlock $block
51
     * @return string
52
     */
53 5
    private function convert(SirTrevorBlock $block)
54
    {
55 5
        foreach ($this->converters as $converter) {
56 5
            if ($converter->matches($block->getType())) {
57 5
                return $converter->toHtml($block->getData());
58
            }
59
        }
60
    }
61
}
62