Passed
Push — master ( b8980d...d8c943 )
by Tobias
02:28
created

MustacheRender::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace WrkLst\DocxMustache;
4
5
class MustacheRender
6
{
7
    protected function Render($items, $mustache_template, $clean_tags = true)
8
    {
9
        if ($clean_tags) {
10
            $mustache_template = $this->TagCleaner($mustache_template);
11
        }
12
13
        $m = new \Mustache_Engine(array('escape' => function($value) {
14
            if (str_replace('*[[DONOTESCAPE]]*', '', $value) != $value) {
15
                            return str_replace('*[[DONOTESCAPE]]*', '', $value);
16
            }
17
            return htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
18
        }));
19
        return $m->render($mustache_template, $items);
20
    }
21
22
    protected function TagCleaner($content)
23
    {
24
        //kills all xml tags within curly mustache brackets
25
        //this is necessary, as word might produce unnecesary xml tage inbetween curly backets.
26
        return preg_replace_callback(
27
            '/{{(.*?)}}/',
28
            function($match) {
29
                return strip_tags($match[0]);
30
            },
31
            $content
32
        );
33
    }
34
}
35