1
|
|
|
<?php |
2
|
|
|
/** @formatter:off |
3
|
|
|
* ****************************************************************** |
4
|
|
|
* Created by Marko Kungla on Jun 25, 2016 - 7:55:29 PM |
5
|
|
|
* Contact [email protected] |
6
|
|
|
* @copyright 2016 Marko Kungla - https://github.com/mkungla |
7
|
|
|
* @license The MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* @category AframeVR |
10
|
|
|
* @package aframe-php |
11
|
|
|
* |
12
|
|
|
* Lang PHP (php version >= 7) |
13
|
|
|
* Encoding UTF-8 |
14
|
|
|
* File ComponentAbstract.php |
15
|
|
|
* Code format PSR-2 and 12 |
16
|
|
|
* @link https://github.com/mkungla/aframe-php |
17
|
|
|
^ @issues https://github.com/mkungla/aframe-php/issues |
18
|
|
|
* ******************************************************************** |
19
|
|
|
* Contributors: |
20
|
|
|
* @author Marko Kungla <[email protected]> |
21
|
|
|
* ******************************************************************** |
22
|
|
|
* Comments: |
23
|
|
|
* @formatter:on */ |
24
|
|
|
namespace AframeVR\Core\Helpers; |
25
|
|
|
|
26
|
|
|
use \AframeVR\Interfaces\ComponentInterface; |
27
|
|
|
use \AframeVR\Core\Exceptions\{ |
28
|
|
|
InvalidComponentMethodException, |
29
|
|
|
InvalidComponentArgumentException |
30
|
|
|
}; |
31
|
|
|
use \DOMAttr; |
32
|
|
|
|
33
|
|
|
abstract class ComponentAbstract implements ComponentInterface |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Array of dom attributes |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $dom_attributes = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Array of component scripts |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $component_scripts = array(); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Name of DOM attribute |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $dom_attribute_name; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Loaded Component Method object |
59
|
|
|
* |
60
|
|
|
* @var object |
61
|
|
|
*/ |
62
|
|
|
protected $methodProvider; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Component Constructor |
66
|
|
|
*/ |
67
|
58 |
|
public function __construct() |
68
|
|
|
{ |
69
|
58 |
|
$this->initializeComponent(); |
70
|
58 |
|
$this->setMethodProvider(); |
71
|
58 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Call passes all calls to no existing methods to self::methodProvider |
75
|
|
|
* |
76
|
|
|
* @param string $method |
77
|
|
|
* @param array $args |
78
|
|
|
* @throws InvalidComponentMethodException |
79
|
|
|
*/ |
80
|
58 |
|
public function __call(string $method, $args) |
81
|
|
|
{ |
82
|
58 |
|
if (is_object($this->methodProvider) && method_exists($this->methodProvider, $method)) { |
83
|
58 |
|
array_unshift($args, 0); |
84
|
58 |
|
$args[0] = &$this->dom_attributes; |
85
|
|
|
|
86
|
58 |
|
return call_user_func_array(array( |
87
|
58 |
|
$this->methodProvider, |
88
|
58 |
|
(string) $method |
89
|
|
|
), $args); |
90
|
|
|
} else { |
91
|
1 |
|
$class = is_object($this->methodProvider) ? get_class($this->methodProvider) : get_called_class(); |
92
|
1 |
|
throw new InvalidComponentMethodException($method, $class); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return DOM attribute contents |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
4 |
|
public function getDomAttributeString(): string |
102
|
|
|
{ |
103
|
4 |
|
$attrs = $this->getDOMAttributesArray(); |
|
|
|
|
104
|
4 |
|
$attr_format = implode(': %s; ', array_keys($attrs)) . ': %s;'; |
105
|
|
|
|
106
|
4 |
|
return vsprintf($attr_format, array_values($attrs)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set class providing component methods |
111
|
|
|
* |
112
|
|
|
* @param string $mp |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
58 |
|
public function setMethodProvider(string $mp = 'DefaultMethods') |
116
|
|
|
{ |
117
|
58 |
|
$mp_class = substr(get_called_class(), 0, strrpos(get_called_class(), '\\')) . '\Methods\\' . $mp; |
118
|
|
|
|
119
|
58 |
|
$this->methodProvider = new $mp_class(); |
120
|
58 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Does component have DOM Atributes |
124
|
|
|
* |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
2 |
|
public function hasDOMAttributes(): bool |
130
|
|
|
{ |
131
|
2 |
|
return ! empty($this->dom_attributes); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get component DOM Atributes array |
136
|
|
|
* |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
20 |
|
public function getDOMAttributesArray(): array |
142
|
|
|
{ |
143
|
20 |
|
return $this->dom_attributes; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get Component scripts |
148
|
|
|
* |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
2 |
|
public function getComponentScripts(): array |
154
|
|
|
{ |
155
|
2 |
|
return (array) $this->component_scripts; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Add component scripts |
160
|
|
|
* |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
* |
163
|
|
|
*/ |
164
|
1 |
|
public function addComponentScripts(string $vendor_component, string $script_name) |
165
|
|
|
{ |
166
|
1 |
|
$this->component_scripts[$vendor_component] = $script_name; |
167
|
1 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get DOMAttr for the entity |
171
|
|
|
* |
172
|
|
|
* @return DOMAttr |
173
|
|
|
*/ |
174
|
2 |
|
public function getDOMAttr(): DOMAttr |
175
|
|
|
{ |
176
|
2 |
|
return new DOMAttr($this->getDomAttributeName(), $this->getDomAttributeString()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get Dom attribute name |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
2 |
|
public function getDomAttributeName(): string |
185
|
|
|
{ |
186
|
2 |
|
return $this->dom_attribute_name; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Set Dom Attribute name |
191
|
|
|
* |
192
|
|
|
* @param string $dom_attribute_name |
193
|
|
|
* @return void |
194
|
|
|
*/ |
195
|
58 |
|
public function setDomAttribute(string $dom_attribute_name) |
196
|
|
|
{ |
197
|
58 |
|
$this->dom_attribute_name = $dom_attribute_name; |
198
|
58 |
|
} |
199
|
|
|
} |
200
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.