Completed
Push — SOLID ( a3f21e...9bf102 )
by Wouter
01:58
created

JsonToHtml::convert()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 21

Duplication

Lines 20
Ratio 80 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 20
loc 25
rs 8.439
cc 6
eloc 21
nc 6
nop 2
1
<?php
2
3
namespace Sioen;
4
5
use Sioen\Types\BlockquoteConverter;
6
use Sioen\Types\HeadingConverter;
7
use Sioen\Types\IframeConverter;
8
use Sioen\Types\ImageConverter;
9
use Sioen\Types\ListConverter;
10
use Sioen\Types\BaseConverter;
11
12
/**
13
 * Class JsonToHtml
14
 *
15
 * Converts a json object received from Sir Trevor to an html representation
16
 *
17
 * @version 1.1.0
18
 * @author Wouter Sioen <[email protected]>
19
 * @license http://www.opensource.org/licenses/mit-license.php MIT
20
 */
21
class JsonToHtml
22
{
23
    /**
24
     * Converts the outputted json from Sir Trevor to html
25
     *
26
     * @param  string $json
27
     * @return string
28
     */
29
    public function toHtml($json)
30
    {
31
        // convert the json to an associative array
32
        $input = json_decode($json, true);
33
        $html = '';
34
35
        // loop trough the data blocks
36
        foreach ($input['data'] as $block) {
37
            $html .= $this->convert($block['type'], $block['data']);
38
        }
39
40
        return $html;
41
    }
42
43
    private function convert($type, array $data)
44
    {
45 View Code Duplication
        switch ($type) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
            case 'heading':
47
                $converter = new HeadingConverter();
48
                break;
49
            case 'list':
50
                $converter = new ListConverter();
51
                break;
52
            case 'quote':
53
                $converter = new BlockquoteConverter();
54
                break;
55
            case 'video':
56
                $converter = new IframeConverter();
57
                break;
58
            case 'image':
59
                $converter = new ImageConverter();
60
                break;
61
            default:
62
                $converter = new BaseConverter();
63
                break;
64
        }
65
66
        return $converter->toHtml($data);
67
    }
68
}
69