Renderer::__toString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Vssl\Render;
4
5
use League\Plates\Engine;
6
7
class Renderer
8
{
9
    /**
10
     * Renderer configuration.
11
     *
12
     * @var array
13
     */
14
    protected $config;
15
16
    /**
17
     * Page data to render.
18
     *
19
     * @var array
20
     */
21
    protected $data;
22
23
    /**
24
     * Cache adapter.
25
     *
26
     * @var \Journey\Cache\CacheAdapterIterface
27
     */
28
    protected $cache;
29
30
    /**
31
     * String data to output.
32
     *
33
     * @var string
34
     */
35
    protected $output;
36
37
    /**
38
     * Template engine.
39
     *
40
     * @var \League\Plates\Engine
41
     */
42
    protected $engine;
43
44
    /**
45
     * Fields that are required in order to render.
46
     *
47
     * @var array
48
     */
49
    protected $required;
50
51
    /**
52
     * Name of the current theme.
53
     *
54
     * @var string
55
     */
56
    protected $theme;
57
58
    /**
59
     * Initialize our new renderer.
60
     */
61 10
    public function __construct(array $config, array $data)
62
    {
63 10
        $this->config = $config;
64 10
        $this->required = array_filter($config['required_fields']);
65 10
        $this->setData($data);
66 10
        $this->engine = new Engine(__DIR__ . "/../templates");
67 10
        $this->registerFunctions();
68 10
        if (isset($config['templates'])) {
69 1
            $this->registerTheme('default', $config['templates']);
70 1
        }
71 10
    }
72
73
    /**
74
     * Register custom theme functions particular function.
75
     *
76
     * @return $this
77
     */
78 10
    public function registerFunctions()
79
    {
80 10
        $this->engine->registerFunction('wrapperClasses', [$this, 'wrapperClasses']);
81 10
        $this->engine->registerFunction('image', [$this, 'image']);
82 10
        $this->engine->registerFunction('inline', [$this, 'inline']);
83 10
    }
84
85
    /**
86
     * Add a single directory to the template engine.
87
     *
88
     * @param  string $theme
89
     * @param  string $directory directory of templates
90
     * @return $this
91
     */
92 4
    public function registerTheme($theme, $directory)
93
    {
94 4
        if (!is_dir($directory)) {
95 1
            throw new RendererException('The specified theme directory does not exist: ' . $directory);
96
        }
97 3
        $this->theme = $theme;
98 3
        $this->engine->addFolder($theme, $directory, true);
99 3
        return $this;
100
    }
101
102
    /**
103
     * Returns the template rendering engine.
104
     *
105
     * @return \League\Plates\Engine
106
     */
107 1
    public function getEngine()
108
    {
109 1
        return $this->engine;
110
    }
111
112
    /**
113
     * Get the page data that will be rendered.
114
     *
115
     * @return array
116
     */
117 3
    public function getData()
118
    {
119 3
        return $this->data;
120
    }
121
122
    /**
123
     * Replace the page data
124
     *
125
     * @param array $data
126
     */
127 10
    public function setData(array $data)
128
    {
129 10
        $this->validateData($data);
130 10
        $this->data = $data;
131 10
        return $this;
132
    }
133
134
    /**
135
     * Validate that the page data is complete enough to render.
136
     *
137
     * Note: Throws an exception if the page data is incomplete.
138
     *
139
     * @param  array  $data array
140
     * @return boolean
141
     */
142 10
    public function validateData(array $data)
143
    {
144 10
        if (!count(array_diff_key(array_flip($this->required), $data))) {
145 10
            return true;
146
        }
147 1
        throw new RendererException('Invalid or incomplete page data');
148
    }
149
150
    /**
151
     * Generate wrapper classes for stripes
152
     *
153
     * @return string
154
     */
155 4
    public function wrapperClasses($string)
156
    {
157 4
        $type = str_replace("stripe-", "", $string);
158 4
        return 'vssl-stripe vssl-stripe--' . $type;
159
    }
160
161
    /**
162
     * Returns the image url on the vssl server.
163
     *
164
     * @param  string $name      hash.extension
165
     * @param  string $style     image style name
166
     * @return string
167
     */
168 2
    public function image($name, $style = false)
169
    {
170 2
        return ltrim($this->config['base_uri'], '/') . "/images" . ($style ? '/' . $style : '') . "/" . $name;
171
    }
172
173
    /**
174
     * Strip most tags from output stored by the inline editor.
175
     *
176
     * @param  string $str           Output
177
     * @param  string $allowed_tags  Permitted HTML tags
178
     * @return string
179
     */
180 2
    public function inline($str, $allowed_tags = '<a><b><strong><i><em>')
181
    {
182 2
        return strip_tags($str, $allowed_tags);
183
    }
184
185
    /**
186
     * Process data before its output. This is the last chance to make changes
187
     * to data before being passed to the actual template files.
188
     *
189
     * @return array
190
     */
191 4
    public function processData($data)
192
    {
193 4
        $data['themePrefix'] = $this->theme ? $this->theme . "::" : "";
194 4
        if (isset($data['stripes'])) {
195 4
            $data['stripes'] = array_filter($data['stripes'], function ($stripe) use ($data) {
196 4
                return $this->engine->exists($data['themePrefix'] . 'stripes/' . $stripe['type']);
197 4
            });
198 4
        }
199 4
        return $data;
200
    }
201
202
    /**
203
     * Render the current data.
204
     *
205
     * @return string
206
     */
207 4
    public function render()
208
    {
209 4
        return $this->engine->render('page', $this->processData($this->data));
210
    }
211
212
    /**
213
     * Return the rendered page.
214
     *
215
     * @return string
216
     */
217 4
    public function __toString()
218
    {
219
        try {
220 4
            return $this->render();
221 1
        } catch (\Exception $e) {
222 1
            return "Error rendering page: " . $e->getMessage();
223
        }
224
    }
225
}
226