Completed
Push — master ( 18deeb...2b3f1d )
by Ryan
08:09
created

FormBuilder::getFormPresenter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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