Completed
Push — master ( e7b79c...402727 )
by Ryan
09:29
created

FormBuilder::addFormSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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\MakeForm;
18
use Anomaly\Streams\Platform\Ui\Form\Command\PopulateFields;
19
use Anomaly\Streams\Platform\Ui\Form\Command\PostForm;
20
use Anomaly\Streams\Platform\Ui\Form\Command\SaveForm;
21
use Anomaly\Streams\Platform\Ui\Form\Command\SetFormResponse;
22
use Anomaly\Streams\Platform\Ui\Form\Component\Action\ActionCollection;
23
use Anomaly\Streams\Platform\Ui\Form\Contract\FormRepositoryInterface;
24
use Closure;
25
use Illuminate\Foundation\Bus\DispatchesJobs;
26
use Illuminate\Contracts\Support\MessageBag;
27
use Symfony\Component\HttpFoundation\Response;
28
29
/**
30
 * Class FormBuilder
31
 *
32
 * @link    http://anomaly.is/streams-platform
33
 * @author  AnomalyLabs, Inc. <[email protected]>
34
 * @author  Ryan Thompson <[email protected]>
35
 * @package Anomaly\Streams\Platform\Ui\Form
36
 */
37
class FormBuilder
38
{
39
40
    use DispatchesJobs;
41
    use FiresCallbacks;
42
43
    /**
44
     * The ajax flag.
45
     *
46
     * @var bool
47
     */
48
    protected $ajax = false;
49
50
    /**
51
     * The form handler.
52
     *
53
     * @var null|string
54
     */
55
    protected $handler = null;
56
57
    /**
58
     * The form validator.
59
     *
60
     * @var null|string
61
     */
62
    protected $validator = null;
63
64
    /**
65
     * The form repository.
66
     *
67
     * @var null|FormRepositoryInterface
68
     */
69
    protected $repository = null;
70
71
    /**
72
     * The form model.
73
     *
74
     * @var null
75
     */
76
    protected $model = null;
77
78
    /**
79
     * The entry object.
80
     *
81
     * @var null|int
82
     */
83
    protected $entry = null;
84
85
    /**
86
     * The fields config.
87
     *
88
     * @var array|string
89
     */
90
    protected $fields = [];
91
92
    /**
93
     * Fields to skip.
94
     *
95
     * @var array|string
96
     */
97
    protected $skips = [];
98
99
    /**
100
     * The actions config.
101
     *
102
     * @var array|string
103
     */
104
    protected $actions = [];
105
106
    /**
107
     * The buttons config.
108
     *
109
     * @var array|string
110
     */
111
    protected $buttons = [];
112
113
    /**
114
     * The form options.
115
     *
116
     * @var array
117
     */
118
    protected $options = [];
119
120
    /**
121
     * The form sections.
122
     *
123
     * @var array
124
     */
125
    protected $sections = [];
126
127
    /**
128
     * The form assets.
129
     *
130
     * @var array
131
     */
132
    protected $assets = [];
133
134
    /**
135
     * The save flag.
136
     *
137
     * @var bool
138
     */
139
    protected $save = true;
140
141
    /**
142
     * The read only flag.
143
     *
144
     * @var bool
145
     */
146
    protected $readOnly = false;
147
148
    /**
149
     * The form object.
150
     *
151
     * @var Form
152
     */
153
    protected $form;
154
155
    /**
156
     * Crate a new FormBuilder instance.
157
     *
158
     * @param Form $form
159
     */
160
    public function __construct(Form $form)
161
    {
162
        $this->form = $form;
163
    }
164
165
    /**
166
     * Build the form.
167
     *
168
     * @param null $entry
169
     * @return $this
170
     */
171
    public function build($entry = null)
172
    {
173
        if ($entry) {
174
            $this->entry = $entry;
175
        }
176
177
        $this->fire('ready', ['builder' => $this]);
178
179
        $this->dispatch(new BuildForm($this));
180
181
        $this->fire('built', ['builder' => $this]);
182
183
        return $this;
184
    }
185
186
    /**
187
     * Make the form.
188
     *
189
     * @param null $entry
190
     * @return $this
191
     */
192
    public function make($entry = null)
193
    {
194
        $this->build($entry);
195
        $this->post();
196
197
        $this->fire('make', ['builder' => $this]);
198
199
        if ($this->getFormResponse() === null) {
200
            $this->dispatch(new LoadForm($this));
201
            $this->dispatch(new AddAssets($this));
202
            $this->dispatch(new MakeForm($this));
203
        }
204
205
        return $this;
206
    }
207
208
    /**
209
     * Handle the form post.
210
     *
211
     * @param null $entry
212
     * @throws \Exception
213
     */
214
    public function handle($entry = null)
215
    {
216
        if (!app('request')->isMethod('post')) {
217
            throw new \Exception('The handle method must be used with a POST request.');
218
        }
219
220
        $this->build($entry);
221
        $this->post();
222
    }
223
224
    /**
225
     * Trigger post operations
226
     * for the form.
227
     *
228
     * @return $this
229
     */
230
    public function post()
231
    {
232
        if (app('request')->isMethod('post')) {
233
234
            $this->fire('post', ['builder' => $this]);
235
236
            if ($this->hasPostData()) {
237
                $this->dispatch(new PostForm($this));
238
            }
239
        } else {
240
            $this->dispatch(new PopulateFields($this));
241
        }
242
243
        return $this;
244
    }
245
246
    /**
247
     * Flash form information to be
248
     * used in conjunction with redirect
249
     * type responses (not self handling).
250
     */
251
    public function flash()
252
    {
253
        $this->dispatch(new FlashFieldValues($this));
254
        $this->dispatch(new FlashFormErrors($this));
255
    }
256
257
    /**
258
     * Render the form.
259
     *
260
     * @param  null $entry
261
     * @return Response
262
     */
263
    public function render($entry = null)
264
    {
265
        $this->make($entry);
266
267
        if (!$this->form->getResponse()) {
268
            $this->dispatch(new SetFormResponse($this));
269
        }
270
271
        return $this->form->getResponse();
272
    }
273
274
    /**
275
     * Fire field events.
276
     *
277
     * @param       $trigger
278
     * @param array $payload
279
     */
280
    public function fireFieldEvents($trigger, array $payload = [])
281
    {
282
        /* @var FieldType $field */
283
        foreach ($this->getFormFields() as $field) {
284
            $field->fire($trigger, array_merge(['builder' => $this], $payload));
285
        }
286
    }
287
288
    /**
289
     * Save the form.
290
     */
291
    public function saveForm()
292
    {
293
        $this->dispatch(new SaveForm($this));
294
    }
295
296
    /**
297
     * Get the form object.
298
     *
299
     * @return Form
300
     */
301
    public function getForm()
302
    {
303
        return $this->form;
304
    }
305
306
    /**
307
     * Get the form presenter.
308
     *
309
     * @return FormPresenter
310
     */
311
    public function getFormPresenter()
312
    {
313
        return $this->form->getPresenter();
314
    }
315
316
    /**
317
     * Get the ajax flag.
318
     *
319
     * @return bool
320
     */
321
    public function isAjax()
322
    {
323
        return $this->ajax;
324
    }
325
326
    /**
327
     * Set the ajax flag.
328
     *
329
     * @param $ajax
330
     * @return $this
331
     */
332
    public function setAjax($ajax)
333
    {
334
        $this->ajax = $ajax;
335
336
        return $this;
337
    }
338
339
    /**
340
     * Get the handler.
341
     *
342
     * @return null|string
343
     */
344
    public function getHandler()
345
    {
346
        return $this->handler;
347
    }
348
349
    /**
350
     * Set the handler.
351
     *
352
     * @param $handler
353
     * @return $this
354
     */
355
    public function setHandler($handler)
356
    {
357
        $this->handler = $handler;
358
359
        return $this;
360
    }
361
362
    /**
363
     * Get the validator.
364
     *
365
     * @return null|string
366
     */
367
    public function getValidator()
368
    {
369
        return $this->validator;
370
    }
371
372
    /**
373
     * Set the validator.
374
     *
375
     * @param $validator
376
     * @return $this
377
     */
378
    public function setValidator($validator)
379
    {
380
        $this->validator = $validator;
381
382
        return $this;
383
    }
384
385
    /**
386
     * Get the repository.
387
     *
388
     * @return FormRepositoryInterface|null
389
     */
390
    public function getRepository()
391
    {
392
        return $this->repository;
393
    }
394
395
    /**
396
     * Set the form repository.
397
     *
398
     * @param FormRepositoryInterface $repository
399
     * @return $this
400
     */
401
    public function setRepository(FormRepositoryInterface $repository)
402
    {
403
        $this->repository = $repository;
404
405
        return $this;
406
    }
407
408
    /**
409
     * Set the form model.
410
     *
411
     * @param  $model
412
     * @return $this
413
     */
414
    public function setModel($model)
415
    {
416
        $this->model = $model;
417
418
        return $this;
419
    }
420
421
    /**
422
     * Get the form model.
423
     *
424
     * @return null
425
     */
426
    public function getModel()
427
    {
428
        return $this->model;
429
    }
430
431
    /**
432
     * Set the entry object.
433
     *
434
     * @param  $entry
435
     * @return $this
436
     */
437
    public function setEntry($entry)
438
    {
439
        $this->entry = $entry;
440
441
        return $this;
442
    }
443
444
    /**
445
     * Get the entry object.
446
     *
447
     * @return null|EntryInterface|FieldInterface|mixed
448
     */
449
    public function getEntry()
450
    {
451
        return $this->entry;
452
    }
453
454
    /**
455
     * Set the fields.
456
     *
457
     * @param  $fields
458
     * @return $this
459
     */
460
    public function setFields($fields)
461
    {
462
        $this->fields = $fields;
463
464
        return $this;
465
    }
466
467
    /**
468
     * Get the fields.
469
     *
470
     * @return array
471
     */
472
    public function getFields()
473
    {
474
        return $this->fields;
475
    }
476
477
    /**
478
     * Add a field.
479
     *
480
     * @param      $field
481
     */
482
    public function addField($field)
483
    {
484
        $this->fields[array_get($field, 'field')] = $field;
485
    }
486
487
    /**
488
     * Add fields.
489
     *
490
     * @param array $fields
491
     */
492
    public function addFields(array $fields)
493
    {
494
        $this->fields = array_merge($this->fields, $fields);
495
    }
496
497
    /**
498
     * Get the skipped fields.
499
     *
500
     * @return array
501
     */
502
    public function getSkips()
503
    {
504
        return $this->skips;
505
    }
506
507
    /**
508
     * Set the skipped fields.
509
     *
510
     * @param $skips
511
     * @return $this
512
     */
513
    public function setSkips($skips)
514
    {
515
        $this->skips = $skips;
516
517
        return $this;
518
    }
519
520
    /**
521
     * Add a skipped field.
522
     *
523
     * @param $fieldSlug
524
     * @return $this
525
     */
526
    public function skipField($fieldSlug)
527
    {
528
        $this->skips[] = $fieldSlug;
529
530
        return $this;
531
    }
532
533
    /**
534
     * Set the actions config.
535
     *
536
     * @param  $actions
537
     * @return $this
538
     */
539
    public function setActions($actions)
540
    {
541
        $this->actions = $actions;
542
543
        return $this;
544
    }
545
546
    /**
547
     * Add an action.
548
     *
549
     * @param       $slug
550
     * @param array $definition
551
     * @return $this
552
     */
553
    public function addAction($slug, array $definition = [])
554
    {
555
        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...
556
            $this->actions[$slug] = $definition;
557
        } else {
558
            $this->actions[] = $slug;
559
        }
560
561
        return $this;
562
    }
563
564
    /**
565
     * Get the actions config.
566
     *
567
     * @return array
568
     */
569
    public function getActions()
570
    {
571
        return $this->actions;
572
    }
573
574
    /**
575
     * Set the buttons config.
576
     *
577
     * @param  $buttons
578
     * @return $this
579
     */
580
    public function setButtons($buttons)
581
    {
582
        $this->buttons = $buttons;
583
584
        return $this;
585
    }
586
587
    /**
588
     * Get the buttons config.
589
     *
590
     * @return array
591
     */
592
    public function getButtons()
593
    {
594
        return $this->buttons;
595
    }
596
597
    /**
598
     * The the options.
599
     *
600
     * @return array
601
     */
602
    public function getOptions()
603
    {
604
        return $this->options;
605
    }
606
607
    /**
608
     * Set the options.
609
     *
610
     * @param array|string $options
611
     * @return $this
612
     */
613
    public function setOptions($options)
614
    {
615
        $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...
616
617
        return $this;
618
    }
619
620
    /**
621
     * Merge in options.
622
     *
623
     * @param array|string $options
624
     * @return $this
625
     */
626
    public function mergeOptions($options)
627
    {
628
        $this->options = array_merge($this->options, $options);
629
630
        return $this;
631
    }
632
633
    /**
634
     * Get the sections.
635
     *
636
     * @return array
637
     */
638
    public function getSections()
639
    {
640
        return $this->sections;
641
    }
642
643
    /**
644
     * Set the sections.
645
     *
646
     * @param array|Closure $sections
647
     * @return $this
648
     */
649
    public function setSections($sections)
650
    {
651
        $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...
652
653
        return $this;
654
    }
655
656
    /**
657
     * Add a section.
658
     *
659
     * @param       $slug
660
     * @param array $section
661
     * @return $this
662
     */
663
    public function addSection($slug, array $section)
664
    {
665
        array_set($this->sections, $slug, $section);
666
667
        return $this;
668
    }
669
670
    /**
671
     * Add a section tab.
672
     *
673
     * @param       $section
674
     * @param       $slug
675
     * @param array $tab
676
     * @return $this
677
     */
678
    public function addSectionTab($section, $slug, array $tab)
679
    {
680
        array_set($this->sections, "{$section}.tabs.{$slug}", $tab);
681
682
        return $this;
683
    }
684
685
    /**
686
     * Get an option value.
687
     *
688
     * @param      $key
689
     * @param null $default
690
     * @return mixed
691
     */
692
    public function getOption($key, $default = null)
693
    {
694
        return array_get($this->options, $key, $default);
695
    }
696
697
    /**
698
     * Set an option value.
699
     *
700
     * @param $key
701
     * @param $value
702
     * @return $this
703
     */
704
    public function setOption($key, $value)
705
    {
706
        array_set($this->options, $key, $value);
707
708
        return $this;
709
    }
710
711
    /**
712
     * Get the assets.
713
     *
714
     * @return array
715
     */
716
    public function getAssets()
717
    {
718
        return $this->assets;
719
    }
720
721
    /**
722
     * Set the assets.
723
     *
724
     * @param $assets
725
     * @return $this
726
     */
727
    public function setAssets($assets)
728
    {
729
        $this->assets = $assets;
730
731
        return $this;
732
    }
733
734
    /**
735
     * Add an asset.
736
     *
737
     * @param $collection
738
     * @param $asset
739
     * @return $this
740
     */
741 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...
742
    {
743
        if (!isset($this->assets[$collection])) {
744
            $this->assets[$collection] = [];
745
        }
746
747
        $this->assets[$collection][] = $asset;
748
749
        return $this;
750
    }
751
752
    /**
753
     * Get the form's stream.
754
     *
755
     * @return StreamInterface|null
756
     */
757
    public function getFormStream()
758
    {
759
        return $this->form->getStream();
760
    }
761
762
    /**
763
     * Get a form option value.
764
     *
765
     * @param      $key
766
     * @param null $default
767
     * @return mixed
768
     */
769
    public function getFormOption($key, $default = null)
770
    {
771
        return $this->form->getOption($key, $default);
772
    }
773
774
    /**
775
     * Set a form option value.
776
     *
777
     * @param $key
778
     * @param $value
779
     * @return $this
780
     */
781
    public function setFormOption($key, $value)
782
    {
783
        $this->form->setOption($key, $value);
784
785
        return $this;
786
    }
787
788
    /**
789
     * Get the form options.
790
     *
791
     * @return \Anomaly\Streams\Platform\Support\Collection
792
     */
793
    public function getFormOptions()
794
    {
795
        return $this->form->getOptions();
796
    }
797
798
    /**
799
     * Get the form model.
800
     *
801
     * @return \Anomaly\Streams\Platform\Entry\EntryModel|EloquentModel|null
802
     */
803
    public function getFormModel()
804
    {
805
        return $this->form->getModel();
806
    }
807
808
    /**
809
     * Get the form entry.
810
     *
811
     * @return EloquentModel|EntryInterface|FieldInterface|AssignmentInterface
812
     */
813
    public function getFormEntry()
814
    {
815
        return $this->form->getEntry();
816
    }
817
818
    /**
819
     * Return the form entry's ID.
820
     *
821
     * @return int|mixed|null
822
     */
823
    public function getFormEntryId()
824
    {
825
        $entry = $this->getFormEntry();
826
827
        if (!$entry instanceof EloquentModel) {
828
            return null;
829
        }
830
831
        return $entry->getId();
832
    }
833
834
    /**
835
     * Get the contextual entry ID.
836
     *
837
     * @return int|null
838
     */
839
    public function getContextualId()
840
    {
841
        return $this->getFormEntryId();
842
    }
843
844
    /**
845
     * Get the form mode.
846
     *
847
     * @return null|string
848
     */
849
    public function getFormMode()
850
    {
851
        return $this->form->getMode();
852
    }
853
854
    /**
855
     * Set the form mode.
856
     *
857
     * @param $mode
858
     * @return $this
859
     */
860
    public function setFormMode($mode)
861
    {
862
        $this->form->setMode($mode);
863
864
        return $this;
865
    }
866
867
    /**
868
     * Get a form value.
869
     *
870
     * @param      $key
871
     * @param null $default
872
     * @return mixed
873
     */
874
    public function getFormValue($key, $default = null)
875
    {
876
        return $this->form->getValue($key, $default);
877
    }
878
879
    /**
880
     * Set a form value.
881
     *
882
     * @param $key
883
     * @param $value
884
     * @return $this
885
     */
886
    public function setFormValue($key, $value)
887
    {
888
        $this->form->setValue($key, $value);
889
890
        return $this;
891
    }
892
893
    /**
894
     * Get the form values.
895
     *
896
     * @return \Anomaly\Streams\Platform\Support\Collection
897
     */
898
    public function getFormValues()
899
    {
900
        return $this->form->getValues();
901
    }
902
903
    /**
904
     * Reset the form.
905
     *
906
     * @return $this
907
     */
908
    public function resetForm()
909
    {
910
        $this->form
911
            ->resetFields()
912
            ->setValues(new Collection());
913
914
        return $this;
915
    }
916
917
    /**
918
     * Get the form input.
919
     *
920
     * @return array
921
     */
922
    public function getFormInput()
923
    {
924
        $values = $this->getFormValues();
925
926
        return $values->all();
927
    }
928
929
    /**
930
     * Get the form data.
931
     *
932
     * @return \Anomaly\Streams\Platform\Support\Collection
933
     */
934
    public function getFormData()
935
    {
936
        return $this->form->getData();
937
    }
938
939
    /**
940
     * Ge the form response.
941
     *
942
     * @return null|Response
943
     */
944
    public function getFormResponse()
945
    {
946
        return $this->form->getResponse();
947
    }
948
949
    /**
950
     * Set the form response.
951
     *
952
     * @param null|false|Response $response
953
     * @return $this
954
     */
955
    public function setFormResponse(Response $response)
956
    {
957
        $this->form->setResponse($response);
958
959
        return $this;
960
    }
961
962
    /**
963
     * Get the form content.
964
     *
965
     * @return null|string
966
     */
967
    public function getFormContent()
968
    {
969
        return $this->form->getContent();
970
    }
971
972
    /**
973
     * Get the form fields.
974
     *
975
     * @return Component\Field\FieldCollection
976
     */
977
    public function getFormFields()
978
    {
979
        return $this->form->getFields();
980
    }
981
982
    /**
983
     * Get the enabled form fields.
984
     *
985
     * @return Component\Field\FieldCollection
986
     */
987
    public function getEnabledFormFields()
988
    {
989
        return $this->form->getEnabledFields();
990
    }
991
992
    /**
993
     * Get the form field.
994
     *
995
     * @param $fieldSlug
996
     * @return FieldType
997
     */
998
    public function getFormField($fieldSlug)
999
    {
1000
        return $this->form->getField($fieldSlug);
1001
    }
1002
1003
    /**
1004
     * Get the form attribute map.
1005
     *
1006
     * @return FieldType
1007
     */
1008
    public function getFormFieldFromAttribute($attribute)
1009
    {
1010
        /* @var FieldType $field */
1011
        foreach ($this->form->getFields() as $field) {
1012
            if ($field->getInputName() == $attribute) {
1013
                return $field;
1014
            }
1015
        }
1016
1017
        return null;
1018
    }
1019
1020
    /**
1021
     * Disable a form field.
1022
     *
1023
     * @param $fieldSlug
1024
     * @return FieldType
1025
     */
1026
    public function disableFormField($fieldSlug)
1027
    {
1028
        return $this->form->disableField($fieldSlug);
1029
    }
1030
1031
    /**
1032
     * Get the form field slugs.
1033
     *
1034
     * @param null $prefix
1035
     * @return array
1036
     */
1037
    public function getFormFieldSlugs($prefix = null)
1038
    {
1039
        $fields = $this->form->getFields();
1040
1041
        return array_map(
1042
            function ($slug) use ($prefix) {
1043
                return $prefix . $slug;
1044
            },
1045
            array_unique($fields->lists('field')->all())
1046
        );
1047
    }
1048
1049
    /**
1050
     * Get the form field names.
1051
     *
1052
     * @return array
1053
     */
1054
    public function getFormFieldNames()
1055
    {
1056
        $fields = $this->form->getFields();
1057
1058
        return $fields->lists('field_name')->all();
1059
    }
1060
1061
    /**
1062
     * Add a form field.
1063
     *
1064
     * @param FieldType $field
1065
     * @return $this
1066
     */
1067
    public function addFormField(FieldType $field)
1068
    {
1069
        $this->form->addField($field);
1070
1071
        return $this;
1072
    }
1073
1074
    /**
1075
     * Set the form errors.
1076
     *
1077
     * @param MessageBag $errors
1078
     * @return $this
1079
     */
1080
    public function setFormErrors(MessageBag $errors)
1081
    {
1082
        $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...
1083
1084
        return $this;
1085
    }
1086
1087
    /**
1088
     * Get the form errors.
1089
     *
1090
     * @return null|MessageBag
1091
     */
1092
    public function getFormErrors()
1093
    {
1094
        return $this->form->getErrors();
1095
    }
1096
1097
    /**
1098
     * Add an error to the form.
1099
     *
1100
     * @param $field
1101
     * @param $message
1102
     * @return $this
1103
     */
1104
    public function addFormError($field, $message)
1105
    {
1106
        $errors = $this->getFormErrors();
1107
1108
        $errors->add($field, $message);
1109
1110
        return $this;
1111
    }
1112
1113
    /**
1114
     * Return whether the form has errors or not.
1115
     *
1116
     * @return bool
1117
     */
1118
    public function hasFormErrors()
1119
    {
1120
        $errors = $this->form->getErrors();
1121
1122
        return !$errors->isEmpty();
1123
    }
1124
1125
    /**
1126
     * Return whether the field has an error or not.
1127
     *
1128
     * @param $fieldName
1129
     * @return bool
1130
     */
1131
    public function hasFormError($fieldName)
1132
    {
1133
        return $this->form->hasError($fieldName);
1134
    }
1135
1136
    /**
1137
     * Get the form actions.
1138
     *
1139
     * @return ActionCollection
1140
     */
1141
    public function getFormActions()
1142
    {
1143
        return $this->form->getActions();
1144
    }
1145
1146
    /**
1147
     * Add a form button.
1148
     *
1149
     * @param ButtonInterface $button
1150
     * @return $this
1151
     */
1152
    public function addFormButton(ButtonInterface $button)
1153
    {
1154
        $this->form->addButton($button);
1155
1156
        return $this;
1157
    }
1158
1159
    /**
1160
     * Add a form section.
1161
     *
1162
     * @param       $slug
1163
     * @param array $section
1164
     * @return $this
1165
     */
1166
    public function addFormSection($slug, array $section)
1167
    {
1168
        $this->form->addSection($slug, $section);
1169
1170
        return $this;
1171
    }
1172
1173
    /**
1174
     * Set the form entry.
1175
     *
1176
     * @param $entry
1177
     * @return $this
1178
     */
1179
    public function setFormEntry($entry)
1180
    {
1181
        $this->form->setEntry($entry);
1182
1183
        return $this;
1184
    }
1185
1186
    /**
1187
     * Get a request value.
1188
     *
1189
     * @param      $key
1190
     * @param null $default
1191
     * @return mixed
1192
     */
1193
    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...
1194
    {
1195
        return array_get($_REQUEST, $this->getOption('prefix') . $key, $default);
1196
    }
1197
1198
    /**
1199
     * Get a post value.
1200
     *
1201
     * @param      $key
1202
     * @param null $default
1203
     * @return mixed
1204
     */
1205
    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...
1206
    {
1207
        return array_get($_POST, $this->getOption('prefix') . $key, $default);
1208
    }
1209
1210
    /**
1211
     * Return a post key flag.
1212
     *
1213
     * @param      $key
1214
     * @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...
1215
     * @return mixed
1216
     */
1217
    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...
1218
    {
1219
        return isset($_POST[$this->getOption('prefix') . $key]);
1220
    }
1221
1222
    /**
1223
     * Return whether any post data exists.
1224
     *
1225
     * @return bool
1226
     */
1227
    public function hasPostData()
1228
    {
1229
        /* @var FieldType $field */
1230
        foreach ($this->getFormFields() as $field) {
1231
            if ($field->hasPostedInput()) {
1232
                return true;
1233
            }
1234
        }
1235
1236
        return false;
1237
    }
1238
1239
    /**
1240
     * Set the save flag.
1241
     *
1242
     * @param bool $save
1243
     * @return $this
1244
     */
1245
    public function setSave($save)
1246
    {
1247
        $this->save = $save;
1248
1249
        return $this;
1250
    }
1251
1252
    /**
1253
     * Return the save flag.
1254
     *
1255
     * @return bool
1256
     */
1257
    public function canSave()
1258
    {
1259
        return $this->save;
1260
    }
1261
1262
    /**
1263
     * Set the read only flag.
1264
     *
1265
     * @param $readOnly
1266
     * @return $this
1267
     */
1268
    public function setReadOnly($readOnly)
1269
    {
1270
        $this->readOnly = $readOnly;
1271
1272
        return $this;
1273
    }
1274
1275
    /**
1276
     * Return the read only flag.
1277
     *
1278
     * @return bool
1279
     */
1280
    public function isReadOnly()
1281
    {
1282
        return $this->readOnly;
1283
    }
1284
}
1285