View::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Asymptix\app;
4
5
use Asymptix\core\Tools;
6
use Asymptix\core\Route;
7
8
/**
9
 * View class.
10
 *
11
 * @category Asymptix PHP Framework
12
 * @author Dmytro Zarezenko <[email protected]>
13
 * @copyright (c) 2015 - 2016, Dmytro Zarezenko
14
 *
15
 * @git https://github.com/Asymptix/Framework
16
 * @license http://opensource.org/licenses/MIT
17
 */
18
class View {
19
20
    /**
21
     * @var Route
22
     */
23
    private $_route = null;
24
25
    /**
26
     * Path to the template.
27
     *
28
     * @var string
29
     */
30
    private $_tpl = null;
31
32
    /**
33
     * Template inside variables list.
34
     *
35
     * @var array
36
     */
37
    private $_fields = [];
38
39
    /**
40
     * Template inside messages list.
41
     *
42
     * @var array
43
     */
44
    private $_messages = [];
45
46
    /**
47
     * Template inside errors list.
48
     *
49
     * @var array
50
     */
51
    private $_errors = [];
52
53
    public function __construct($tpl) {
54
        if (!empty($tpl)) {
55
            if (is_string($tpl)) {
56
                $this->_tpl = $tpl;
57
            } elseif (Tools::isInstanceOf($tpl, new Route)) {
0 ignored issues
show
Documentation introduced by
new \Asymptix\core\Route() is of type object<Asymptix\core\Route>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
                $this->_route = $tpl;
59
                $this->_tpl = $this->_route->controller . "/" . $this->_route->controller . "_" . $this->_route->action;
60
            } else {
61
                throw new \Exception("Invalid view template");
62
            }
63
        } else {
64
            throw new \Exception("Empty view template");
65
        }
66
    }
67
68
    public function render($path = "", $suffix = "") {
69
        if (empty($this->_tpl)) {
70
            throw new \Exception("View object was not initialized with template");
71
        }
72
73
        require_once($path . $this->_tpl . $suffix);
74
    }
75
76
    public function setTpl($_tpl) {
77
        $this->_tpl = $_tpl;
78
    }
79
80
    public function getFields() {
81
        return $this->_fields;
82
    }
83
84
    public function setFields($_fields) {
85
        $this->_fields = $_fields;
86
    }
87
88
    public function getMessages() {
89
        return $this->_messages;
90
    }
91
92
    public function setMessages($_messages) {
93
        $this->_messages = $_messages;
94
    }
95
96
    public function getErrors() {
97
        return $this->_errors;
98
    }
99
100
    public function setErrors($_errors) {
101
        $this->_errors = $_errors;
102
    }
103
104
}
105