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

Widget::getXml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
ccs 6
cts 6
cp 1
cc 1
eloc 6
nc 1
nop 0
crap 1
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui;
4
5
use eXpansion\Framework\Core\Helpers\Translations;
6
use eXpansion\Framework\Core\Model\UserGroups\Group;
7
use FML\Controls\Frame;
8
use FML\Controls\Label;
9
use FML\Elements\Dico;
10
use FML\Elements\Format;
11
use FML\Elements\SimpleScript;
12
use FML\Script\Features\ToggleInterface;
13
use FML\Script\Script;
14
use FML\Script\ScriptInclude;
15
use FML\Types\Container;
16
use FML\Types\Renderable;
17
18
class Widget extends Manialink implements Container
19
{
20
21
    /** @var  ManiaScript */
22
    protected $scriptData;
23
24
    /** @var Translations */
25
    protected $translationHelper;
26
27
    /** @var \FML\ManiaLink */
28
    protected $manialink;
29
30
    /** @var Dico */
31
    protected $dictionary;
32
33
    /** @var Label */
34
    protected $closeButton;
35
36
    /** @var Frame */
37
    protected $contentFrame;
38
39
    /** @var Frame */
40
    protected $windowFrame;
41
42 3
    public function __construct(
43
        Group $group,
44
        Translations $translationHelper,
45
        $name,
46
        $sizeX,
47
        $sizeY,
48
        $posX = null,
49
        $posY = null
50
    ) {
51 3
        parent::__construct($group, $name, $sizeX, $sizeY, $posX, $posY);
52
53 3
        $this->translationHelper = $translationHelper;
54
55
        // Manialink containing everything
56 3
        $this->manialink = new \FML\ManiaLink();
57 3
        $this->manialink->setId($this->getId())
58 3
            ->setName($name)
59 3
            ->setVersion(\FML\ManiaLink::VERSION_3);
60
61 3
        $this->dictionary = new Dico();
62 3
        $this->manialink->setDico($this->dictionary);
63
64 3
        $windowFrame = new Frame('Window');
65 3
        $windowFrame->setPosition($posX, $posY);
66 3
        $this->manialink->addChild($windowFrame);
67
68
        // Frame to handle the content of the window.
69 3
        $this->contentFrame = new Frame();
70 3
        $this->contentFrame->setPosition(0, 0);
71 3
        $this->contentFrame->setSize($sizeX, $sizeY);
72 3
        $windowFrame->addChild($this->contentFrame);
73
74 3
        $this->windowFrame = $windowFrame;
75 3
    }
76
77
    /**
78
     * @return \FML\ManiaLink
79
     */
80
    public function getFmlManialink()
81
    {
82
        return $this->manialink;
83
    }
84
85
    /**
86
     * sets scripts data
87
     *
88
     * @param ManiaScript $script
89
     */
90
    public function setScriptData(ManiaScript $script)
91
    {
92
        $this->scriptData = $script->__toString();
0 ignored issues
show
Documentation Bug introduced by
It seems like $script->__toString() of type string is incompatible with the declared type object<eXpansion\Framewo...\Model\Gui\ManiaScript> of property $scriptData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98 1
    public function getXml()
99
    {
100 1
        $this->addDictionaryInformation();
101
102 1
        $toggleInterfaceF9 = new ToggleInterface("F9");
103 1
        $this->manialink->getScript()
104 1
            ->addFeature($toggleInterfaceF9);
105
106 1
        return $this->manialink->__toString();
107
    }
108
109
    /**
110
     * Add translations to dictionary.
111
     */
112 1
    protected function addDictionaryInformation()
113
    {
114 1
        $translations = array();
115 1
        $this->getDictionaryInformation($this->manialink, $translations);
116 1
        $this->dictionary->removeAllEntries();
117
118 1
        foreach ($translations as $msgId => $messages) {
119 1
            foreach ($messages as $message) {
120 1
                $this->dictionary->setEntry($message['Lang'], $msgId, htmlspecialchars($message['Text']));
121
            }
122
        }
123 1
    }
124
125
    /**
126
     * Recursive search all dome tree in order to find all translatable labels.
127
     *
128
     * @param Container|\FML\ManiaLink $control
129
     * @param $translations
130
     */
131 1
    protected function getDictionaryInformation($control, &$translations)
132
    {
133 1
        foreach ($control->getChildren() as $child) {
134 1
            if ($child instanceof Label && $child->getTranslate()) {
135 1
                $textId = 'exp_'.md5($child->getTextId());
136 1
                $translations[$textId] = $this->translationHelper->getTranslations($child->getTextId(), []);
137
138
                // Replaces with text id that can be used in the xml.
139 1
                $child->setTextId($textId);
140
            } else {
141 1
                if ($child instanceof Frame) {
142 1
                    $this->getDictionaryInformation($child, $translations);
143
                }
144
            }
145
        }
146 1
    }
147
148
    /**
149
     * Get the children
150
     *
151
     * @api
152
     * @return Renderable[]
153
     */
154 1
    public function getChildren()
155
    {
156 1
        return $this->contentFrame->getChildren();
157
    }
158
159
    /**
160
     * Add a new child
161
     *
162
     * @api
163
     *
164
     * @param Renderable $child Child Control to add
165
     *
166
     * @return static
167
     */
168 1
    public function addChild(Renderable $child)
169
    {
170 1
        $this->contentFrame->addChild($child);
171
172 1
        return $this;
173
    }
174
175
    /**
176
     * Add a new child
177
     *
178
     * @api
179
     *
180
     * @param Renderable $child Child Control to add
181
     *
182
     * @return static
183
     * @deprecated Use addChild()
184
     * @see        Container::addChild()
185
     */
186 1
    public function add(Renderable $child)
187
    {
188 1
        $this->contentFrame->addChild($child);
189
190 1
        return $this;
191
    }
192
193
    /**
194
     * Add new children
195
     *
196
     * @api
197
     *
198
     * @param Renderable[] $children Child Controls to add
199
     *
200
     * @return static
201
     */
202 1
    public function addChildren(array $children)
203
    {
204 1
        $this->contentFrame->addChildren($children);
205
206 1
        return $this;
207
    }
208
209
    /**
210
     * Remove all children
211
     *
212
     * @api
213
     * @return static
214
     */
215 1
    public function removeAllChildren()
216
    {
217 1
        $this->contentFrame->removeAllChildren();
218
219 1
        return $this;
220
    }
221
222
    /**
223
     * Remove all children
224
     *
225
     * @api
226
     * @return static
227
     * @deprecated Use removeAllChildren()
228
     * @see        Container::removeAllChildren()
229
     */
230 1
    public function removeChildren()
231
    {
232 1
        $this->contentFrame->removeAllChildren();
233
234 1
        return $this;
235
    }
236
237
    /**
238
     * Get the Format
239
     *
240
     * @api
241
     *
242
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
243
     *
244
     * @return Format
245
     * @deprecated Use Style
246
     * @see        Style
247
     */
248 1
    public function getFormat($createIfEmpty = true)
249
    {
250 1
        return $this->contentFrame->getFormat($createIfEmpty);
0 ignored issues
show
Deprecated Code introduced by
The method FML\Controls\Frame::getFormat() has been deprecated with message: Use Style

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
251
    }
252
253
    /**
254
     * Set the Format
255
     *
256
     * @api
257
     *
258
     * @param Format $format New Format
259
     *
260
     * @return static
261
     * @deprecated Use Style
262
     * @see        Style
263
     *
264
     */
265 1
    public function setFormat(Format $format = null)
266
    {
267 1
        $this->contentFrame->setFormat($format);
0 ignored issues
show
Deprecated Code introduced by
The method FML\Controls\Frame::setFormat() has been deprecated with message: Use Style

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
268
269 1
        return $this;
270
    }
271
272
    /**
273
     * @return Frame
274
     */
275
    public function getContentFrame()
276
    {
277
        return $this->contentFrame;
278
    }
279
}
280