1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Amarkal\UI; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Defines an abstract UI component |
7
|
|
|
*/ |
8
|
|
|
abstract class AbstractComponent |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* An associative array holding the model to be used by this controller |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor |
18
|
|
|
* |
19
|
|
|
* @param array $model |
20
|
|
|
*/ |
21
|
|
|
public function __construct( array $model = array() ) |
22
|
|
|
{ |
23
|
|
|
// Check that the required parameters are provided. |
24
|
|
|
foreach( $this->required_parameters() as $key ) |
25
|
|
|
{ |
26
|
|
|
if ( !isset($model[$key]) ) |
27
|
|
|
{ |
28
|
|
|
throw new \RuntimeException('The required parameter "'.$key.'" was not provided for '.get_called_class()); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->model = array_merge( $this->default_model(), $model ); |
33
|
|
|
|
34
|
|
|
$this->on_created(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get model parameter value by name. |
39
|
|
|
* |
40
|
|
|
* @param string $name The parameter's name. |
41
|
|
|
* |
42
|
|
|
* @return mixed the parameter's value. |
43
|
|
|
*/ |
44
|
|
|
public function __get( $name ) |
45
|
|
|
{ |
46
|
|
|
if( isset( $this->model[$name] ) ) |
47
|
|
|
{ |
48
|
|
|
return $this->model[$name]; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set model parameter by name. |
54
|
|
|
* |
55
|
|
|
* @param string $name The parameter's name. |
56
|
|
|
* @param mixed $value The value to set. |
57
|
|
|
* |
58
|
|
|
* @return mixed the settings' parameter value. |
59
|
|
|
*/ |
60
|
|
|
public function __set( $name, $value ) |
61
|
|
|
{ |
62
|
|
|
$this->model[$name] = $value; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Render the template with the local properties. |
67
|
|
|
* |
68
|
|
|
* @return string The rendered template. |
69
|
|
|
* @throws TemplateNotFoundException Thrown if the template file can |
70
|
|
|
* not found. |
71
|
|
|
*/ |
72
|
|
|
public function render( $echo = false ){ |
73
|
|
|
|
74
|
|
|
$rendered_html = ''; |
75
|
|
|
|
76
|
|
|
if( file_exists( $this->get_script_path() ) ) |
77
|
|
|
{ |
78
|
|
|
ob_start(); |
79
|
|
|
include( $this->get_script_path() ); |
80
|
|
|
$rendered_html = ob_get_clean(); |
81
|
|
|
} |
82
|
|
|
else |
83
|
|
|
{ |
84
|
|
|
throw new \RuntimeException( "Error: cannot render HTML, template file not found at " . $this->get_script_path() ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if( !$echo ) |
88
|
|
|
{ |
89
|
|
|
return $rendered_html; |
90
|
|
|
} |
91
|
|
|
echo $rendered_html; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The default model to use when none is provided to the constructor. |
96
|
|
|
* This method should be overriden by child class to define the default |
97
|
|
|
* model. |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function default_model() |
102
|
|
|
{ |
103
|
|
|
return array(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the current component model data. |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function get_model() |
112
|
|
|
{ |
113
|
|
|
return $this->model; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* The list of required model parameters. |
118
|
|
|
* This method should be overriden by child class to specify required model |
119
|
|
|
* parameters. |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function required_parameters() |
124
|
|
|
{ |
125
|
|
|
return array(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* A hook that is called once the component has been created. |
130
|
|
|
*/ |
131
|
|
|
protected function on_created() {} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the full path to the template file. |
135
|
|
|
* |
136
|
|
|
* @return string The full path. |
137
|
|
|
*/ |
138
|
|
|
abstract function get_script_path(); |
|
|
|
|
139
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.