Template::set_model()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Amarkal\UI;
4
5
/**
6
 * Templates are a combination of markup and logic. Use templates to maintain
7
 * separation of concepts.
8
 */
9
class Template
10
{
11
    /**
12
     * An associative array holding the model to be used by this controller
13
     * @var array 
14
     */
15
    protected $model;
16
    
17
    /**
18
     * @var string The path to the template file
19
     */
20
    protected $template;
21
22
    /**
23
     * Constructor
24
     * 
25
     * @param array $model
26
     * @param string $template_path
27
     */
28
    public function __construct( array $model = array(), $template_path = null ) 
29
    {
30
        $this->set_model( $model );
31
        $this->template = $template_path;
32
    }
33
    
34
    /**
35
     * Get model argument value by name.
36
     * 
37
     * @param string $name The argument's name.
38
     * 
39
     * @return mixed the argument's value.
40
     */
41
    public function __get( $name ) 
42
    {
43
        if( isset( $this->model[$name] ) )
44
        {
45
            return $this->model[$name];
46
        }
47
    }
48
    
49
    /**
50
     * Set model argument by name.
51
     * 
52
     * @param string $name The argument's name.
53
     * @param mixed $value The value to set.
54
     * 
55
     * @return mixed the settings' argument value.
56
     */
57
    public function __set( $name, $value )
58
    {
59
        $this->model[$name] = $value;
60
    }
61
62
    /**
63
     * Check if a model argument exists
64
     *
65
     * @param string $name The argument's name.
66
     * @return boolean Whether this arguments exists or not.
67
     */
68
    public function __isset( $name )
69
    {
70
        return isset($this->model[$name]);
71
    }
72
    
73
    /**
74
     * Get the current component model data.
75
     * 
76
     * @return array
77
     */
78
    public function get_model()
79
    {
80
        return $this->model;
81
    }
82
    
83
    /**
84
     * Set the model data for this component.
85
     * 
86
     * @return array
87
     */
88
    public function set_model( $model )
89
    {
90
        $this->model = $model;
91
    }
92
    
93
    /**
94
     * Get the full path to the template file.
95
     * 
96
     * @return string The full path.
97
     */
98
    public function get_template_path()
99
    {
100
        return $this->template;
101
    }
102
    
103
    /**
104
     * Render the template with the local properties.
105
     * 
106
     * @return string The rendered template.
107
     * @throws TemplateNotFoundException Thrown if the template file cannot be found.
108
     */
109
    public function render( $echo = false )
110
    {
111
        $rendered_html = '';
112
        
113
        if( file_exists( $this->get_template_path() ) ) 
114
        {
115
            ob_start();
116
            include( $this->get_template_path() );
117
            $rendered_html = ob_get_clean();
118
        } 
119
        else 
120
        {
121
            throw new \RuntimeException( "Error: cannot render HTML, template file not found at " . $this->get_template_path() );
122
        }
123
        
124
        if( !$echo )
125
        {
126
            return $rendered_html;
127
        }
128
        echo $rendered_html;
129
    }
130
}