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