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

Template::normalizeContext()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
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