1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Amarkal\UI; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Implements a Composite UI component. |
7
|
|
|
*/ |
8
|
|
|
class Component_composite |
9
|
|
|
extends AbstractComponent |
|
|
|
|
10
|
|
|
implements ValueComponentInterface, |
|
|
|
|
11
|
|
|
DisableableComponentInterface, |
|
|
|
|
12
|
|
|
FilterableComponentInterface, |
|
|
|
|
13
|
|
|
ValidatableComponentInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The list of child components. |
17
|
|
|
* |
18
|
|
|
* @var UI\AbstractComponent[] |
19
|
|
|
*/ |
20
|
|
|
private $components; |
21
|
|
|
|
22
|
|
|
public $component_type = 'composite'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The __set magic method is overridden here to apply value & name changes to |
26
|
|
|
* child components. |
27
|
|
|
*/ |
28
|
|
|
public function __set( $name, $value ) |
29
|
|
|
{ |
30
|
|
|
parent::__set($name, $value); |
31
|
|
|
|
32
|
|
|
if( 'value' === $name ) |
33
|
|
|
{ |
34
|
|
|
$this->set_value($value); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if( 'name' === $name ) |
38
|
|
|
{ |
39
|
|
|
$this->set_name($value); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Apply the value to each of the child components. |
45
|
|
|
* |
46
|
|
|
* @param array $value |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function set_value( array $value ) |
50
|
|
|
{ |
51
|
|
|
foreach($value as $n => $v) |
52
|
|
|
{ |
53
|
|
|
$component = $this->get_component($n); |
54
|
|
|
$component->value = $v; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Apply the new name to each of the child components. |
60
|
|
|
* |
61
|
|
|
* @param string $name |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function set_name( $name ) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
foreach($this->components as $c) |
67
|
|
|
{ |
68
|
|
|
$c->name_template = str_replace('{{parent_name}}', $this->get_name(), $c->composite_name_template); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function default_model() |
76
|
|
|
{ |
77
|
|
|
return array( |
78
|
|
|
'name' => '', |
79
|
|
|
'parent_name' => '', |
80
|
|
|
'id' => '', |
81
|
|
|
'disabled' => false, |
82
|
|
|
'template' => null, |
83
|
|
|
'components' => array(), |
84
|
|
|
'filter' => array($this, 'filter'), |
85
|
|
|
'validation' => array($this, 'validation'), |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function required_arguments() |
93
|
|
|
{ |
94
|
|
|
return array('name','components','template'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function get_template_path() |
101
|
|
|
{ |
102
|
|
|
return __DIR__.'/template.phtml'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Parse the template by converting the name tokens into UI components. |
107
|
|
|
* |
108
|
|
|
* @return string The parsed template |
109
|
|
|
*/ |
110
|
|
|
public function parse_template() |
111
|
|
|
{ |
112
|
|
|
return preg_replace_callback('/\{\{([a-zA-Z\d-_]+)\}\}/', function($a){ |
113
|
|
|
$component = $this->get_component($a[1]); |
114
|
|
|
return $component->render(); |
115
|
|
|
}, $this->model['template']); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* If this is the root composite component, this will return the component's |
120
|
|
|
* name. If this is a child composite component, this will return the |
121
|
|
|
* component's name as a key in the parent's array, i.e 'parent_name[child_name]' |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function get_name() |
126
|
|
|
{ |
127
|
|
|
if('' !== $this->parent_name) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
return "{$this->parent_name}[{$this->name}]"; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
return $this->name; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function filter($v) |
135
|
|
|
{ |
136
|
|
View Code Duplication |
foreach($this->components as $component) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
if($component instanceof FilterableComponentInterface && |
139
|
|
|
\is_callable($component->filter)) |
140
|
|
|
{ |
141
|
|
|
$n = $component->name; |
142
|
|
|
$v[$n] = $component->filter($v[$n]); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $v; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function validation($v,&$e) |
150
|
|
|
{ |
151
|
|
View Code Duplication |
foreach($this->components as $component) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
if($component instanceof ValidatableComponentInterface && |
154
|
|
|
\is_callable($component->validation)) |
155
|
|
|
{ |
156
|
|
|
$n = $component->name; |
157
|
|
|
if(!$component->validation($v[$n],$e)) |
158
|
|
|
{ |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Instantiate child UI components when created. |
168
|
|
|
*/ |
169
|
|
|
protected function on_created() |
170
|
|
|
{ |
171
|
|
|
foreach( $this->model['components'] as $args ) |
172
|
|
|
{ |
173
|
|
|
$this->components[$args['name']] = $this->create_component($args); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* |
179
|
|
|
* @param type $args |
180
|
|
|
* @return type |
181
|
|
|
*/ |
182
|
|
|
private function create_component( $args ) |
183
|
|
|
{ |
184
|
|
|
$type = $args['type']; |
185
|
|
|
|
186
|
|
|
if('composite' === $type) |
187
|
|
|
{ |
188
|
|
|
$args['parent_name'] = $this->get_name(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$c = \Amarkal\UI\ComponentFactory::create( $type, $args ); |
192
|
|
|
|
193
|
|
|
// Apply the composite name template |
194
|
|
|
$c->name_template = str_replace('{{parent_name}}', $this->get_name(), $c->composite_name_template); |
195
|
|
|
|
196
|
|
|
return $c; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get a child component by name. |
201
|
|
|
* |
202
|
|
|
* @param string $name |
203
|
|
|
* @return UI\AbstractComponent |
204
|
|
|
* @throws \RuntimeException If there's no child component corresponding to the given name |
205
|
|
|
*/ |
206
|
|
|
private function get_component( $name ) |
207
|
|
|
{ |
208
|
|
|
if(!array_key_exists($name, $this->components)) |
209
|
|
|
{ |
210
|
|
|
throw new \RuntimeException("Composite sub-component not found with name $name"); |
211
|
|
|
} |
212
|
|
|
return $this->components[$name]; |
213
|
|
|
} |
214
|
|
|
} |