Passed
Push — master ( b22650...e2a2f8 )
by Robson
03:41
created

Smodal::setSmodaltemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace RobsonSuzin\Smodal;
4
5
/**
6
 * Class Smodal
7
 * @package Source\Support
8
 */
9
class Smodal
10
{
11
12
    /**
13
     * @var
14
     */
15
    private $smodalname;
16
17
    /**
18
     * @var
19
     */
20
    private $sdebug;
21
    /**
22
     * @var
23
     */
24
    private $smodaltemplate;
25
    /**
26
     * @var
27
     */
28
    private $smodalhtml;
29
    /**
30
     * @var
31
     */
32
    private $smodalwidth;
33
    /**
34
     * @var
35
     */
36
    private $smodalprint;
37
    /**
38
     * @var
39
     */
40
    private $smodaleffect;
41
    /**
42
     * @var
43
     */
44
    private $smodaldata;
45
    /**
46
     * @var
47
     */
48
    private $sadddata;
49
    /**
50
     * @var
51
     */
52
    private $sremovedata;
53
    /**
54
     * @var
55
     */
56
    private $saddattr;
57
    /**
58
     * @var
59
     */
60
    private $sremoveattr;
61
    /**
62
     * @var
63
     */
64
    private $saddhtml;
65
    /**
66
     * @var
67
     */
68
    private $saddclass;
69
    /**
70
     * @var
71
     */
72
    private $sremoveclass;
73
    /**
74
     * @var
75
     */
76
    private $sremoveelement;
77
    /**
78
     * @var
79
     */
80
    private $saddcss;
81
82
    /**
83
     * @var
84
     */
85
    private $resultstring;
86
87
    /**
88
     * @var
89
     */
90
    private $methods;
91
92
    /**
93
     * Smodal constructor.
94
     */
95
    public function __construct(string $smodalname = 'app_modal_dialog')
96
    {
97
        $this->setSmodalname($smodalname);
98
99
        $this->methods = [
100
            'smodalname',
101
            'smodaltemplate',
102
            'smodalhtml',
103
            'smodalwidth',
104
            'smodalprint',
105
            'smodaleffect',
106
            'smodaldata',
107
            'sadddata',
108
            'sremovedata',
109
            'saddattr',
110
            'sremoveattr',
111
            'saddhtml',
112
            'saddclass',
113
            'sremoveclass',
114
            'sremoveelement',
115
            'saddcss'
116
        ];
117
    }
118
119
    /**
120
     * @param string $smodalname
121
     * @return Smodal
122
     */
123
    public function setSmodalname(string $smodalname): Smodal
124
    {
125
        $this->smodalname = $smodalname;
126
        return $this;
127
    }
128
129
    /**
130
     * @param bool $sdebug
131
     * @return Smodal
132
     */
133
    public function setSdebug(bool $sdebug): Smodal
134
    {
135
        $this->sdebug = $sdebug;
136
        return $this;
137
    }
138
139
    /**
140
     * @param string $smodaltemplate
141
     * @return Smodal
142
     */
143
    public function setSmodaltemplate(string $smodaltemplate): Smodal
144
    {
145
        $this->smodaltemplate = $smodaltemplate;
146
        return $this;
147
    }
148
149
    /**
150
     * @param string $smodaltemplate
151
     * @return Smodal
152
     * Função para ser retrocompativel
153
     */
154
    public function setSmodaltype(string $smodaltemplate): Smodal
155
    {
156
        $this->smodaltemplate = $smodaltemplate;
157
        return $this;
158
    }
159
160
    /**
161
     * @param string $smodalhtml
162
     * @return Smodal
163
     */
164
    public function setSmodalhtml(string $smodalhtml): Smodal
165
    {
166
        $this->smodalhtml = $smodalhtml;
167
        return $this;
168
    }
169
170
    /**
171
     * @param int $smodalwidth
172
     * @return Smodal
173
     */
174
    public function setSmodalwidth(int $smodalwidth): Smodal
175
    {
176
        $this->smodalwidth = $smodalwidth;
177
        return $this;
178
    }
179
180
181
    /**
182
     * @param string $smodalprint
183
     * @return Smodal
184
     */
185
    public function setSmodalprint(string $smodalprint): Smodal
186
    {
187
        $this->smodalprint = $smodalprint;
188
        return $this;
189
    }
190
191
192
    /**
193
     * @param string $smodaleffect
194
     * @return Smodal
195
     */
196
    public function setSmodaleffect(string $smodaleffect): Smodal
197
    {
198
        $this->smodaleffect = $smodaleffect;
199
        return $this;
200
    }
201
202
    /**
203
     * @param string $smodaldata
204
     * @return Smodal
205
     */
206
    public function setSmodaldata(string $smodaldata): Smodal
207
    {
208
        $this->smodaldata = $smodaldata;
209
        return $this;
210
    }
211
212
213
    /**
214
     * @param string $element
215
     * @param string $data
216
     * @param string $value
217
     * @return Smodal
218
     */
219
    public function setSadddata(string $element, string $data, string $value): Smodal
220
    {
221
        $this->sadddata[] = [$element => [$data => $value]];
222
        return $this;
223
    }
224
225
    /**
226
     * @param string $element
227
     * @param string $data
228
     * @return Smodal
229
     */
230
    public function setSremovedata(string $element, string $data): Smodal
231
    {
232
        $this->sremovedata[] = [$element => $data];
233
        return $this;
234
    }
235
236
237
    /**
238
     * @param string $element
239
     * @param string $attr
240
     * @param string $value
241
     * @return Smodal
242
     */
243
    public function setSaddattr(string $element, string $attr, string $value): Smodal
244
    {
245
        $this->saddattr[] = [$element => [$attr => $value]];
246
        return $this;
247
    }
248
249
250
    /**
251
     * @param string $element
252
     * @param string $attr
253
     * @return Smodal
254
     */
255
    public function setSremoveattr(string $element, string $attr): Smodal
256
    {
257
        $this->sremoveattr[] = [$element => $attr];
258
        return $this;
259
    }
260
261
262
    /**
263
     * @param string $element
264
     * @param string $value
265
     * @return Smodal
266
     */
267
    public function setSaddhtml(string $element, string $value): Smodal
268
    {
269
        $this->saddhtml[] = [$element => $value];
270
        return $this;
271
    }
272
273
274
    /**
275
     * @param string $element
276
     * @param string $class
277
     * @return Smodal
278
     */
279
    public function setSaddclass(string $element, string $class): Smodal
280
    {
281
        $this->saddclass[] = [$element => $class];
282
        return $this;
283
    }
284
285
286
    /**
287
     * @param string $element
288
     * @param string $class
289
     * @return Smodal
290
     */
291
    public function setSremoveclass(string $element, string $class): Smodal
292
    {
293
        $this->sremoveclass[] = [$element => $class];
294
        return $this;
295
    }
296
297
298
    /**
299
     * @param string $element
300
     * @return Smodal
301
     */
302
    public function setSremoveelement(string $element): Smodal
303
    {
304
        $this->sremoveelement = $element;
305
        return $this;
306
    }
307
308
309
    /**
310
     * @param string $element
311
     * @param string $css
312
     * @param string $value
313
     * @return Smodal
314
     */
315
    public function setSaddcss(string $element, string $css, string $value): Smodal
316
    {
317
        $this->saddcss[] = [$element => [$css => $value]];
318
        return $this;
319
    }
320
321
    /**
322
     * @param mixed $resultstring
323
     */
324
    public function setResultstring($resultstring): void
325
    {
326
        $this->resultstring = $resultstring;
327
    }
328
329
    /**
330
     * @return mixed
331
     */
332
    private function getResultstring(): ?string
333
    {
334
        return $this->resultstring;
335
    }
336
337
    /**
338
     * @return string
339
     */
340
    public function renderString(): string
341
    {
342
        foreach ($this->methods as $method) {
343
            $this->setResultstring($this->getResultstring() . $this->createString($method));
344
        }
345
        return $this->getResultstring();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getResultstring() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
346
    }
347
348
    /**
349
     * @return object
350
     */
351
    public function renderObject(): object
352
    {
353
        $objectSmodal = new \stdClass();
354
355
        foreach ($this->methods as $method) {
356
            $objectSmodal->$method = $this->convertString($this->$method);
357
        }
358
        $objectSmodal->sdebug = $this->sdebug;
359
        return $objectSmodal;
360
    }
361
362
    /**
363
     * @param $name
364
     * @return string
365
     */
366
    private function createString($name): ?string
367
    {
368
        $result = "";
369
        if ($this->$name) {
370
            $result = "{$name}='{$this->convertString($this->$name)}' ";
371
            $this->setResultstring($this->getResultstring() . $result);
372
        }
373
        return $result;
374
    }
375
376
    /**
377
     * @param $options
378
     * @return string|null
379
     */
380
    private function convertStringForeach($options): ?string
381
    {
382
        $result = '';
383
        foreach ($options as $option) {
384
            foreach ($option as $subkey => $subvalue) {
385
                if (is_int($subkey)) {
386
                    $result .= "{$subvalue}|";
387
                } else {
388
                    if (!is_array($subvalue)) {
389
                        $result .= "{$subkey}::{$subvalue}|";
390
                    } else {
391
                        foreach ($subvalue as $ssbkey => $ssvalue) {
392
393
                            $result .= "{$subkey}::{$ssbkey}=={$ssvalue}|";
394
                        }
395
                    }
396
                }
397
            }
398
        }
399
        return $result;
400
    }
401
402
    /**
403
     * @param $options
404
     * @return string
405
     */
406
    private function convertString($options): string
407
    {
408
409
        $result = "";
410
        if (!is_array($options)) {
411
            $result .= $options;
412
        } else {
413
            $result .= $this->convertStringForeach($options);
414
            $result = substr($result, 0, -1);
415
        }
416
417
        return $result;
418
    }
419
420
421
}