Test Failed
Push — master ( 6de795...4e1c14 )
by Paul
04:01
created

Template::app()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
     * @param string $templatePath
13
     * @return void|string
14 15
     */
15
    public function build($templatePath, array $data = [])
16 15
    {
17 15
        $data = $this->normalize($data);
18 15
        $path = str_replace('templates/', '', $templatePath);
19 15
        $template = $this->app()->build($templatePath, $data);
20 15
        $template = $this->app()->filterString('build/template/'.$path, $template, $data);
21 15
        $template = $this->interpolate($template, $data, $path);
22 15
        $template = $this->app()->filterString('rendered/template', $template, $templatePath, $data);
23 15
        $template = $this->app()->filterString('rendered/template/'.$path, $template, $data);
24
        return $template;
25
    }
26
27
    /**
28
     * Interpolate context values into template placeholders.
29
     * @param string $template
30
     * @param string $templatePath
31
     * @return string
32 15
     */
33
    public function interpolate($template, array $data = [], $templatePath)
34 15
    {
35 15
        $context = $this->normalizeContext(Arr::get($data, 'context', []));
36 15
        $context = $this->app()->filterArray('interpolate/'.$templatePath, $context, $template, $data);
37
        return $this->interpolateContext($template, $context);
38
    }
39
40
    /**
41
     * Interpolate context values into template placeholders.
42
     * @param string $text
43
     * @return string
44 15
     */
45
    public function interpolateContext($text, array $context = [])
46 15
    {
47 7
        foreach ($context as $key => $value) {
48 7
            $text = strtr(
49 7
                $text,
50
                array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value)
51
            );
52 15
        }
53
        return trim($text);
54
    }
55
56
    /**
57
     * @param string $templatePath
58
     * @return void|string
59
     */
60
    public function render($templatePath, array $data = [])
61
    {
62
        echo $this->build($templatePath, $data);
63
    }
64
65
    /**
66
     * @return \GeminiLabs\SiteReviews\Application|\GeminiLabs\SiteReviews\Addons\Addon
67 15
     */
68
    protected function app()
69 15
    {
70 15
        return glsr();
71 15
    }
72 15
73
    /**
74
     * @return array
75
     */
76 15
    protected function normalize(array $data)
77
    {
78
        $arrayKeys = ['context', 'globals'];
79
        $data = wp_parse_args($data, array_fill_keys($arrayKeys, []));
80
        foreach ($arrayKeys as $key) {
81
            if (!is_array($data[$key])) {
82 15
                $data[$key] = [];
83
            }
84
        }
85 7
        return $data;
86 15
    }
87
88 7
    /**
89 15
     * @return array
90
     */
91
    protected function normalizeContext(array $context)
92
    {
93
        $context = array_filter($context, function ($value) {
94
            return !is_array($value) && !is_object($value);
95
        });
96
        return array_map(function ($value) {
97
            return (string) $value;
98
        }, $context);
99
    }
100
}
101