Completed
Branch master (555f37)
by Alfred
01:38
created

FormGenerator::addCountries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Iriven France
5
 * Date: 08/05/2018
6
 * Time: 16:25
7
 */
8
9
namespace Iriven\Plugins\Form;
10
11
use \Bundle\Core\CoreController;
0 ignored issues
show
Bug introduced by
The type Bundle\Core\CoreController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use \Iriven\Plugins\Form\Core\Libs\Collection;
13
use \Iriven\Plugins\Form\Elements\Audio;
14
use \Iriven\Plugins\Form\Elements\Button;
15
use \Iriven\Plugins\Form\Elements\Captcha;
16
use \Iriven\Plugins\Form\Elements\Checkbox;
17
use \Iriven\Plugins\Form\Elements\Color;
18
use \Iriven\Plugins\Form\Elements\Country;
19
use \Iriven\Plugins\Form\Elements\Datalist;
20
use \Iriven\Plugins\Form\Elements\Date;
21
use \Iriven\Plugins\Form\Elements\Datetime;
22
use \Iriven\Plugins\Form\Elements\DatetimeLocal;
23
use \Iriven\Plugins\Form\Elements\Editor;
24
use \Iriven\Plugins\Form\Elements\Email;
25
use \Iriven\Plugins\Form\Elements\File;
26
use \Iriven\Plugins\Form\Elements\Hidden;
27
use \Iriven\Plugins\Form\Elements\Image;
28
use \Iriven\Plugins\Form\Elements\Month;
29
use \Iriven\Plugins\Form\Elements\Number;
30
use \Iriven\Plugins\Form\Elements\Password;
31
use \Iriven\Plugins\Form\Elements\Phone;
32
use \Iriven\Plugins\Form\Elements\Radio;
33
use \Iriven\Plugins\Form\Elements\Range;
34
use \Iriven\Plugins\Form\Elements\Reset;
35
use \Iriven\Plugins\Form\Elements\Search;
36
use \Iriven\Plugins\Form\Elements\Select;
37
use \Iriven\Plugins\Form\Elements\Submit;
38
use \Iriven\Plugins\Form\Elements\Text;
39
use \Iriven\Plugins\Form\Elements\Textarea;
40
use \Iriven\Plugins\Form\Elements\Time;
41
use \Iriven\Plugins\Form\Elements\Url;
42
use \Iriven\Plugins\Form\Elements\Video;
43
use \Iriven\Plugins\Form\Elements\Week;
44
use \Iriven\Plugins\Form\Elements\YesNo;
45
use \Iriven\Plugins\Form\Core\fieldsetGenerator;
46
use \Iriven\Plugins\Form\Core\form;
47
use \Iriven\Plugins\Form\Core\Libs\Traits\KeyNormalizer;
48
use \Iriven\Plugins\Form\Core\FormElement;
49
50
51
class FormGenerator
52
{
53
    use KeyNormalizer;
54
    public  $Errors;
55
    private $Form;
56
    private $Fieldset;
57
    private $Request;
58
59
    /**
60
     * FormGenerator constructor.
61
     *
62
     * @param CoreController $App
63
     */
64
    public function __construct(CoreController $App)
65
    {
66
        $this->Request = $App->Request();
67
        $formErrors = $App->get('errors',[]);
68
        is_array($formErrors) OR $formErrors = [$formErrors];
69
        $this->Errors = new Collection($formErrors);
70
    }
71
72
    /**
73
     * @param       $label
74
     * @param array $attributes
75
     * @return $this
76
     */
77
    public function addAudio($label,$attributes=[])
78
    {
79
        if($this->Form instanceof form)
80
        {
81
            $item = new Audio($label,$attributes);
82
            $item->Attributes()->set('accept','audio/*');
83
            $item->Attributes()->set('value', $this->getDefaultValue($item));
84
            $this->Form->Attributes()->set('enctype','multipart/form-data');
85
            $this->Form->append($item);
86
        }
87
        return $this;
88
    }
89
90
    /**
91
     * @param       $label
92
     * @param array $attributes
93
     * @return $this
94
     */
95
    public function addButton($label,$attributes=[])
96
    {
97
        if($this->Form instanceof form)
98
        {
99
            $item = new Button($label,$attributes);
100
            $this->Form->append($item);
101
        }
102
        return $this;
103
    }
104
105
    /**
106
     * @param       $label
107
     * @param array $attributes
108
     * @return $this
109
     */
110
    public function addCaptcha($label,$attributes=[])
111
    {
112
        if($this->Form instanceof form)
113
        {
114
            $item = new Captcha($label,$attributes);
115
            //$this->Request->getSession()->set(md5($item->Attributes()->get('name')), $item->Attributes()->get('capresponse'));
116
           // $this->Request->Session->set(md5($item->Attributes()->get('name')),$item->Attributes()->get('capresponse'));
117
            $this->Form->append($item);
118
        }
119
        return $this;
120
    }
121
122
    /**
123
     * @param $label
124
     * @param array $options
125
     * @param array $attributes
126
     * @return $this
127
     */
128
    public function addCheckbox($label,$options = [],$attributes=[])
129
    {
130
        if($this->Form instanceof form)
131
        {
132
            $item = new Checkbox($label,$options ,$attributes);
133
            $item->Attributes()->set('value', $this->getDefaultValue($item));
134
            $this->Form->append($item);
135
        }
136
        return $this;
137
    }
138
139
    /**
140
     * @param $label
141
     * @param array $attributes
142
     * @return $this
143
     */
144
    public function addColor($label,$attributes=[])
145
    {
146
        if($this->Form instanceof form)
147
        {
148
            $item = new Color($label,$attributes);
149
            $item->Attributes()->set('value', $this->getDefaultValue($item));
150
            $this->Form->append($item);
151
        }
152
        return $this;
153
    }
154
155
    /**
156
     * @param $label
157
     * @param array $attributes
158
     * @return $this
159
     */
160
    public function addCountries($label,$attributes=[])
161
    {
162
        if($this->Form instanceof form)
163
        {
164
            $item = new Country($label,$attributes);
165
            $item->Attributes()->set('value', $this->getDefaultValue($item));
166
            $this->Form->append($item);
167
        }
168
        return $this;
169
    }
170
    /**
171
     * @param $label
172
     * @param array $attributes
173
     * @return $this
174
     */   
175
    public function addDatalist($label, $options=[], $attributes = [])
176
    {
177
        if ($this->Form instanceof form) {
178
                $item = new Datalist($label, $options, $attributes);
179
                $item->Attributes()->set('value', $this->getDefaultValue($item));
180
                $this->Form->append($item);
181
            }
182
        return $this;
183
    }
184
    
185
    /**
186
     * @param $label
187
     * @param array $attributes
188
     * @return $this
189
     */
190
    public function addDate($label,$attributes=[])
191
    {
192
        if($this->Form instanceof form)
193
        {
194
            $item = new Date($label,$attributes);
195
            $item->Attributes()->set('value', $this->getDefaultValue($item));
196
            $this->Form->append($item);
197
        }
198
        return $this;
199
    }
200
201
    /**
202
     * @param $label
203
     * @param array $attributes
204
     * @return $this
205
     */
206
    public function addDatetime($label,$attributes=[])
207
    {
208
        if($this->Form instanceof form)
209
        {
210
            $item = new Datetime($label,$attributes);
211
            $item->Attributes()->set('value', $this->getDefaultValue($item));
212
            $this->Form->append($item);
213
        }
214
        return $this;
215
    }
216
217
    /**
218
     * @param $label
219
     * @param array $attributes
220
     * @return $this
221
     */
222
    public function addDatetimeLocal($label,$attributes=[])
223
    {
224
        if($this->Form instanceof form)
225
        {
226
            $item = new DatetimeLocal($label,$attributes);
227
            $item->Attributes()->set('value', $this->getDefaultValue($item));
228
            $this->Form->append($item);
229
        }
230
        return $this;
231
    }
232
    /**
233
     * @param $label
234
     * @param array $attributes
235
     * @return $this
236
     */
237
    public function addEditor($label,$attributes=[])
238
    {
239
        if($this->Form instanceof form)
240
        {
241
            $item = new Editor($label,$attributes);
242
            $item->Attributes()->set('value', $this->getDefaultValue($item));
243
            $this->Form->append($item);
244
        }
245
        return $this;
246
    }
247
    /**
248
     * @param $label
249
     * @param array $attributes
250
     * @return $this
251
     */
252
    public function addEmail($label,$attributes=[])
253
    {
254
        if($this->Form instanceof form)
255
        {
256
            $item = new Email($label,$attributes);
257
            $item->Attributes()->set('value', $this->getDefaultValue($item));
258
            $this->Form->append($item);
259
        }
260
        return $this;
261
    }
262
    /**
263
     * @param array $attributes
264
     * @return $this
265
     */
266
    public function addFieldset($attributes=[])
267
    {
268
        if($this->Form instanceof form)
269
        {
270
            if($this->Fieldset instanceof fieldsetGenerator)
271
                $this->Form->append($this->Fieldset->Close());
272
            $this->Fieldset = new fieldsetGenerator($attributes);
273
            $this->Fieldset->Attributes()->set('name', $this->Form->Attributes()->get('name'));
274
            $this->Form->append($this->Fieldset->Open());
275
        }
276
        return $this;  
277
    }
278
    /**
279
     * @param       $label
280
     * @param array $attributes
281
     * @return $this
282
     */
283
    public function addFile($label,$attributes=[])
284
    {
285
        if($this->Form instanceof form)
286
        {
287
            $item = new File($label,$attributes);
288
            $this->Form->Attributes()->set('enctype','multipart/form-data');
289
            $item->Attributes()->set('value', $this->getDefaultValue($item));
290
            $this->Form->append($item);
291
        }
292
        return $this;
293
    }
294
295
    /**
296
     * @param $label
297
     * @param array $attributes
298
     * @return $this
299
     */
300
    public function addHidden($label,$attributes=[])
301
    {
302
        if($this->Form instanceof form)
303
        {
304
            $item = new Hidden($label,$attributes);
305
            $item->Attributes()->set('value', $this->getDefaultValue($item));
306
            $this->Form->append($item);
307
        }
308
        return $this;
309
    }
310
311
    /**
312
     * @param $html
313
     * @return $this
314
     */
315
    public function addHtml($html)
316
    {
317
        if($this->Form instanceof form)
318
        {
319
            if(gettype($html) === 'string')
320
            $this->Form->append($html);
321
        }
322
        return $this;
323
    }
324
325
    /**
326
     * @param $label
327
     * @param array $attributes
328
     * @return $this
329
     */
330
    public function addImage($label,$attributes=[])
331
    {
332
        if($this->Form instanceof form)
333
        {
334
            $item = new Image($label,$attributes);
335
            $this->Form->Attributes()->set('enctype','multipart/form-data');
336
            $item->Attributes()->set('value', $this->getDefaultValue($item));
337
            $this->Form->append($item);
338
        }
339
        return $this;
340
    }
341
342
    /**
343
     * @param $label
344
     * @param array $attributes
345
     * @return $this
346
     */
347
    public function addMonth($label,$attributes=[])
348
    {
349
        if($this->Form instanceof form)
350
        {
351
            $item = new Month($label,$attributes);
352
            $item->Attributes()->set('value', $this->getDefaultValue($item));
353
            $this->Form->append($item);
354
        }
355
        return $this;
356
    }
357
358
    /**
359
     * @param $label
360
     * @param array $attributes
361
     * @return $this
362
     */
363
    public function addNumber($label,$attributes=[])
364
    {
365
        if($this->Form instanceof form)
366
        {
367
            $item = new Number($label,$attributes);
368
            $item->Attributes()->set('value', $this->getDefaultValue($item));
369
            $this->Form->append($item);
370
        }
371
        return $this;
372
    }
373
    /**
374
     * @param $label
375
     * @param array $attributes
376
     * @return $this
377
     */
378
    public function addPassword($label,$attributes=[])
379
    {
380
        if($this->Form instanceof form)
381
        {
382
            $item = new Password($label,$attributes);
383
            $item->Attributes()->set('value', '');
384
            $this->Form->append($item);
385
        }
386
        return $this;
387
    }
388
389
    /**
390
     * @param $label
391
     * @param array $attributes
392
     * @return $this
393
     */
394
    public function addPhone($label,$attributes=[])
395
    {
396
        if($this->Form instanceof form)
397
        {
398
            $item = new Phone($label,$attributes);
399
            $item->Attributes()->set('value', $this->getDefaultValue($item));
400
            $this->Form->append($item);
401
        }
402
        return $this;
403
    }
404
405
    /**
406
     * @param $label
407
     * @param array $options
408
     * @param array $attributes
409
     * @return $this
410
     */
411
    public function addRadio($label,$options=[],$attributes=[])
412
    {
413
        if($this->Form instanceof form)
414
        {
415
            $item = new Radio($label,$options,$attributes);
416
            $item->Attributes()->set('value', $this->getDefaultValue($item));
417
            $this->Form->append($item);
418
        }
419
        return $this;
420
    }
421
422
    /**
423
     * @param $label
424
     * @param array $attributes
425
     * @return $this
426
     */
427
    public function addRange($label,$attributes=[])
428
    {
429
        if($this->Form instanceof form)
430
        {
431
            $item = new Range($label,$attributes);
432
            $item->Attributes()->set('value', $this->getDefaultValue($item));
433
            $this->Form->append($item);
434
        }
435
        return $this;
436
    }
437
438
    /**
439
     * @param $label
440
     * @param array $attributes
441
     * @return $this
442
     */
443
    public function addReset($label,$attributes=[])
444
    {
445
        if($this->Form instanceof form)
446
        {
447
            $item = new Reset($label,$attributes);
448
            $item->Attributes()->set('value', $this->getDefaultValue($item));
449
            $this->Form->append($item);
450
        }
451
        return $this;
452
    }
453
454
    /**
455
     * @param $label
456
     * @param array $attributes
457
     * @return $this
458
     */
459
    public function addSearch($label,$attributes=[])
460
    {
461
        if($this->Form instanceof form)
462
        {
463
            $item = new Search($label,$attributes);
464
            $item->Attributes()->set('value', $this->getDefaultValue($item));
465
            $this->Form->append($item);
466
        }
467
        return $this;
468
    }
469
    /**
470
     * @param $label
471
     * @param array $options
472
     * @param array $attributes
473
     * @return $this
474
     */
475
    public function addSelect($label,array $options = [],$attributes=[])
476
    {
477
        if($this->Form instanceof form)
478
        {
479
            $item = new Select($label,$options,$attributes);
480
            $item->Attributes()->set('value', $this->getDefaultValue($item));
481
            $this->Form->append($item);
482
        }
483
        return $this;
484
    }
485
    /**
486
     * @param $label
487
     * @param array $attributes
488
     * @return $this
489
     */
490
    public function addSubmit($label,$attributes=[])
491
    {
492
        if($this->Form instanceof form)
493
        {
494
            $item = new Submit($label,$attributes);
495
            $this->Form->append($item);
496
        }
497
        return $this;
498
    }
499
500
    /**
501
     * @param $label
502
     * @param array $attributes
503
     * @return $this
504
     */
505
    public function addText($label,$attributes=[])
506
    {
507
        if($this->Form instanceof form)
508
        {
509
            $item = new Text($label,$attributes);
510
            $item->Attributes()->set('value', $this->getDefaultValue($item));
511
            $this->Form->append($item);
512
        }
513
        return $this;
514
    }
515
516
    /**
517
     * @param $label
518
     * @param array $attributes
519
     * @return $this
520
     */
521
    public function addTextarea($label,$attributes=[])
522
    {
523
        if($this->Form instanceof form)
524
        {
525
            $item = new Textarea($label,$attributes);
526
            $item->Attributes()->set('value', $this->getDefaultValue($item));
527
            $this->Form->append($item);
528
        }
529
        return $this;
530
    }
531
532
    /**
533
     * @param $label
534
     * @param array $attributes
535
     * @return $this
536
     */
537
    public function addTime($label,$attributes=[])
538
    {
539
        if($this->Form instanceof form)
540
        {
541
            $item = new Time($label,$attributes);
542
            $item->Attributes()->set('value', $this->getDefaultValue($item));
543
            $this->Form->append($item);
544
        }
545
        return $this;
546
    }
547
548
    /**
549
     * @param $label
550
     * @param array $attributes
551
     * @return $this
552
     */
553
    public function addUrl($label,$attributes=[])
554
    {
555
        if($this->Form instanceof form)
556
        {
557
            $item = new Url($label,$attributes);
558
            $item->Attributes()->set('value', $this->getDefaultValue($item));
559
            $this->Form->append($item);
560
        }
561
        return $this;
562
    }
563
564
    /**
565
     * @param $label
566
     * @param array $attributes
567
     * @return $this
568
     */
569
    public function addVideo($label,$attributes=[])
570
    {
571
        if($this->Form instanceof form)
572
        {
573
            $item = new Video($label,$attributes);
574
            $this->Form->Attributes()->set('enctype','multipart/form-data');
575
            $item->Attributes()->set('value', $this->getDefaultValue($item));
576
            $this->Form->append($item);
577
        }
578
        return $this;
579
    }
580
    /**
581
     * @param $label
582
     * @param array $attributes
583
     * @return $this
584
     */
585
    public function addWeek($label,$attributes=[])
586
    {
587
        if($this->Form instanceof form)
588
        {
589
            $item = new Week($label,$attributes);
590
            $item->Attributes()->set('value', $this->getDefaultValue($item));
591
            $this->Form->append($item);
592
        }
593
        return $this;
594
    }
595
596
    /**
597
     * @param $label
598
     * @param array $attributes
599
     * @return $this
600
     */
601
    public function addYesNo($label,$attributes=[])
602
    {
603
        if($this->Form instanceof form)
604
        {
605
            $item = new YesNo($label,$attributes);
606
            $item->Attributes()->set('value', $this->getDefaultValue($item));
607
            $this->Form->append($item);
608
        }
609
        return $this;
610
    }
611
    /**
612
     * @param bool $print
613
     * @return null|string
614
     */
615
    public function Close($print=true)
616
    {
617
        $html = null;
618
        if ($this->Form instanceof form)
619
        {
620
            if($this->Fieldset instanceof fieldsetGenerator)
621
                $this->Form->append($this->Fieldset->Close());
622
            $html = $this->Form->RenderHtml();
623
            $this->Form = null;
624
            if($print)
625
                print_r($html);
626
        }
627
        return $html;
628
    }
629
    /**
630
     * @return mixed
631
     */
632
    public function Errors()
633
    {
634
        return $this->Errors;
635
    }
636
    /**
637
     * @param $element
638
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
639
     * @return mixed
640
     */
641
    private function getDefaultValue(FormElement $element,$default=null)
642
    {
643
        $name = $element->Attributes()->get('name');
644
        $method = strtoupper($this->Request->getMethod());
645
        switch($method){
646
            case 'POST':
647
            case 'PUT':
648
            case 'DELETE':
649
            case 'PATCH':
650
                $Request = $this->Request->request;
651
            break;           
652
            default:
653
                $Request = $this->Request->query; 
654
            break;
655
        }
656
        if(!$value = $Request->get($name, $default))
657
        $value = $element->Attributes()->get('value', $default);
658
        return $value;
659
    }
660
    /**
661
     * @param array $attributes
662
     * @return $this
663
     */
664
    public function Open($attributes = [])
665
    {
666
        if($this->Form instanceof form)
667
            $this->Close();
668
        $this->Form = new form($attributes);
669
        return $this;
670
    }
671
}