LayoutRow::addClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace eXpansion\Framework\Gui\Layouts;
4
5
use FML\Controls\Control;
6
use FML\Controls\Frame;
7
use FML\Elements\Format;
8
use FML\Script\Features\ScriptFeature;
9
use FML\Types\Container;
10
use FML\Types\Renderable;
11
use FML\Types\ScriptFeatureable;
12
13
class LayoutRow implements Renderable, ScriptFeatureable, Container
14
{
15
16
    protected $frameClasses = [];
17
    /**
18
     * @var float|int
19
     */
20
    protected $height = 0;
21
    /**
22
     * @var float|int
23
     */
24
    protected $width = 0;
25
26
    /** @var Control[] */
27
    protected $elements = [];
28
29
    /**
30
     * @var float|int
31
     */
32
    protected $margin = 0;
33
34
    /**
35
     * @var float|int
36
     */
37
    protected $posX;
38
39
    /**
40
     * @var float|int
41
     */
42
    protected $posy;
43
44
    /**
45
     * @var float|int
46
     */
47
    protected $startY = 0;
48
49
    /** @var float */
50
    protected $additionalHeight;
51
52
    protected $hAlign = "left";
53
    protected $vAlign = "top";
54
55
56
    /**
57
     * layoutLine constructor.
58
     *
59
     * @param float    $posX
60
     * @param float    $posY
61
     * @param object[] $elements
62
     * @param int      $margin
63
     *
64
     * @throws \Exception
65
     */
66
    public function __construct($posX, $posY, $elements = [], $margin = 1, $additionalHeight = 0)
67
    {
68
        if (!is_array($elements)) {
69
            throw new \Exception('not an array');
70
        }
71
72
        $this->margin = $margin;
73
        $this->elements = $elements;
74
        $this->additionalHeight = $additionalHeight;
0 ignored issues
show
Documentation Bug introduced by
The property $additionalHeight was declared of type double, but $additionalHeight is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
75
        $this->setPosition($posX, $posY);
76
        $this->updateSize();
77
    }
78
79
    /**
80
     * Update the size of the layout according to all the elements in it.
81
     */
82
    protected function updateSize()
83
    {
84
        $sizeY = abs($this->startY);
85
        $sizeX = 0;
86 View Code Duplication
        foreach ($this->elements as $idx => $element) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
87
            $sizeY += $element->getHeight() + $this->margin;
88
89
            if (abs($element->getX()) + $element->getWidth() > $sizeX) {
90
                $sizeX = abs($element->getX()) + $element->getWidth();
91
            }
92
        }
93
94
        $this->setSize($sizeX, $sizeY);
95
    }
96
97
    /**
98
     * Set position.
99
     *
100
     * @param double $x
101
     * @param double $y
102
     * @return LayoutRow
103
     */
104
    public function setPosition($x, $y)
105
    {
106
        $this->posX = $x;
107
        $this->posy = $y;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @param mixed $posX
114
     *
115
     * @return LayoutRow
116
     */
117
    public function setX($posX)
118
    {
119
        $this->posX = $posX;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param mixed $posY
126
     *
127
     * @return LayoutRow
128
     */
129
    public function setY($posY)
130
    {
131
        $this->posy = $posY;
132
133
        return $this;
134
    }
135
136
    /**
137
     * @param float|int $startY
138
     */
139
    public function setStartY($startY)
140
    {
141
        $this->startY = $startY;
142
    }
143
144
    /**
145
     * Render the XML element
146
     *
147
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
148
     * @return \DOMElement
149
     */
150
    public function render(\DOMDocument $domDocument)
151
    {
152
        $frame = new Frame();
153
        $frame->setAlign($this->hAlign, $this->vAlign);
154
        $frame->setPosition($this->posX, $this->posy);
155
        $frame->addClasses($this->frameClasses);
156
        $frame->setSize($this->getWidth(), $this->getHeight()+4);
157
        
158
        $startY = $this->startY;
159
160
        foreach ($this->elements as $idx => $element) {
161
            $element->setY($startY);
162
            $startY -= $element->getHeight() + $this->margin;
163
            $frame->addChild($element);
164
        }
165
166
        return $frame->render($domDocument);
167
    }
168
169
    /**
170
     * Get the Script Features
171
     *
172
     * @return ScriptFeature[]
173
     */
174 View Code Duplication
    public function getScriptFeatures()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
175
    {
176
        $features = [];
177
        foreach ($this->elements as $element) {
178
            if ($element instanceof ScriptFeatureable) {
179
                $features[] = $element->getScriptFeatures();
180
            }
181
        }
182
183
        return ScriptFeature::collect($features);
184
    }
185
186
    /**
187
     * @return mixed
188
     */
189
    public function getX()
190
    {
191
        return $this->posX;
192
    }
193
194
    /**
195
     * @return mixed
196
     */
197
    public function getY()
198
    {
199
        return $this->posy;
200
    }
201
202
    /**
203
     * @return float
204
     */
205
    public function getHeight()
206
    {
207
        return $this->height;
208
    }
209
210
    /**
211
     * @param float $height
212
     */
213
    public function setHeight($height)
214
    {
215
        $this->height = $height;
216
    }
217
218
    /**
219
     * @param float $width
220
     * @return LayoutRow
221
     */
222
    public function setWidth($width)
223
    {
224
        $this->width = $width;
225
226
        return $this;
227
    }
228
229
    /**
230
     * @return float|int
231
     */
232
    public function getWidth()
233
    {
234
        return $this->width;
235
    }
236
237
    /**
238
     * @param Renderable $element
239
     */
240
    public function addChild(Renderable $element)
241
    {
242
        $this->elements[] = $element;
243
        $this->updateSize();
244
    }
245
246
    public function getChildren()
247
    {
248
        return $this->elements;
249
    }
250
251
252
    /**
253
     * @param string $class
254
     * @return LayoutRow
255
     */
256
    public function addClass($class)
257
    {
258
        $this->frameClasses[] = $class;
259
260
        return $this;
261
    }
262
263
    /**
264
     * Add a new child
265
     *
266
     * @api
267
     * @param Renderable $child Child Control to add
268
     * @deprecated Use addChild()
269
     * @see        Container::addChild()
270
     */
271
    public function add(Renderable $child)
272
    {
273
274
    }
275
276
    /**
277
     * Add new children
278
     *
279
     * @api
280
     *
281
     * @param Renderable[] $children Child Controls to add
282
     *
283
     */
284
    public function addChildren(array $children)
285
    {
286
        foreach ($children as $child) {
287
            $this->addChild($child);
288
        }
289
    }
290
291
    /**
292
     * Remove all children
293
     *
294
     * @api
295
     *
296
     */
297
    public function removeAllChildren()
298
    {
299
        $this->width = 0;
300
        $this->height = 0;
301
        $this->elements = [];
302
303
        return $this;
304
    }
305
306
    /**
307
     * Remove all children
308
     *
309
     * @api
310
     * @deprecated Use removeAllChildren()
311
     * @see        Container::removeAllChildren()
312
     */
313
    public function removeChildren()
314
    {
315
316
    }
317
318
    /**
319
     * Get the Format
320
     *
321
     * @api
322
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
323
     * @deprecated Use Style
324
     * @see        Style
325
     */
326
    public function getFormat($createIfEmpty = true)
327
    {
328
329
    }
330
331
    /**
332
     * Set the Format
333
     *
334
     * @api
335
     * @param Format $format New Format
336
     * @deprecated Use Style
337
     * @see        Style
338
     */
339
    public function setFormat(Format $format = null)
340
    {
341
342
    }
343
344
    /**
345
     * @param float $sizeX
346
     * @param float $sizeY
347
     * @return LayoutRow
348
     */
349
    private function setSize($sizeX, $sizeY)
350
    {
351
        $this->width = $sizeX;
352
        $this->height = $sizeY;
353
354
        return $this;
355
356
    }
357
358
    /**
359
     * @param string $hAling
360
     * @param string $vAlign
361
     * @return LayoutRow
362
     */
363
    public function setAlign($hAling = "left", $vAlign = "top")
364
    {
365
        $this->hAlign = $hAling;
366
        $this->vAlign = $vAlign;
367
368
        return $this;
369
    }
370
371
    public function getHorizontalAlign()
372
    {
373
        return $this->hAlign;
374
    }
375
376
    public function getVerticalAlign()
377
    {
378
        return $this->vAlign;
379
    }
380
}
381