Completed
Push — SOLID ( 92d068...e6143d )
by Wouter
02:59
created

JsonToHtml   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.86%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 6
c 5
b 0
f 0
lcom 1
cbo 0
dl 0
loc 47
ccs 13
cts 14
cp 0.9286
rs 10

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($block['type'], $block['data']);
41
        }
42
43 5
        return $html;
44
    }
45
46
47
    /**
48
     * Converts on array to an html string
49
     *
50
     * @param string $type
51
     * @param array $data
52
     * @return string
53
     */
54 5
    private function convert($type, array $data)
55
    {
56 5
        foreach ($this->converters as $converter) {
57 5
            if ($converter->matches($type)) {
58 5
                return $converter->toHtml($data);
59
            }
60
        }
61
    }
62
}
63