Passed
Push — master ( b86a9b...818b3e )
by Thierry
02:35
created

TemplateView   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 9
c 0
b 0
f 0
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addNamespace() 0 3 1
A render() 0 11 2
1
<?php
2
3
/**
4
 * TemplateView.php
5
 *
6
 * A view wrapper for the template library provided by the jaxon-utils package.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\App\View;
16
17
use Jaxon\Utils\Template\TemplateEngine;
18
19
class TemplateView implements ViewInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class TemplateView
Loading history...
20
{
21
    /**
22
     * The Jaxon template engine
23
     *
24
     * @var TemplateEngine
25
     */
26
    protected $xTemplateEngine;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
27
28
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xTemplateEngine should have a doc-comment as per coding-style.
Loading history...
29
     * The class constructor
30
     */
31
    public function __construct(TemplateEngine $xTemplateEngine)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
32
    {
33
        $this->xTemplateEngine = $xTemplateEngine;
34
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
35
36
    /**
37
     * Add a namespace to this view renderer
38
     *
39
     * @param string $sNamespace    The namespace name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
40
     * @param string $sDirectory    The namespace directory
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
41
     * @param string $sExtension    The extension to append to template names
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
42
     *
43
     * @return void
44
     */
45
    public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension = '')
46
    {
47
        $this->xTemplateEngine->addNamespace($sNamespace, $sDirectory, $sExtension);
48
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
49
50
    /**
51
     * Render a view
52
     *
53
     * @param Store $store    A store populated with the view data
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
54
     *
55
     * @return string        The string representation of the view
56
     */
57
    public function render(Store $store): string
58
    {
59
        $sViewName = $store->getViewName();
60
        $sNamespace = $store->getNamespace();
61
        // In this view renderer, the namespace must always be prepended to the view name.
62
        if(substr($sViewName, 0, strlen($sNamespace) + 2) != $sNamespace . '::')
63
        {
64
            $sViewName = $sNamespace . '::' . $sViewName;
65
        }
66
        // Render the template
67
        return trim($this->xTemplateEngine->render($sViewName, $store->getViewData()), " \t\n");
68
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
69
}
70