Completed
Push — master ( 7261f1...460b97 )
by Ryan
09:21 queued 04:12
created

FormBuilder::mergeSkips()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Ui\Form;
2
3
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
4
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface;
5
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
6
use Anomaly\Streams\Platform\Field\Contract\FieldInterface;
7
use Anomaly\Streams\Platform\Model\EloquentModel;
8
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
9
use Anomaly\Streams\Platform\Support\Collection;
10
use Anomaly\Streams\Platform\Traits\FiresCallbacks;
11
use Anomaly\Streams\Platform\Ui\Button\Contract\ButtonInterface;
12
use Anomaly\Streams\Platform\Ui\Form\Command\AddAssets;
13
use Anomaly\Streams\Platform\Ui\Form\Command\BuildForm;
14
use Anomaly\Streams\Platform\Ui\Form\Command\FlashFieldValues;
15
use Anomaly\Streams\Platform\Ui\Form\Command\FlashFormErrors;
16
use Anomaly\Streams\Platform\Ui\Form\Command\LoadForm;
17
use Anomaly\Streams\Platform\Ui\Form\Command\LoadFormValues;
18
use Anomaly\Streams\Platform\Ui\Form\Command\MakeForm;
19
use Anomaly\Streams\Platform\Ui\Form\Command\PopulateFields;
20
use Anomaly\Streams\Platform\Ui\Form\Command\PostForm;
21
use Anomaly\Streams\Platform\Ui\Form\Command\SaveForm;
22
use Anomaly\Streams\Platform\Ui\Form\Command\SetFormResponse;
23
use Anomaly\Streams\Platform\Ui\Form\Command\ValidateForm;
24
use Anomaly\Streams\Platform\Ui\Form\Component\Action\ActionCollection;
25
use Anomaly\Streams\Platform\Ui\Form\Component\Action\Contract\ActionInterface;
26
use Anomaly\Streams\Platform\Ui\Form\Contract\FormRepositoryInterface;
27
use Closure;
28
use Illuminate\Contracts\Support\MessageBag;
29
use Illuminate\Foundation\Bus\DispatchesJobs;
30
use Symfony\Component\HttpFoundation\Response;
31
32
/**
33
 * Class FormBuilder
34
 *
35
 * @link   http://pyrocms.com/
36
 * @author PyroCMS, Inc. <[email protected]>
37
 * @author Ryan Thompson <[email protected]>
38
 */
39
class FormBuilder
40
{
41
42
    use DispatchesJobs;
43
    use FiresCallbacks;
44
45
    /**
46
     * The ajax flag.
47
     *
48
     * @var bool
49
     */
50
    protected $ajax = false;
51
52
    /**
53
     * The form handler.
54
     *
55
     * @var null|string
56
     */
57
    protected $handler = null;
58
59
    /**
60
     * The form validator.
61
     *
62
     * @var null|string
63
     */
64
    protected $validator = null;
65
66
    /**
67
     * The form repository.
68
     *
69
     * @var null|FormRepositoryInterface
70
     */
71
    protected $repository = null;
72
73
    /**
74
     * The form model.
75
     *
76
     * @var null
77
     */
78
    protected $model = null;
79
80
    /**
81
     * The entry object.
82
     *
83
     * @var null|int
84
     */
85
    protected $entry = null;
86
87
    /**
88
     * The fields config.
89
     *
90
     * @var array|string
91
     */
92
    protected $fields = [];
93
94
    /**
95
     * Fields to skip.
96
     *
97
     * @var array|string
98
     */
99
    protected $skips = [];
100
101
    /**
102
     * The actions config.
103
     *
104
     * @var array|string
105
     */
106
    protected $actions = [];
107
108
    /**
109
     * The buttons config.
110
     *
111
     * @var array|string
112
     */
113
    protected $buttons = [];
114
115
    /**
116
     * The form options.
117
     *
118
     * @var array
119
     */
120
    protected $options = [];
121
122
    /**
123
     * The form sections.
124
     *
125
     * @var array
126
     */
127
    protected $sections = [];
128
129
    /**
130
     * The form assets.
131
     *
132
     * @var array
133
     */
134
    protected $assets = [];
135
136
    /**
137
     * The save flag.
138
     *
139
     * @var bool
140
     */
141
    protected $save = true;
142
143
    /**
144
     * The read only flag.
145
     *
146
     * @var bool
147
     */
148
    protected $readOnly = false;
149
150
    /**
151
     * The form object.
152
     *
153
     * @var Form
154
     */
155
    protected $form;
156
157
    /**
158
     * Crate a new FormBuilder instance.
159
     *
160
     * @param Form $form
161
     */
162
    public function __construct(Form $form)
163
    {
164
        $this->form = $form;
165
    }
166
167
    /**
168
     * Build the form.
169
     *
170
     * @param  null $entry
171
     * @return $this
172
     */
173
    public function build($entry = null)
174
    {
175
        if ($entry) {
176
            $this->entry = $entry;
177
        }
178
179
        $this->fire('ready', ['builder' => $this]);
180
181
        $this->dispatch(new BuildForm($this));
182
183
        $this->fire('built', ['builder' => $this]);
184
185
        return $this;
186
    }
187
188
    /**
189
     * Make the form.
190
     *
191
     * @param  null $entry
192
     * @return $this
193
     */
194
    public function make($entry = null)
195
    {
196
        $this->build($entry);
197
        $this->post();
198
199
        $this->fire('make', ['builder' => $this]);
200
201
        if ($this->getFormResponse() === null) {
202
            $this->dispatch(new LoadForm($this));
203
            $this->dispatch(new AddAssets($this));
204
            $this->dispatch(new MakeForm($this));
205
        }
206
207
        return $this;
208
    }
209
210
    /**
211
     * Handle the form post.
212
     *
213
     * @param  null $entry
214
     * @return $this
215
     * @throws \Exception
216
     */
217
    public function handle($entry = null)
218
    {
219
        if (!app('request')->isMethod('post')) {
220
            throw new \Exception('The handle method must be used with a POST request.');
221
        }
222
223
        $this->build($entry);
224
        $this->post();
225
226
        return $this;
227
    }
228
229
    /**
230
     * Trigger post operations
231
     * for the form.
232
     *
233
     * @return $this
234
     */
235
    public function post()
236
    {
237
        if (app('request')->isMethod('post')) {
238
            $this->fire('post', ['builder' => $this]);
239
240
            if ($this->hasPostData()) {
241
                $this->dispatch(new PostForm($this));
242
            }
243
        } else {
244
            $this->dispatch(new PopulateFields($this));
245
        }
246
247
        return $this;
248
    }
249
250
    /**
251
     * Validate the form.
252
     *
253
     * @return $this
254
     */
255
    public function validate()
256
    {
257
        $this->dispatch(new LoadFormValues($this));
258
        $this->dispatch(new ValidateForm($this));
259
260
        return $this;
261
    }
262
263
    /**
264
     * Flash form information to be
265
     * used in conjunction with redirect
266
     * type responses (not self handling).
267
     */
268
    public function flash()
269
    {
270
        $this->dispatch(new FlashFormErrors($this));
271
        $this->dispatch(new FlashFieldValues($this));
272
    }
273
274
    /**
275
     * Render the form.
276
     *
277
     * @param  null $entry
278
     * @return Response
279
     */
280
    public function render($entry = null)
281
    {
282
        $this->make($entry);
283
284
        if (!$this->form->getResponse()) {
285
            $this->dispatch(new SetFormResponse($this));
286
        }
287
288
        return $this->form->getResponse();
289
    }
290
291
    /**
292
     * Fire field events.
293
     *
294
     * @param       $trigger
295
     * @param array $payload
296
     */
297
    public function fireFieldEvents($trigger, array $payload = [])
298
    {
299
        /* @var FieldType $field */
300
        foreach ($this->getFormFields() as $field) {
301
            $field->fire($trigger, array_merge(['builder' => $this], $payload));
302
        }
303
    }
304
305
    /**
306
     * Save the form.
307
     */
308
    public function saveForm()
309
    {
310
        $this->dispatch(new SaveForm($this));
311
    }
312
313
    /**
314
     * Get the form object.
315
     *
316
     * @return Form
317
     */
318
    public function getForm()
319
    {
320
        return $this->form;
321
    }
322
323
    /**
324
     * Get the form presenter.
325
     *
326
     * @return FormPresenter
327
     */
328
    public function getFormPresenter()
329
    {
330
        return $this->form->getPresenter();
331
    }
332
333
    /**
334
     * Get the ajax flag.
335
     *
336
     * @return bool
337
     */
338
    public function isAjax()
339
    {
340
        return $this->ajax;
341
    }
342
343
    /**
344
     * Set the ajax flag.
345
     *
346
     * @param $ajax
347
     * @return $this
348
     */
349
    public function setAjax($ajax)
350
    {
351
        $this->ajax = $ajax;
352
353
        return $this;
354
    }
355
356
    /**
357
     * Get the handler.
358
     *
359
     * @return null|string
360
     */
361
    public function getHandler()
362
    {
363
        return $this->handler;
364
    }
365
366
    /**
367
     * Set the handler.
368
     *
369
     * @param $handler
370
     * @return $this
371
     */
372
    public function setHandler($handler)
373
    {
374
        $this->handler = $handler;
375
376
        return $this;
377
    }
378
379
    /**
380
     * Get the validator.
381
     *
382
     * @return null|string
383
     */
384
    public function getValidator()
385
    {
386
        return $this->validator;
387
    }
388
389
    /**
390
     * Set the validator.
391
     *
392
     * @param $validator
393
     * @return $this
394
     */
395
    public function setValidator($validator)
396
    {
397
        $this->validator = $validator;
398
399
        return $this;
400
    }
401
402
    /**
403
     * Get the repository.
404
     *
405
     * @return FormRepositoryInterface|null
406
     */
407
    public function getRepository()
408
    {
409
        return $this->repository;
410
    }
411
412
    /**
413
     * Set the form repository.
414
     *
415
     * @param  FormRepositoryInterface $repository
416
     * @return $this
417
     */
418
    public function setRepository(FormRepositoryInterface $repository)
419
    {
420
        $this->repository = $repository;
421
422
        return $this;
423
    }
424
425
    /**
426
     * Set the form model.
427
     *
428
     * @param  $model
429
     * @return $this
430
     */
431
    public function setModel($model)
432
    {
433
        $this->model = $model;
434
435
        return $this;
436
    }
437
438
    /**
439
     * Get the form model.
440
     *
441
     * @return null
442
     */
443
    public function getModel()
444
    {
445
        return $this->model;
446
    }
447
448
    /**
449
     * Set the entry object.
450
     *
451
     * @param  $entry
452
     * @return $this
453
     */
454
    public function setEntry($entry)
455
    {
456
        $this->entry = $entry;
457
458
        return $this;
459
    }
460
461
    /**
462
     * Get the entry object.
463
     *
464
     * @return null|EntryInterface|FieldInterface|mixed
465
     */
466
    public function getEntry()
467
    {
468
        return $this->entry;
469
    }
470
471
    /**
472
     * Set the fields.
473
     *
474
     * @param  $fields
475
     * @return $this
476
     */
477
    public function setFields($fields)
478
    {
479
        $this->fields = $fields;
480
481
        return $this;
482
    }
483
484
    /**
485
     * Get the fields.
486
     *
487
     * @return array
488
     */
489
    public function getFields()
490
    {
491
        return $this->fields;
492
    }
493
494
    /**
495
     * Add a field.
496
     *
497
     * @param   $field
498
     */
499
    public function addField($field)
500
    {
501
        $this->fields[array_get($field, 'field')] = $field;
502
    }
503
504
    /**
505
     * Add fields.
506
     *
507
     * @param array $fields
508
     */
509
    public function addFields(array $fields)
510
    {
511
        $this->fields = array_merge($this->fields, $fields);
512
    }
513
514
    /**
515
     * Get the skipped fields.
516
     *
517
     * @return array
518
     */
519
    public function getSkips()
520
    {
521
        return $this->skips;
522
    }
523
524
    /**
525
     * Set the skipped fields.
526
     *
527
     * @param $skips
528
     * @return $this
529
     */
530
    public function setSkips($skips)
531
    {
532
        $this->skips = $skips;
533
534
        return $this;
535
    }
536
537
    /**
538
     * Merge in skipped fields.
539
     *
540
     * @param array $skips
541
     * @return $this
542
     */
543
    public function mergeSkips(array $skips)
544
    {
545
        $this->skips = array_merge($this->skips, $skips);
546
547
        return $this;
548
    }
549
550
    /**
551
     * Add a skipped field.
552
     *
553
     * @param $fieldSlug
554
     * @return $this
555
     */
556
    public function skipField($fieldSlug)
557
    {
558
        $this->skips[] = $fieldSlug;
559
560
        return $this;
561
    }
562
563
    /**
564
     * Set the actions config.
565
     *
566
     * @param  $actions
567
     * @return $this
568
     */
569
    public function setActions($actions)
570
    {
571
        $this->actions = $actions;
572
573
        return $this;
574
    }
575
576
    /**
577
     * Add an action.
578
     *
579
     * @param        $slug
580
     * @param  array $definition
581
     * @return $this
582
     */
583
    public function addAction($slug, array $definition = [])
584
    {
585
        if ($definition) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $definition of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
586
            $this->actions[$slug] = $definition;
587
        } else {
588
            $this->actions[] = $slug;
589
        }
590
591
        return $this;
592
    }
593
594
    /**
595
     * Get the actions config.
596
     *
597
     * @return array
598
     */
599
    public function getActions()
600
    {
601
        return $this->actions;
602
    }
603
604
    /**
605
     * Set the buttons config.
606
     *
607
     * @param  $buttons
608
     * @return $this
609
     */
610
    public function setButtons($buttons)
611
    {
612
        $this->buttons = $buttons;
613
614
        return $this;
615
    }
616
617
    /**
618
     * Get the buttons config.
619
     *
620
     * @return array
621
     */
622
    public function getButtons()
623
    {
624
        return $this->buttons;
625
    }
626
627
    /**
628
     * The the options.
629
     *
630
     * @return array
631
     */
632
    public function getOptions()
633
    {
634
        return $this->options;
635
    }
636
637
    /**
638
     * Set the options.
639
     *
640
     * @param  array|string $options
641
     * @return $this
642
     */
643
    public function setOptions($options)
644
    {
645
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options can also be of type string. However, the property $options is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
646
647
        return $this;
648
    }
649
650
    /**
651
     * Merge in options.
652
     *
653
     * @param  array|string $options
654
     * @return $this
655
     */
656
    public function mergeOptions($options)
657
    {
658
        $this->options = array_merge($this->options, $options);
659
660
        return $this;
661
    }
662
663
    /**
664
     * Get the sections.
665
     *
666
     * @return array
667
     */
668
    public function getSections()
669
    {
670
        return $this->sections;
671
    }
672
673
    /**
674
     * Set the sections.
675
     *
676
     * @param  array|Closure $sections
677
     * @return $this
678
     */
679
    public function setSections($sections)
680
    {
681
        $this->sections = $sections;
0 ignored issues
show
Documentation Bug introduced by
It seems like $sections can also be of type object<Closure>. However, the property $sections is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
682
683
        return $this;
684
    }
685
686
    /**
687
     * Add a section.
688
     *
689
     * @param        $slug
690
     * @param  array $section
691
     * @return $this
692
     */
693
    public function addSection($slug, array $section)
694
    {
695
        array_set($this->sections, $slug, $section);
696
697
        return $this;
698
    }
699
700
    /**
701
     * Add a section tab.
702
     *
703
     * @param        $section
704
     * @param        $slug
705
     * @param  array $tab
706
     * @return $this
707
     */
708
    public function addSectionTab($section, $slug, array $tab)
709
    {
710
        array_set($this->sections, "{$section}.tabs.{$slug}", $tab);
711
712
        return $this;
713
    }
714
715
    /**
716
     * Get an option value.
717
     *
718
     * @param        $key
719
     * @param  null  $default
720
     * @return mixed
721
     */
722
    public function getOption($key, $default = null)
723
    {
724
        return array_get($this->options, $key, $default);
725
    }
726
727
    /**
728
     * Set an option value.
729
     *
730
     * @param $key
731
     * @param $value
732
     * @return $this
733
     */
734
    public function setOption($key, $value)
735
    {
736
        array_set($this->options, $key, $value);
737
738
        return $this;
739
    }
740
741
    /**
742
     * Get the assets.
743
     *
744
     * @return array
745
     */
746
    public function getAssets()
747
    {
748
        return $this->assets;
749
    }
750
751
    /**
752
     * Set the assets.
753
     *
754
     * @param $assets
755
     * @return $this
756
     */
757
    public function setAssets($assets)
758
    {
759
        $this->assets = $assets;
760
761
        return $this;
762
    }
763
764
    /**
765
     * Add an asset.
766
     *
767
     * @param $collection
768
     * @param $asset
769
     * @return $this
770
     */
771 View Code Duplication
    public function addAsset($collection, $asset)
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...
772
    {
773
        if (!isset($this->assets[$collection])) {
774
            $this->assets[$collection] = [];
775
        }
776
777
        $this->assets[$collection][] = $asset;
778
779
        return $this;
780
    }
781
782
    /**
783
     * Get the form's stream.
784
     *
785
     * @return StreamInterface|null
786
     */
787
    public function getFormStream()
788
    {
789
        return $this->form->getStream();
790
    }
791
792
    /**
793
     * Get a form option value.
794
     *
795
     * @param        $key
796
     * @param  null  $default
797
     * @return mixed
798
     */
799
    public function getFormOption($key, $default = null)
800
    {
801
        return $this->form->getOption($key, $default);
802
    }
803
804
    /**
805
     * Set a form option value.
806
     *
807
     * @param $key
808
     * @param $value
809
     * @return $this
810
     */
811
    public function setFormOption($key, $value)
812
    {
813
        $this->form->setOption($key, $value);
814
815
        return $this;
816
    }
817
818
    /**
819
     * Get the form options.
820
     *
821
     * @return \Anomaly\Streams\Platform\Support\Collection
822
     */
823
    public function getFormOptions()
824
    {
825
        return $this->form->getOptions();
826
    }
827
828
    /**
829
     * Get the form model.
830
     *
831
     * @return \Anomaly\Streams\Platform\Entry\EntryModel|EloquentModel|null
832
     */
833
    public function getFormModel()
834
    {
835
        return $this->form->getModel();
836
    }
837
838
    /**
839
     * Get the form entry.
840
     *
841
     * @return EloquentModel|EntryInterface|FieldInterface|AssignmentInterface
842
     */
843
    public function getFormEntry()
844
    {
845
        return $this->form->getEntry();
846
    }
847
848
    /**
849
     * Return the form entry's ID.
850
     *
851
     * @return int|mixed|null
852
     */
853
    public function getFormEntryId()
854
    {
855
        $entry = $this->getFormEntry();
856
857
        if (!$entry instanceof EloquentModel) {
858
            return null;
859
        }
860
861
        return $entry->getId();
862
    }
863
864
    /**
865
     * Get the contextual entry ID.
866
     *
867
     * @return int|null
868
     */
869
    public function getContextualId()
870
    {
871
        return $this->getFormEntryId();
872
    }
873
874
    /**
875
     * Get the form mode.
876
     *
877
     * @return null|string
878
     */
879
    public function getFormMode()
880
    {
881
        return $this->form->getMode();
882
    }
883
884
    /**
885
     * Set the form mode.
886
     *
887
     * @param $mode
888
     * @return $this
889
     */
890
    public function setFormMode($mode)
891
    {
892
        $this->form->setMode($mode);
893
894
        return $this;
895
    }
896
897
    /**
898
     * Get a form value.
899
     *
900
     * @param        $key
901
     * @param  null  $default
902
     * @return mixed
903
     */
904
    public function getFormValue($key, $default = null)
905
    {
906
        return $this->form->getValue($key, $default);
907
    }
908
909
    /**
910
     * Set a form value.
911
     *
912
     * @param $key
913
     * @param $value
914
     * @return $this
915
     */
916
    public function setFormValue($key, $value)
917
    {
918
        $this->form->setValue($key, $value);
919
920
        return $this;
921
    }
922
923
    /**
924
     * Get the form values.
925
     *
926
     * @return \Anomaly\Streams\Platform\Support\Collection
927
     */
928
    public function getFormValues()
929
    {
930
        return $this->form->getValues();
931
    }
932
933
    /**
934
     * Reset the form.
935
     *
936
     * @return $this
937
     */
938
    public function resetForm()
939
    {
940
        $this->form
941
            ->resetFields()
942
            ->setValues(new Collection());
943
944
        return $this;
945
    }
946
947
    /**
948
     * Get the form input.
949
     *
950
     * @return array
951
     */
952
    public function getFormInput()
953
    {
954
        $values = $this->getFormValues();
955
956
        return $values->all();
957
    }
958
959
    /**
960
     * Get the form data.
961
     *
962
     * @return \Anomaly\Streams\Platform\Support\Collection
963
     */
964
    public function getFormData()
965
    {
966
        return $this->form->getData();
967
    }
968
969
    /**
970
     * Add form data.
971
     *
972
     * @param $key
973
     * @param $value
974
     * @return $this
975
     */
976
    public function addFormData($key, $value)
977
    {
978
        $this->form->addData($key, $value);
979
980
        return $this;
981
    }
982
983
    /**
984
     * Ge the form response.
985
     *
986
     * @return null|Response
987
     */
988
    public function getFormResponse()
989
    {
990
        return $this->form->getResponse();
991
    }
992
993
    /**
994
     * Set the form response.
995
     *
996
     * @param  null|false|Response $response
997
     * @return $this
998
     */
999
    public function setFormResponse(Response $response)
1000
    {
1001
        $this->form->setResponse($response);
1002
1003
        return $this;
1004
    }
1005
1006
    /**
1007
     * Get the form content.
1008
     *
1009
     * @return null|string
1010
     */
1011
    public function getFormContent()
1012
    {
1013
        return $this->form->getContent();
1014
    }
1015
1016
    /**
1017
     * Get the form fields.
1018
     *
1019
     * @return Component\Field\FieldCollection
1020
     */
1021
    public function getFormFields()
1022
    {
1023
        return $this->form->getFields();
1024
    }
1025
1026
    /**
1027
     * Get the enabled form fields.
1028
     *
1029
     * @return Component\Field\FieldCollection
1030
     */
1031
    public function getEnabledFormFields()
1032
    {
1033
        return $this->form->getEnabledFields();
1034
    }
1035
1036
    /**
1037
     * Get the form field.
1038
     *
1039
     * @param $fieldSlug
1040
     * @return FieldType
1041
     */
1042
    public function getFormField($fieldSlug)
1043
    {
1044
        return $this->form->getField($fieldSlug);
1045
    }
1046
1047
    /**
1048
     * Get the form attribute map.
1049
     *
1050
     * @return FieldType
1051
     */
1052
    public function getFormFieldFromAttribute($attribute)
1053
    {
1054
        /* @var FieldType $field */
1055
        foreach ($this->form->getFields() as $field) {
1056
            if ($field->getInputName() == $attribute) {
1057
                return $field;
1058
            }
1059
        }
1060
1061
        return null;
1062
    }
1063
1064
    /**
1065
     * Disable a form field.
1066
     *
1067
     * @param $fieldSlug
1068
     * @return FieldType
1069
     */
1070
    public function disableFormField($fieldSlug)
1071
    {
1072
        return $this->form->disableField($fieldSlug);
1073
    }
1074
1075
    /**
1076
     * Get the form field slugs.
1077
     *
1078
     * @param  null $prefix
1079
     * @return array
1080
     */
1081
    public function getFormFieldSlugs($prefix = null)
1082
    {
1083
        $fields = $this->form->getFields();
1084
1085
        return array_map(
1086
            function ($slug) use ($prefix) {
1087
                return $prefix . $slug;
1088
            },
1089
            array_unique($fields->pluck('field')->all())
1090
        );
1091
    }
1092
1093
    /**
1094
     * Get the form field names.
1095
     *
1096
     * @return array
1097
     */
1098
    public function getFormFieldNames()
1099
    {
1100
        $fields = $this->form->getFields();
1101
1102
        return $fields->pluck('field_name')->all();
1103
    }
1104
1105
    /**
1106
     * Add a form field.
1107
     *
1108
     * @param  FieldType $field
1109
     * @return $this
1110
     */
1111
    public function addFormField(FieldType $field)
1112
    {
1113
        $this->form->addField($field);
1114
1115
        return $this;
1116
    }
1117
1118
    /**
1119
     * Set the form errors.
1120
     *
1121
     * @param  MessageBag $errors
1122
     * @return $this
1123
     */
1124
    public function setFormErrors(MessageBag $errors)
1125
    {
1126
        $this->form->setErrors($errors);
0 ignored issues
show
Compatibility introduced by
$errors of type object<Illuminate\Contracts\Support\MessageBag> is not a sub-type of object<Illuminate\Support\MessageBag>. It seems like you assume a concrete implementation of the interface Illuminate\Contracts\Support\MessageBag to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
1127
1128
        return $this;
1129
    }
1130
1131
    /**
1132
     * Get the form errors.
1133
     *
1134
     * @return null|MessageBag
1135
     */
1136
    public function getFormErrors()
1137
    {
1138
        return $this->form->getErrors();
1139
    }
1140
1141
    /**
1142
     * Add an error to the form.
1143
     *
1144
     * @param $field
1145
     * @param $message
1146
     * @return $this
1147
     */
1148
    public function addFormError($field, $message)
1149
    {
1150
        $errors = $this->getFormErrors();
1151
1152
        $errors->add($field, $message);
1153
1154
        return $this;
1155
    }
1156
1157
    /**
1158
     * Return whether the form has errors or not.
1159
     *
1160
     * @return bool
1161
     */
1162
    public function hasFormErrors()
1163
    {
1164
        $errors = $this->form->getErrors();
1165
1166
        return !$errors->isEmpty();
1167
    }
1168
1169
    /**
1170
     * Return whether the field has an error or not.
1171
     *
1172
     * @param $fieldName
1173
     * @return bool
1174
     */
1175
    public function hasFormError($fieldName)
1176
    {
1177
        return $this->form->hasError($fieldName);
1178
    }
1179
1180
    /**
1181
     * Get the form actions.
1182
     *
1183
     * @return ActionCollection
1184
     */
1185
    public function getFormActions()
1186
    {
1187
        return $this->form->getActions();
1188
    }
1189
1190
    /**
1191
     * Get the active form action.
1192
     *
1193
     * @return null|ActionInterface
1194
     */
1195
    public function getActiveFormAction()
1196
    {
1197
        if (!$actions = $this->form->getActions()) {
1198
            return null;
1199
        }
1200
1201
        if (!$active = $actions->active()) {
1202
            return null;
1203
        }
1204
1205
        return $active;
1206
    }
1207
1208
    /**
1209
     * Add a form button.
1210
     *
1211
     * @param  ButtonInterface $button
1212
     * @return $this
1213
     */
1214
    public function addFormButton(ButtonInterface $button)
1215
    {
1216
        $this->form->addButton($button);
1217
1218
        return $this;
1219
    }
1220
1221
    /**
1222
     * Add a form section.
1223
     *
1224
     * @param        $slug
1225
     * @param  array $section
1226
     * @return $this
1227
     */
1228
    public function addFormSection($slug, array $section)
1229
    {
1230
        $this->form->addSection($slug, $section);
1231
1232
        return $this;
1233
    }
1234
1235
    /**
1236
     * Set the form entry.
1237
     *
1238
     * @param $entry
1239
     * @return $this
1240
     */
1241
    public function setFormEntry($entry)
1242
    {
1243
        $this->form->setEntry($entry);
1244
1245
        return $this;
1246
    }
1247
1248
    /**
1249
     * Get a request value.
1250
     *
1251
     * @param        $key
1252
     * @param  null  $default
1253
     * @return mixed
1254
     */
1255
    public function getRequestValue($key, $default = null)
0 ignored issues
show
Coding Style introduced by
getRequestValue uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
1256
    {
1257
        return array_get($_REQUEST, $this->getOption('prefix') . $key, $default);
1258
    }
1259
1260
    /**
1261
     * Get a post value.
1262
     *
1263
     * @param        $key
1264
     * @param  null  $default
1265
     * @return mixed
1266
     */
1267
    public function getPostValue($key, $default = null)
0 ignored issues
show
Coding Style introduced by
getPostValue uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
1268
    {
1269
        return array_get($_POST, $this->getOption('prefix') . $key, $default);
1270
    }
1271
1272
    /**
1273
     * Return a post key flag.
1274
     *
1275
     * @param        $key
1276
     * @param  null  $default
0 ignored issues
show
Bug introduced by
There is no parameter named $default. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1277
     * @return mixed
1278
     */
1279
    public function hasPostedInput($key)
0 ignored issues
show
Coding Style introduced by
hasPostedInput uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
1280
    {
1281
        return isset($_POST[$this->getOption('prefix') . $key]);
1282
    }
1283
1284
    /**
1285
     * Return whether any post data exists.
1286
     *
1287
     * @return bool
1288
     */
1289
    public function hasPostData()
1290
    {
1291
        /* @var FieldType $field */
1292
        foreach ($this->getFormFields() as $field) {
1293
            if ($field->hasPostedInput()) {
1294
                return true;
1295
            }
1296
        }
1297
1298
        return false;
1299
    }
1300
1301
    /**
1302
     * Set the save flag.
1303
     *
1304
     * @param  bool $save
1305
     * @return $this
1306
     */
1307
    public function setSave($save)
1308
    {
1309
        $this->save = $save;
1310
1311
        return $this;
1312
    }
1313
1314
    /**
1315
     * Return the save flag.
1316
     *
1317
     * @return bool
1318
     */
1319
    public function canSave()
1320
    {
1321
        return $this->save;
1322
    }
1323
1324
    /**
1325
     * Set the read only flag.
1326
     *
1327
     * @param $readOnly
1328
     * @return $this
1329
     */
1330
    public function setReadOnly($readOnly)
1331
    {
1332
        $this->readOnly = $readOnly;
1333
1334
        return $this;
1335
    }
1336
1337
    /**
1338
     * Return the read only flag.
1339
     *
1340
     * @return bool
1341
     */
1342
    public function isReadOnly()
1343
    {
1344
        return $this->readOnly;
1345
    }
1346
}
1347