|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Amarkal\UI; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Defines an abstract UI component |
|
7
|
|
|
*/ |
|
8
|
|
|
abstract class AbstractComponent |
|
9
|
|
|
extends Template |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* This template is used to generate the name for this component. the token |
|
13
|
|
|
* {{name}} will be replaced by the component's name during runtime. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
public $name_template = '{{name}}'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* This template is used to generate the name for this component when it is |
|
21
|
|
|
* used as a child component in a composite component. The token |
|
22
|
|
|
* {{parent_name}} will be replaced with the name of the parent (composite) |
|
23
|
|
|
* component. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public $composite_name_template = '{{parent_name}}[{{name}}]'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor |
|
31
|
|
|
* |
|
32
|
|
|
* @param array $model |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct( array $model = array() ) |
|
35
|
|
|
{ |
|
36
|
|
|
parent::__construct($model); |
|
37
|
|
|
$this->on_created(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The default model to use when none is provided to the constructor. |
|
42
|
|
|
* This method should be overriden by child class to define the default |
|
43
|
|
|
* model. |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function default_model() |
|
48
|
|
|
{ |
|
49
|
|
|
return array(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The list of required model arguments. |
|
54
|
|
|
* This method should be overriden by child class to specify required model |
|
55
|
|
|
* arguments. |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function required_arguments() |
|
60
|
|
|
{ |
|
61
|
|
|
return array(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set the model data for this component. |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
public function set_model( $model ) |
|
70
|
|
|
{ |
|
71
|
|
|
// Check that the required arguments are provided. |
|
72
|
|
|
foreach( $this->required_arguments() as $key ) |
|
73
|
|
|
{ |
|
74
|
|
|
if ( !isset($model[$key]) ) |
|
75
|
|
|
{ |
|
76
|
|
|
throw new \RuntimeException('The required argument "'.$key.'" was not provided for '.get_called_class()); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// Assign the name of the component as the id if no id was specified |
|
81
|
|
|
if( !isset($model['id']) ) |
|
82
|
|
|
{ |
|
83
|
|
|
$model['id'] = $model['name']; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->model = array_merge( $this->default_model(), $model ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the name for this component by parsing the name template. |
|
91
|
|
|
* |
|
92
|
|
|
* @return type |
|
93
|
|
|
*/ |
|
94
|
|
|
public function get_name() |
|
95
|
|
|
{ |
|
96
|
|
|
return \str_replace('{{name}}', $this->name, $this->name_template); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function component_attributes() |
|
100
|
|
|
{ |
|
101
|
|
|
return sprintf( |
|
102
|
|
|
'class="amarkal-ui-component amarkal-ui-component-%s" amarkal-component-name="%s"', |
|
103
|
|
|
$this->component_type, |
|
|
|
|
|
|
104
|
|
|
$this->name |
|
|
|
|
|
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* A hook that is called once the component has been created. |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function on_created() {} |
|
112
|
|
|
} |