Passed
Push — master ( 4e1c14...c00f79 )
by Paul
07:08
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\Contracts\TemplateContract as Contract;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
9
class Template implements Contract
10
{
11
    /**
12
     * @return \GeminiLabs\SiteReviews\Application|\GeminiLabs\SiteReviews\Addons\Addon
13
     */
14 15
    public function app()
15
    {
16 15
        return glsr();
17
    }
18
19
    /**
20
     * @param string $templatePath
21
     * @return void|string
22
     */
23 15
    public function build($templatePath, array $data = [])
24
    {
25 15
        $data = $this->normalize($data);
26 15
        $path = str_replace('templates/', '', $templatePath);
27 15
        $template = $this->app()->build($templatePath, $data);
28 15
        $template = $this->app()->filterString('build/template/'.$path, $template, $data);
29 15
        $template = $this->interpolate($template, $data, $path);
30 15
        $template = $this->app()->filterString('rendered/template', $template, $templatePath, $data);
31 15
        $template = $this->app()->filterString('rendered/template/'.$path, $template, $data);
32 15
        return $template;
33
    }
34
35
    /**
36
     * Interpolate context values into template placeholders.
37
     * @param string $template
38
     * @param string $templatePath
39
     * @return string
40
     */
41 15
    public function interpolate($template, array $data = [], $templatePath)
42
    {
43 15
        $context = $this->normalizeContext(Arr::get($data, 'context', []));
44 15
        $context = $this->app()->filterArray('interpolate/'.$templatePath, $context, $template, $data);
45 15
        return $this->interpolateContext($template, $context);
46
    }
47
48
    /**
49
     * Interpolate context values into template placeholders.
50
     * @param string $text
51
     * @return string
52
     */
53 15
    public function interpolateContext($text, array $context = [])
54
    {
55 15
        foreach ($context as $key => $value) {
56 7
            $text = strtr(
57 7
                $text,
58 7
                array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value)
59
            );
60
        }
61 15
        return trim($text);
62
    }
63
64
    /**
65
     * @param string $templatePath
66
     * @return void|string
67
     */
68
    public function render($templatePath, array $data = [])
69
    {
70
        echo $this->build($templatePath, $data);
71
    }
72
73
    /**
74
     * @return array
75
     */
76 15
    protected function normalize(array $data)
77
    {
78 15
        $arrayKeys = ['context', 'globals'];
79 15
        $data = wp_parse_args($data, array_fill_keys($arrayKeys, []));
80 15
        foreach ($arrayKeys as $key) {
81 15
            if (!is_array($data[$key])) {
82
                $data[$key] = [];
83
            }
84
        }
85 15
        return $data;
86
    }
87
88
    /**
89
     * @return array
90
     */
91 15
    protected function normalizeContext(array $context)
92
    {
93
        $context = array_filter($context, function ($value) {
94 7
            return !is_array($value) && !is_object($value);
95 15
        });
96
        return array_map(function ($value) {
97 7
            return (string) $value;
98 15
        }, $context);
99
    }
100
}
101