Passed
Push — master ( b04009...b8980d )
by Tobias
02:33
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
    public function __construct($items, $mustache_template, $clean_tags = true)
8
    {
9
        return $this->Render($items, $mustache_template, $clean_tags);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
10
    }
11
12
    protected function Render($items, $mustache_template, $clean_tags = true)
13
    {
14
        if ($clean_tags) {
15
            $mustache_template = $this->TagCleaner($mustache_template);
16
        }
17
18
        $m = new \Mustache_Engine(array('escape' => function($value) {
19
            if (str_replace('*[[DONOTESCAPE]]*', '', $value) != $value) {
20
                            return str_replace('*[[DONOTESCAPE]]*', '', $value);
21
            }
22
            return htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
23
        }));
24
        return $m->render($mustache_template, $items);
25
    }
26
27
    protected function TagCleaner($content)
28
    {
29
        //kills all xml tags within curly mustache brackets
30
        //this is necessary, as word might produce unnecesary xml tage inbetween curly backets.
31
        return preg_replace_callback(
32
            '/{{(.*?)}}/',
33
            function($match) {
34
                return strip_tags($match[0]);
35
            },
36
            $content
37
        );
38
    }
39
}
40