Completed
Pull Request — master (#175)
by
unknown
05:05
created

uiInput::setWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace eXpansion\Framework\Gui\Components;
4
5
use FML\Controls\Entry;
6
use FML\Controls\Frame;
7
use FML\Controls\Quad;
8
use FML\Script\Features\ScriptFeature;
9
use FML\Script\Script;
10
use FML\Script\ScriptLabel;
11
use FML\Types\Renderable;
12
use FML\Types\ScriptFeatureable;
13
14
class uiInput extends abstractUiElement implements Renderable, ScriptFeatureable
15
{
16
17
    const TYPE_DEFAULT = "Basic";
18
    const TYPE_PASSWORD = "Password";
19
20
    /**
21
     * @var string
22
     */
23
    protected $name;
24
    /**
25
     * @var string
26
     */
27
    protected $default;
28
29
    /** @var null|string */
30
    protected $action = null;
31
32
    protected $textFormat = "Basic";
33
34
    /** @var null|string  */
35
    protected $id = null;
36
37 View Code Duplication
    public function __construct($name, $default = "", $width = 30, $textFormat = "Basic")
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...
38
    {
39
        $this->name = $name;
40
        $this->default = $default;
41
        $this->width = $width;
42
        $this->setSize($width, 4);
43
        $this->textFormat = $textFormat;
44
    }
45
46
    /**
47
     * Render the XML element
48
     *
49
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
50
     * @return \DOMElement
51
     */
52
    public function render(\DOMDocument $domDocument)
53
    {
54
        $frame = new Frame();
55
        $frame->setPosition($this->posX, $this->posY)
56
57
            ->addClasses(["uiContainer", "uiInput"]);
58
59
        $quad = new Quad();
60
        $quad->setSize($this->width * 2, $this->height * 2)
61
            ->setScale(0.5)
62
            ->setPosition($this->width / 2, -$this->height / 2)
63
            ->setStyles('Bgs1', 'BgColorContour')
64
            ->setAlign("center", "center")
65
            ->setBackgroundColor('FFFA')
66
            ->addClass("uiInput")
67
            ->setScriptEvents(true)
68
            ->setDataAttributes($this->_dataAttributes)->addClasses($this->_classes);
69
70
        $input = new Entry();
71
        $input->setSize($this->width, $this->height)
72
            ->setPosition(0, 0)
73
            ->setDefault($this->default)
74
            ->setSelectText(true)
75
            ->setAlign("left", "top")
76
            ->setAreaColor("0005")
77
            ->setAreaFocusColor('000a')
78
            ->setTextFormat($this->textFormat)
79
            ->setName($this->name)
80
            ->setTextSize(2)
81
            ->setScriptEvents(true)
82
            ->setId($this->id);
83
        if ($this->action) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->action of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
84
            $input->addDataAttribute("action", $this->action);
85
        }
86
87
        $frame->addChild($quad);
88
        $frame->addChild($input);
89
90
        return $frame->render($domDocument);
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getName()
97
    {
98
        return $this->name;
99
    }
100
101
    /**
102
     * @param string $name
103
     */
104
    public function setName($name)
105
    {
106
        $this->name = $name;
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getDefault()
115
    {
116
        return $this->default;
117
    }
118
119
    /**
120
     * @param string $default
121
     */
122
    public function setDefault($default)
123
    {
124
        $this->default = $default;
125
126
        return $this;
127
    }
128
129
    /**
130
     * @return float
131
     */
132
    public function getWidth()
133
    {
134
        return $this->width;
135
    }
136
137
    /**
138
     * @param float $width
139
     */
140
    public function setWidth($width)
141
    {
142
        $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...
143
144
        return $this;
145
    }
146
147
    public function getHeight()
148
    {
149
        return $this->height + 1;
150
    }
151
152
    /**
153
     * Prepare the given Script for rendering by adding the needed Labels, etc.
154
     *
155
     * @param Script $script Script to prepare
156
     */
157
    public function prepare(Script $script)
158
    {
159
        $script->addCustomScriptLabel(ScriptLabel::MouseClick, $this->getScriptMouseClick());
160
        if ($this->action) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->action of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
161
            $script->addCustomScriptLabel(ScriptLabel::EntrySubmit, $this->getScriptEntrySubmit());
162
        }
163
    }
164
165
    /**
166
     * Get the Script Features
167
     *
168
     * @return ScriptFeature[]
169
     */
170
    public function getScriptFeatures()
171
    {
172
        return ScriptFeature::collect($this);
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function getTextFormat()
179
    {
180
        return $this->textFormat;
181
    }
182
183
    /**
184
     * @param string $textFormat
185
     * @return $this
186
     */
187
    public function setTextFormat($textFormat)
188
    {
189
        $this->textFormat = $textFormat;
190
191
        return $this;
192
    }
193
194
    protected function getScriptMouseClick()
195
    {
196
        return <<<EOL
197
             if (Event.Control.HasClass("uiInput") ) {              
198
                 declare CMlFrame frame <=> Event.Control.Parent;
199
                 (frame.Controls[1] as CMlEntry).StartEdition();               
200
            }					
201
EOL;
202
203
    }
204
205
    protected function getScriptEntrySubmit()
206
    {
207
        return <<<EOL
208
             if (Event.Control.DataAttributeExists("action") ) {              
209
                TriggerPageAction(Event.Control.DataAttributeGet("action"));            
210
            }					
211
EOL;
212
213
    }
214
215
216
    /**
217
     * @return null|string
218
     */
219
    public function getAction()
220
    {
221
        return $this->action;
222
    }
223
224
    /**
225
     * @param null|string $action
226
     * @return $this
227
     */
228
    public function setAction($action)
229
    {
230
        $this->action = $action;
231
232
        return $this;
233
    }
234
235
    /**
236
     * @return null|string
237
     */
238
    public function getId(): string
239
    {
240
        return $this->id;
241
    }
242
243
    /**
244
     * @param null|string $id
245
     */
246
    public function setId(string $id)
247
    {
248
        $this->id = $id;
249
    }
250
}
251