Completed
Pull Request — master (#100)
by
unknown
02:35
created

layoutRow::updateSize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
ccs 0
cts 11
cp 0
cc 3
eloc 8
nc 3
nop 0
crap 12
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
    public function __construct($startX, $startY, $elements = [], $margin = 0)
54
    {
55
        if (!is_array($elements)) {
56
            throw new \Exception('not an array');
57
        }
58
59
        $this->margin = $margin;
60
        $this->elements = $elements;
61
        $this->setPosition($startX, $startY);
62
        $this->updateSize();
63
    }
64
65
    protected function updateSize() {
66
        $sizeX = 0;
67
        $sizeY= 0;
68
        foreach ($this->elements as $idx => $element) {
69
            $sizeY += $element->getY() + $element->getHeight() + $this->margin;
70
71
            if (abs($element->getX()) + $element->getWidth() > $sizeX) {
72
                $sizeX = abs($element->getX()) + $element->getWidth();
73
            }
74
        }
75
        $this->setSize($sizeX, $sizeY);
76
    }
77
78
    public function setPosition($x, $y)
79
    {
80
        $this->startX = $x;
81
        $this->startY = $y;
82
    }
83
84
    /**
85
     * @param mixed $startX
86
     * @return
87
     */
88
    public function setX($startX)
89
    {
90
        $this->startX = $startX;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param mixed $startY
97
     * @return
98
     */
99
    public function setY($startY)
100
    {
101
        $this->startY = $startY;
102
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
    public function render(\DOMDocument $domDocument)
114
    {
115
        $frame = new Frame();
116
        $frame->setPosition($this->startX, $this->startY);
117
        $frame->addClasses($this->frameClasses);
118
119
        $startY = 0;
120
121
        foreach ($this->elements as $idx => $element) {
122
            $pos = $element->getY();
123
            $element->setY($startY + $pos);
124
            $startY -= $pos + $element->getHeight() + $this->margin;
125
            $frame->addChild($element);
126
        }
127
128
        $frame->setSize($this->getWidth(), $this->getHeight());
129
        return $frame->render($domDocument);
130
    }
131
132
    /**
133
     * Get the Script Features
134
     *
135
     * @return ScriptFeature[]
136
     */
137 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...
138
    {
139
        $features = [];
140
        foreach ($this->elements as $element) {
141
            if ($element instanceof ScriptFeatureable) {
142
                $features[] = $element->getScriptFeatures();
143
            }
144
        }
145
        return ScriptFeature::collect($features);
146
    }
147
148
    /**
149
     * @return mixed
150
     */
151
    public function getX()
152
    {
153
        return $this->startX;
154
    }
155
156
    /**
157
     * @return mixed
158
     */
159
    public function getY()
160
    {
161
        return $this->startY;
162
    }
163
164
    /**
165
     * @return float
166
     */
167
    public function getHeight()
168
    {
169
        return $this->height;
170
    }
171
172
    /**
173
     * @param float $height
174
     */
175
    public function setHeight($height)
176
    {
177
        $this->height = $height;
178
    }
179
180
    /**
181
     * @param float $width
182
     * @return layoutRow
183
     */
184
    public function setWidth($width)
185
    {
186
        $this->width = $width;
187
188
        return $this;
189
    }
190
191
    /**
192
     * @return float|int
193
     */
194
    public function getWidth()
195
    {
196
        return $this->width;
197
    }
198
199
    /**
200
     * @param Renderable $element
201
     */
202
    public function addChild(Renderable $element)
203
    {
204
        $this->elements[] = $element;
205
        $this->updateSize();
206
    }
207
208
    public function getChildren()
209
    {
210
        return $this->elements;
211
    }
212
213
214
    public function addClass($class)
215
    {
216
        $this->frameClasses[] = $class;
217
    }
218
219
    /**
220
     * Add a new child
221
     *
222
     * @api
223
     * @param Renderable $child Child Control to add
224
     * @return static
225
     * @deprecated Use addChild()
226
     * @see        Container::addChild()
227
     */
228
    public function add(Renderable $child)
229
    {
230
        // TODO: Implement add() method.
231
    }
232
233
    /**
234
     * Add new children
235
     *
236
     * @api
237
     * @param Renderable[] $children Child Controls to add
238
     * @return static
239
     */
240
    public function addChildren(array $children)
241
    {
242
        // TODO: Implement addChildren() method.
243
    }
244
245
    /**
246
     * Remove all children
247
     *
248
     * @api
249
     * @return static
250
     */
251
    public function removeAllChildren()
252
    {
253
        // TODO: Implement removeAllChildren() method.
254
    }
255
256
    /**
257
     * Remove all children
258
     *
259
     * @api
260
     * @return static
261
     * @deprecated Use removeAllChildren()
262
     * @see        Container::removeAllChildren()
263
     */
264
    public function removeChildren()
265
    {
266
        // TODO: Implement removeChildren() method.
267
    }
268
269
    /**
270
     * Get the Format
271
     *
272
     * @api
273
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
274
     * @return Format
275
     * @deprecated Use Style
276
     * @see        Style
277
     */
278
    public function getFormat($createIfEmpty = true)
279
    {
280
        // TODO: Implement getFormat() method.
281
    }
282
283
    /**
284
     * Set the Format
285
     *
286
     * @api
287
     * @param Format $format New Format
288
     * @return static
289
     * @deprecated Use Style
290
     * @see        Style
291
     */
292
    public function setFormat(Format $format = null)
293
    {
294
        // TODO: Implement setFormat() method.
295
    }
296
297
    private function setSize($sizeX, $sizeY)
298
    {
299
        $this->width = $sizeX;
300
        $this->height = $sizeY;
301
    }
302
}
303