Completed
Pull Request — master (#95)
by
unknown
03:20
created

layoutLine::addElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

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 6
cp 0
cc 1
eloc 4
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 layoutLine 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 $frameClasses = [];
15
16
    private $width = 0;
17
    private $height = 0;
18
    /** @var Control[] */
19
    private $elements = [];
20
21
    private $margin = 2;
22
    /**
23
     * @var
24
     */
25
    private $startX = 0;
26
    /**
27
     * @var
28
     */
29
    private $startY = 0;
30
31
    /**
32
     * layoutLine constructor.
33
     * @param $startX
34
     * @param $startY
35
     * @param Control[] $elements
36
     * @param $margin
37
     * @throws \Exception
38
     */
39
    public function __construct($startX, $startY, $elements = [], $margin = 0)
40
    {
41
        if (!is_array($elements)) {
42
            throw new \Exception('not an array');
43
        }
44
        $this->margin = $margin;
45
        $this->elements = $elements;
46
        $this->startX = $startX;
47
        $this->startY = $startY;
48
        foreach ($this->elements as $idx => $element) {
49
            $this->width += $element->getWidth() + $this->margin;
50
            $this->height += $element->getHeight();
51
        }
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
        $frame->addClasses($this->frameClasses);
66
67
        $startX = 0;
68
        foreach ($this->elements as $idx => $element) {
69
            $element->setX($startX);
70
            $startX += $element->getWidth() + $this->margin;
71
            $frame->addChild($element);
72
        }
73
74
        return $frame->render($domDocument);
75
    }
76
77
    /**
78
     * Get the Script Features
79
     *
80
     * @return ScriptFeature[]
81
     */
82
    public function getScriptFeatures()
83
    {
84
        $features = [];
85
        foreach ($this->elements as $element) {
86
            if ($element instanceof ScriptFeatureable) {
87
                $features[] = $element->getScriptFeatures();
88
            }
89
        }
90
91
        return ScriptFeature::collect($features);
92
    }
93
94
    /**
95
     * @param mixed $startX
96
     * @return layoutLine
97
     */
98
    public function setX($startX)
99
    {
100
        $this->startX = $startX;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param mixed $startY
107
     * @return layoutLine
108
     */
109
    public function setY($startY)
110
    {
111
        $this->startY = $startY;
112
113
        return $this;
114
    }
115
116
    /**
117
     * @return mixed
118
     */
119
    public function getX()
120
    {
121
        return $this->startX;
122
    }
123
124
    /**
125
     * @return mixed
126
     */
127
    public function getY()
128
    {
129
        return $this->startY;
130
    }
131
132
    /**
133
     * @return float
134
     */
135
    public function getWidth()
136
    {
137
        return $this->width;
138
    }
139
140
    /**
141
     * @param float $width
142
     */
143
    public function setWidth($width)
144
    {
145
        $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...
146
    }
147
148
    /**
149
     * @return float
150
     */
151
    public function getHeight()
152
    {
153
        return $this->height;
154
    }
155
156
    /**
157
     * @param float $height
158
     */
159
    public function setHeight($height)
160
    {
161
        $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...
162
    }
163
164
    public function addChild($element)
165
    {
166
        $this->elements[] = $element;
167
        $this->width += $element->getWidth() + $this->margin;
168
        $this->height += $element->getHeight();
169
    }
170
171
    public function addClass($class)
172
    {
173
        $this->frameClasses [] = $class;
174
    }
175
}
176