Completed
Push — master ( e3663d...ab0db8 )
by Alex
03:22
created

View::getTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
namespace Mezon\Application;
3
4
use Mezon\HtmlTemplate\HtmlTemplate;
5
6
/**
7
 * Class View
8
 *
9
 * @package Mezon
10
 * @subpackage View
11
 * @author Dodonov A.A.
12
 * @version v.1.0 (2019/08/06)
13
 * @copyright Copyright (c) 2019, aeon.org
14
 */
15
16
/**
17
 * Base class for all views
18
 */
19
class View extends ViewBase
20
{
21
22
    /**
23
     * View's name
24
     *
25
     * @var string
26
     */
27
    private $viewName = '';
28
29
    /**
30
     * Constructor
31
     *
32
     * @param HtmlTemplate $template
33
     *            template
34
     * @param string $viewName
35
     *            View name to be rendered
36
     */
37
    public function __construct(HtmlTemplate $template = null, string $viewName = '')
38
    {
39
        parent::__construct($template);
40
41
        $this->viewName = $viewName;
42
    }
43
44
    /**
45
     * Method renders content from view
46
     *
47
     * @param string $viewName
48
     *            View name to be rendered
49
     * @return string Generated content
50
     */
51
    public function render(string $viewName = ''): string
52
    {
53
        if ($viewName === '') {
54
            $viewName = $this->viewName;
55
        }
56
57
        if ($viewName === '') {
58
            $viewName = 'Default';
59
        }
60
61
        if (method_exists($this, 'view' . $viewName)) {
62
            return call_user_func([
63
                $this,
64
                'view' . $viewName
65
            ]);
66
        }
67
68
        throw (new \Exception('View ' . $viewName . ' was not found'));
69
    }
70
71
    /**
72
     * Method returns view name
73
     *
74
     * @return string view name
75
     */
76
    public function getViewName(): string
77
    {
78
        return $this->viewName;
79
    }
80
}
81