Completed
Pull Request — master (#95)
by
unknown
02:41
created

layoutRow::setY()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 6
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 5
cp 0
cc 1
eloc 3
nc 1
nop 1
crap 2
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\Container;
9
use FML\Types\Renderable;
10
use FML\Types\ScriptFeatureable;
11
12 View Code Duplication
class layoutRow implements Renderable, ScriptFeatureable
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    private $height = 0;
15
    private $width = 0;
16
17
    /** @var Control[] */
18
    private $elements = [];
19
20
    private $margin = 1;
21
    /**
22
     * @var
23
     */
24
    private $startX;
25
    /**
26
     * @var
27
     */
28
    private $startY;
29
30
    /**
31
     * layoutLine constructor.
32
     * @param $startX
33
     * @param $startY
34
     * @param Control[] $elements
35
     * @param int $margin
36
     * @throws \Exception
37
     */
38
    public function __construct($startX, $startY, $elements = [], $margin = 0)
39
    {
40
        if (!is_array($elements)) {
41
            throw new \Exception('not an array');
42
        }
43
44
        $this->margin = $margin;
45
        $this->elements = $elements;
46
        $this->startX = $startX;
47
        $this->startY = $startY;
48
49
        foreach ($this->elements as $idx => $element) {
50
            $this->width += $element->getWidth();
51
            $this->height += $element->getHeight() + $this->margin;
52
        }
53
    }
54
55
    /**
56
     * Render the XML element
57
     *
58
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
59
     * @return \DOMElement
60
     */
61
    public function render(\DOMDocument $domDocument)
62
    {
63
        $frame = new Frame();
64
        $frame->setPosition($this->startX, $this->startY);
65
66
        $startY = 0;
67
        foreach ($this->elements as $idx => $element) {
68
            $element->setY($startY);
69
            $startY -= $element->getHeight() + $this->margin;
70
            $frame->addChild($element);
71
        }
72
73
        return $frame->render($domDocument);
74
    }
75
76
    /**
77
     * Get the Script Features
78
     *
79
     * @return ScriptFeature[]
80
     */
81
    public function getScriptFeatures()
82
    {
83
        $features = [];
84
        foreach ($this->elements as $element) {
85
            if ($element instanceof ScriptFeatureable) {
86
                $features[] = $element->getScriptFeatures();
87
            }
88
        }
89
90
        return ScriptFeature::collect($features);
91
    }
92
93
    /**
94
     * @param mixed $startX
95
     * @return static
96
     */
97
    public function setX($startX)
98
    {
99
        $this->startX = $startX;
100
101
        return $this;
102
    }
103
104
    /**
105
     * @param mixed $startY
106
     * @return static
107
     */
108
    public function setY($startY)
109
    {
110
        $this->startY = $startY;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return mixed
117
     */
118
    public function getX()
119
    {
120
        return $this->startX;
121
    }
122
123
    /**
124
     * @return mixed
125
     */
126
    public function getY()
127
    {
128
        return $this->startY;
129
    }
130
131
    /**
132
     * @return float
133
     */
134
    public function getHeight()
135
    {
136
        return $this->height;
137
    }
138
139
    /**
140
     * @param float $height
141
     */
142
    public function setHeight($height)
143
    {
144
        $this->height = $height;
0 ignored issues
show
Documentation Bug introduced by
The property $height was declared of type integer, but $height is of type double. 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...
145
    }
146
147
    /**
148
     * @param float $width
149
     * @return layoutRow
150
     */
151
    public function setWidth($width)
152
    {
153
        $this->width = $width;
0 ignored issues
show
Documentation Bug introduced by
The property $width was declared of type integer, but $width is of type double. 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...
154
155
        return $this;
156
    }
157
158
    /**
159
     * @return float|int
160
     */
161
    public function getWidth()
162
    {
163
        return $this->width;
164
    }
165
166
    public function addElement($element)
167
    {
168
        $this->elements[] = $element;
169
        $this->width += $element->getWidth();
170
        $this->height += $element->getHeight() + $this->margin;
171
    }
172
173
}
174