Passed
Push — master ( 64f340...ccb079 )
by Paul
08:17 queued 03:57
created

Template   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 88.57%

Importance

Changes 5
Bugs 0 Features 4
Metric Value
eloc 30
c 5
b 0
f 4
dl 0
loc 83
ccs 31
cts 35
cp 0.8857
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 10 1
A interpolate() 0 5 1
A render() 0 3 1
A normalize() 0 11 3
A interpolateContext() 0 9 2
A normalizeContext() 0 8 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
use GeminiLabs\SiteReviews\Helpers\Str;
7
8
class Template
9
{
10
    /**
11
     * @param string $templatePath
12
     * @return void|string
13
     */
14 7
    public function build($templatePath, array $data = [])
15
    {
16 7
        $data = $this->normalize($data);
17 7
        $path = Str::removePrefix('templates/', $templatePath);
18 7
        $template = glsr()->build($templatePath, $data);
19 7
        $template = apply_filters('site-reviews/build/template/'.$path, $template, $data);
20 7
        $template = $this->interpolate($template, $data, $path);
21 7
        $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data);
22 7
        $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data);
23 7
        return $template;
24
    }
25
26
    /**
27
     * Interpolate context values into template placeholders.
28
     * @param string $template
29
     * @param string $templatePath
30
     * @return string
31
     */
32 7
    public function interpolate($template, array $data = [], $templatePath)
33
    {
34 7
        $context = $this->normalizeContext(Arr::get($data, 'context', []));
35 7
        $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template, $data);
36 7
        return $this->interpolateContext($template, $context);
37
    }
38
39
    /**
40
     * Interpolate context values into template placeholders.
41
     * @param string $text
42
     * @return string
43
     */
44 7
    public function interpolateContext($text, array $context = [])
45
    {
46 7
        foreach ($context as $key => $value) {
47 1
            $text = strtr(
48 1
                $text,
49 1
                array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value)
50
            );
51
        }
52 7
        return trim($text);
53
    }
54
55
    /**
56
     * @param string $templatePath
57
     * @return void|string
58
     */
59
    public function render($templatePath, array $data = [])
60
    {
61
        echo $this->build($templatePath, $data);
62
    }
63
64
    /**
65
     * @return array
66
     */
67 7
    protected function normalize(array $data)
68
    {
69 7
        $arrayKeys = ['context', 'globals'];
70 7
        $data = wp_parse_args($data, array_fill_keys($arrayKeys, []));
71 7
        foreach ($arrayKeys as $key) {
72 7
            if (is_array($data[$key])) {
73 7
                continue;
74
            }
75
            $data[$key] = [];
76
        }
77 7
        return $data;
78
    }
79
80
    /**
81
     * @return array
82
     */
83 7
    protected function normalizeContext(array $context)
84
    {
85
        $context = array_filter($context, function ($value) {
86 1
            return !is_array($value) && !is_object($value);
87 7
        });
88
        return array_map(function ($value) {
89 1
            return (string) $value;
90 7
        }, $context);
91
    }
92
}
93