|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Framework\Gui\Layouts; |
|
4
|
|
|
|
|
5
|
|
|
use FML\Controls\Control; |
|
6
|
|
|
use FML\Controls\Frame; |
|
7
|
|
|
use FML\Script\Features\ScriptFeature; |
|
8
|
|
|
use FML\Types\Renderable; |
|
9
|
|
|
use FML\Types\ScriptFeatureable; |
|
10
|
|
|
|
|
11
|
|
View Code Duplication |
class layoutRow implements Renderable, ScriptFeatureable |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
private $frameClasses = []; |
|
14
|
|
|
/** |
|
15
|
|
|
* @var float|int |
|
16
|
|
|
*/ |
|
17
|
|
|
private $height = 0; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var float|int |
|
20
|
|
|
*/ |
|
21
|
|
|
private $width = 0; |
|
22
|
|
|
|
|
23
|
|
|
/** @var Control[] */ |
|
24
|
|
|
private $elements = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var float|int |
|
28
|
|
|
*/ |
|
29
|
|
|
private $margin = 1; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var float|int |
|
33
|
|
|
*/ |
|
34
|
|
|
private $startX; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var float|int |
|
38
|
|
|
*/ |
|
39
|
|
|
private $startY; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* layoutLine constructor. |
|
43
|
|
|
* @param float $startX |
|
44
|
|
|
* @param float $startY |
|
45
|
|
|
* @param object[] $elements |
|
46
|
|
|
* @param int $margin |
|
47
|
|
|
* @throws \Exception |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function __construct($startX, $startY, $elements = [], $margin = 0) |
|
50
|
|
|
{ |
|
51
|
1 |
|
if (!is_array($elements)) { |
|
52
|
|
|
throw new \Exception('not an array'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
$this->margin = $margin; |
|
56
|
1 |
|
$this->elements = $elements; |
|
57
|
1 |
|
$this->startX = $startX; |
|
58
|
1 |
|
$this->startY = $startY; |
|
59
|
|
|
|
|
60
|
1 |
|
foreach ($this->elements as $idx => $element) { |
|
61
|
|
|
$this->width += $element->getWidth(); |
|
62
|
|
|
$this->height += $element->getHeight() + $this->margin; |
|
63
|
|
|
} |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Render the XML element |
|
68
|
|
|
* |
|
69
|
|
|
* @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered |
|
70
|
|
|
* @return \DOMElement |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function render(\DOMDocument $domDocument) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$frame = new Frame(); |
|
75
|
1 |
|
$frame->setPosition($this->startX, $this->startY); |
|
76
|
1 |
|
$frame->addClasses($this->frameClasses); |
|
77
|
|
|
|
|
78
|
1 |
|
$startY = 0; |
|
79
|
1 |
|
foreach ($this->elements as $idx => $element) { |
|
80
|
1 |
|
$element->setY($startY); |
|
81
|
1 |
|
$startY -= $element->getHeight() + $this->margin; |
|
82
|
1 |
|
$frame->addChild($element); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
return $frame->render($domDocument); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get the Script Features |
|
90
|
|
|
* |
|
91
|
|
|
* @return ScriptFeature[] |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getScriptFeatures() |
|
94
|
|
|
{ |
|
95
|
|
|
$features = []; |
|
96
|
|
|
foreach ($this->elements as $element) { |
|
97
|
|
|
if ($element instanceof ScriptFeatureable) { |
|
98
|
|
|
$features[] = $element->getScriptFeatures(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return ScriptFeature::collect($features); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param mixed $startX |
|
107
|
|
|
* @return static |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setX($startX) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->startX = $startX; |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param mixed $startY |
|
118
|
|
|
* @return static |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setY($startY) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->startY = $startY; |
|
123
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return mixed |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getX() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->startX; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getY() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->startY; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return float |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getHeight() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->height; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param float $height |
|
153
|
|
|
*/ |
|
154
|
|
|
public function setHeight($height) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->height = $height; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param float $width |
|
161
|
|
|
* @return layoutRow |
|
162
|
|
|
*/ |
|
163
|
|
|
public function setWidth($width) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->width = $width; |
|
166
|
|
|
|
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return float|int |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getWidth() |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->width; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param object $element |
|
180
|
|
|
*/ |
|
181
|
1 |
|
public function addChild($element) |
|
182
|
|
|
{ |
|
183
|
1 |
|
$this->elements[] = $element; |
|
184
|
1 |
|
$this->width += $element->getWidth(); |
|
185
|
1 |
|
$this->height += $element->getHeight() + $this->margin; |
|
186
|
1 |
|
} |
|
187
|
|
|
|
|
188
|
1 |
|
public function addClass($class) |
|
189
|
|
|
{ |
|
190
|
1 |
|
$this->frameClasses[] = $class; |
|
191
|
1 |
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.