|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: php_r |
|
5
|
|
|
* Date: 16.2.2018 |
|
6
|
|
|
* Time: 21.08 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace eXpansion\Framework\Gui\Builders; |
|
10
|
|
|
|
|
11
|
|
|
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface; |
|
12
|
|
|
use eXpansion\Framework\Core\Plugins\Gui\ActionFactory; |
|
13
|
|
|
use eXpansion\Framework\Gui\Components\Button; |
|
14
|
|
|
use eXpansion\Framework\Gui\Components\Label as uiLAbel; |
|
15
|
|
|
use eXpansion\Framework\Gui\Ui\Factory; |
|
16
|
|
|
use FML\Controls\Frame; |
|
17
|
|
|
use FML\Controls\Label; |
|
18
|
|
|
use FML\Controls\Quad; |
|
19
|
|
|
|
|
20
|
|
|
class uiBuilder |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Factory |
|
24
|
|
|
*/ |
|
25
|
|
|
private $uiFactory; |
|
26
|
|
|
private $pluginClass; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var ActionFactory |
|
29
|
|
|
*/ |
|
30
|
|
|
private $actionFactory; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var ManialinkInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $manialink; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
Factory $uiFactory, |
|
38
|
|
|
ActionFactory $actionFactory, |
|
39
|
|
|
ManialinkInterface $manialink, |
|
40
|
|
|
$pluginClass |
|
41
|
|
|
) { |
|
42
|
|
|
$this->uiFactory = $uiFactory; |
|
43
|
|
|
$this->pluginClass = $pluginClass; |
|
44
|
|
|
$this->actionFactory = $actionFactory; |
|
45
|
|
|
$this->manialink = $manialink; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $xmlString |
|
50
|
|
|
* @return Frame |
|
51
|
|
|
*/ |
|
52
|
|
|
public function build($xmlString) |
|
53
|
|
|
{ |
|
54
|
|
|
$xml = new \DOMDocument(1, "utf-8"); |
|
55
|
|
|
$xml->loadXML($xmlString); |
|
56
|
|
|
|
|
57
|
|
|
return $this->parse($xml->documentElement); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param \DOMNode $node |
|
62
|
|
|
* @param null $result |
|
63
|
|
|
* @return Frame |
|
64
|
|
|
*/ |
|
65
|
|
|
private function parse($node, $result = null) |
|
66
|
|
|
{ |
|
67
|
|
|
if ($result === null) { |
|
68
|
|
|
$result = new Frame(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($node->nodeType == XML_TEXT_NODE) { |
|
|
|
|
|
|
72
|
|
|
/** @var DOMText $node */ |
|
73
|
|
|
// $result->setText($node->nodeValue); |
|
|
|
|
|
|
74
|
|
|
} else { |
|
75
|
|
|
|
|
76
|
|
|
if ($node->hasChildNodes()) { |
|
77
|
|
|
$children = $node->childNodes; |
|
78
|
|
|
for ($i = 0; $i < $children->length; $i++) { |
|
79
|
|
|
$child = $children->item($i); |
|
80
|
|
|
if ($child->nodeName != '#text') { |
|
81
|
|
|
$aux = $this->castTagToMethod($child->nodeName, $child); |
|
82
|
|
|
$result->addChild($this->parse($child, $aux)); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
if ($result instanceof Label) { |
|
89
|
|
|
$result->setText($node->nodeValue); |
|
90
|
|
|
} |
|
91
|
|
|
if ($result instanceof uiLabel) { |
|
92
|
|
|
$result->setText($node->nodeValue); |
|
93
|
|
|
} |
|
94
|
|
|
if ($result instanceof Button) { |
|
95
|
|
|
$result->setText($node->nodeValue); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($node->hasAttributes()) { |
|
99
|
|
|
$attributes = $node->attributes; |
|
100
|
|
|
if (!is_null($attributes)) { |
|
101
|
|
|
foreach ($attributes as $index => $attr) { |
|
102
|
|
|
// $result->setAttr($attr->name, $attr->value); |
|
|
|
|
|
|
103
|
|
|
$this->parseAttributes($result, $attr, $node); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $result; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function castTagToMethod($tag, \DOMNode $node) |
|
113
|
|
|
{ |
|
114
|
|
|
if (substr($tag, 0, 2) == "ui") { |
|
115
|
|
|
return $this->uiFactory->{"create".ucfirst(substr($tag, 2))}(); |
|
116
|
|
|
} else { |
|
117
|
|
|
switch ($tag) { |
|
118
|
|
|
case "label": |
|
119
|
|
|
return Label::create(); |
|
120
|
|
|
case "quad": |
|
121
|
|
|
return Quad::create(); |
|
122
|
|
|
case "frame": |
|
123
|
|
|
return Frame::create(); |
|
124
|
|
|
case "include": |
|
125
|
|
|
return $this->build($node->nodeValue); |
|
126
|
|
|
break; |
|
|
|
|
|
|
127
|
|
|
default: |
|
128
|
|
|
return Frame::create(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
private function parseAttributes(&$result, $attr, \DOMNode $node) |
|
135
|
|
|
{ |
|
136
|
|
|
switch ($attr->name) { |
|
137
|
|
View Code Duplication |
case "size": |
|
|
|
|
|
|
138
|
|
|
list($x, $y) = explode(" ", $attr->value); |
|
139
|
|
|
$result->setSize($x, $y); |
|
140
|
|
|
|
|
141
|
|
|
return; |
|
142
|
|
View Code Duplication |
case "pos": |
|
|
|
|
|
|
143
|
|
|
list($x, $y) = explode(" ", $attr->value); |
|
144
|
|
|
$result->setPosition($x, $y); |
|
145
|
|
|
|
|
146
|
|
|
return; |
|
147
|
|
|
case "actionCallback": |
|
148
|
|
|
$param = null; |
|
149
|
|
|
foreach ($node->attributes as $index => $attr2) { |
|
150
|
|
|
if ($attr2->name == 'actionParam') { |
|
151
|
|
|
$param = ["id" => $attr2->value]; |
|
152
|
|
|
break; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
$action = $this->actionFactory->createManialinkAction($this->manialink, |
|
156
|
|
|
[$this->pluginClass, $attr->value], $param, false); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
$result->setAction($action); |
|
159
|
|
|
|
|
160
|
|
|
return; |
|
161
|
|
|
default: |
|
162
|
|
|
if ($attr->name == "actionParam") { |
|
163
|
|
|
return; |
|
164
|
|
|
} |
|
165
|
|
|
$method = "set".ucfirst($attr->name); |
|
166
|
|
|
$result->{$method}($attr->value); |
|
167
|
|
|
|
|
168
|
|
|
return; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
} |
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.