FormBuilder::setFormValue()   A
last analyzed

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