Completed
Push — SOLID ( 1ed1a1...b29565 )
by Wouter
03:45
created

JsonToHtml::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 1
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
class JsonToHtml
17
{
18
    /** @var array */
19
    private $converters;
20
21 1
    public function addConverter(Converter $converter)
22
    {
23 1
        $this->converters[] = $converter;
24 1
    }
25
26
    /**
27
     * Converts the outputted json from Sir Trevor to html
28
     *
29
     * @param  string $json
30
     * @return string
31
     */
32 1
    public function toHtml($json)
33
    {
34
        // convert the json to an associative array
35 1
        $input = json_decode($json, true);
36 1
        $html = '';
37
38
        // loop trough the data blocks
39 1
        foreach ($input['data'] as $block) {
40 1
            $html .= $this->convert($block['type'], $block['data']);
41 1
        }
42
43 1
        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 1
    private function convert($type, array $data)
55
    {
56 1
        foreach ($this->converters as $converter) {
57 1
            if ($converter->matches($type)) {
58 1
                return $converter->toHtml($data);
59
            }
60 1
        }
61
    }
62
}
63