Completed
Push — master ( 5e0a7d...23f988 )
by De Cramer
03:04
created

layoutRow::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 1
ccs 7
cts 8
cp 0.875
cc 2
eloc 7
nc 2
nop 4
crap 2.0078
1
<?php
2
3
namespace eXpansion\Framework\Gui\Layouts;
4
5
use FML\Controls\Control;
6
use FML\Controls\Frame;
7
use FML\Controls\Quad;
8
use FML\Elements\Format;
9
use FML\Script\Features\ScriptFeature;
10
use FML\Types\Container;
11
use FML\Types\Renderable;
12
use FML\Types\ScriptFeatureable;
13
14
class layoutRow implements Renderable, ScriptFeatureable, Container
15
{
16
17
    protected $frameClasses = [];
18
    /**
19
     * @var float|int
20
     */
21
    protected $height = 0;
22
    /**
23
     * @var float|int
24
     */
25
    protected $width = 0;
26
27
    /** @var Control[] */
28
    protected $elements = [];
29
30
    /**
31
     * @var float|int
32
     */
33
    protected $margin = 1;
34
35
    /**
36
     * @var float|int
37
     */
38
    protected $startX;
39
40
    /**
41
     * @var float|int
42
     */
43
    protected $startY;
44
45
    /**
46
     * layoutLine constructor.
47
     * @param float $startX
48
     * @param float $startY
49
     * @param object[] $elements
50
     * @param int $margin
51
     * @throws \Exception
52
     */
53 1
    public function __construct($startX, $startY, $elements = [], $margin = 0)
54
    {
55 1
        if (!is_array($elements)) {
56
            throw new \Exception('not an array');
57
        }
58
59 1
        $this->margin = $margin;
60 1
        $this->elements = $elements;
61 1
        $this->setPosition($startX, $startY);
62 1
        $this->updateSize();
63 1
    }
64
65 1
    protected function updateSize()
66
    {
67 1
        $sizeX = 0;
68 1
        $sizeY = 0;
69 1
        foreach ($this->elements as $idx => $element) {
70 1
            $sizeY += $element->getY() + $element->getHeight() + $this->margin;
71
72 1
            if (abs($element->getX()) + $element->getWidth() > $sizeX) {
73 1
                $sizeX = abs($element->getX()) + $element->getWidth();
74
            }
75
        }
76 1
        $this->setSize($sizeX, $sizeY);
77 1
    }
78
79 1
    public function setPosition($x, $y)
80
    {
81 1
        $this->startX = $x;
82 1
        $this->startY = $y;
83 1
    }
84
85
    /**
86
     * @param mixed $startX
87
     * @return
88
     */
89
    public function setX($startX)
90
    {
91
        $this->startX = $startX;
92
93
        return $this;
94
    }
95
96
    /**
97
     * @param mixed $startY
98
     * @return
99
     */
100
    public function setY($startY)
101
    {
102
        $this->startY = $startY;
103
        return $this;
104
    }
105
106
107
    /**
108
     * Render the XML element
109
     *
110
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
111
     * @return \DOMElement
112
     */
113 1
    public function render(\DOMDocument $domDocument)
114
    {
115 1
        $frame = new Frame();
116 1
        $frame->setPosition($this->startX, $this->startY);
117 1
        $frame->addClasses($this->frameClasses);
118
119 1
        $startY = 0;
120
121 1
        foreach ($this->elements as $idx => $element) {
122 1
            $pos = $element->getY();
0 ignored issues
show
Unused Code introduced by
$pos is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
123 1
            $element->setY($startY);
124 1
            $startY -= $element->getHeight() - $this->margin;
125 1
            $frame->addChild($element);
126
        }
127
128 1
        $frame->setSize($this->getWidth(), $this->getHeight());
129
130 1
        return $frame->render($domDocument);
131
    }
132
133
    /**
134
     * Get the Script Features
135
     *
136
     * @return ScriptFeature[]
137
     */
138 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...
139
    {
140
        $features = [];
141
        foreach ($this->elements as $element) {
142
            if ($element instanceof ScriptFeatureable) {
143
                $features[] = $element->getScriptFeatures();
144
            }
145
        }
146
147
        return ScriptFeature::collect($features);
148
    }
149
150
    /**
151
     * @return mixed
152
     */
153
    public function getX()
154
    {
155
        return $this->startX;
156
    }
157
158
    /**
159
     * @return mixed
160
     */
161
    public function getY()
162
    {
163
        return $this->startY;
164
    }
165
166
    /**
167
     * @return float
168
     */
169 1
    public function getHeight()
170
    {
171 1
        return $this->height;
172
    }
173
174
    /**
175
     * @param float $height
176
     */
177
    public function setHeight($height)
178
    {
179
        $this->height = $height;
180
    }
181
182
    /**
183
     * @param float $width
184
     * @return layoutRow
185
     */
186
    public function setWidth($width)
187
    {
188
        $this->width = $width;
189
190
        return $this;
191
    }
192
193
    /**
194
     * @return float|int
195
     */
196 1
    public function getWidth()
197
    {
198 1
        return $this->width;
199
    }
200
201
    /**
202
     * @param Renderable $element
203
     */
204 1
    public function addChild(Renderable $element)
205
    {
206 1
        $this->elements[] = $element;
207 1
        $this->updateSize();
208 1
    }
209
210
    public function getChildren()
211
    {
212
        return $this->elements;
213
    }
214
215
216 1
    public function addClass($class)
217
    {
218 1
        $this->frameClasses[] = $class;
219 1
    }
220
221
    /**
222
     * Add a new child
223
     *
224
     * @api
225
     * @param Renderable $child Child Control to add
226
     * @return static
227
     * @deprecated Use addChild()
228
     * @see        Container::addChild()
229
     */
230
    public function add(Renderable $child)
231
    {
232
        // TODO: Implement add() method.
233
    }
234
235
    /**
236
     * Add new children
237
     *
238
     * @api
239
     * @param Renderable[] $children Child Controls to add
240
     * @return static
241
     */
242
    public function addChildren(array $children)
243
    {
244
        // TODO: Implement addChildren() method.
245
    }
246
247
    /**
248
     * Remove all children
249
     *
250
     * @api
251
     * @return static
252
     */
253
    public function removeAllChildren()
254
    {
255
        // TODO: Implement removeAllChildren() method.
256
    }
257
258
    /**
259
     * Remove all children
260
     *
261
     * @api
262
     * @return static
263
     * @deprecated Use removeAllChildren()
264
     * @see        Container::removeAllChildren()
265
     */
266
    public function removeChildren()
267
    {
268
        // TODO: Implement removeChildren() method.
269
    }
270
271
    /**
272
     * Get the Format
273
     *
274
     * @api
275
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
276
     * @return Format
277
     * @deprecated Use Style
278
     * @see        Style
279
     */
280
    public function getFormat($createIfEmpty = true)
281
    {
282
        // TODO: Implement getFormat() method.
283
    }
284
285
    /**
286
     * Set the Format
287
     *
288
     * @api
289
     * @param Format $format New Format
290
     * @return static
291
     * @deprecated Use Style
292
     * @see        Style
293
     */
294
    public function setFormat(Format $format = null)
295
    {
296
        // TODO: Implement setFormat() method.
297
    }
298
299 1
    private function setSize($sizeX, $sizeY)
300
    {
301 1
        $this->width = $sizeX;
302 1
        $this->height = $sizeY;
303 1
    }
304
}
305