Completed
Branch master (dc960a)
by Alfred
03:29 queued 01:48
created
PhpFormBuilder.php 1 patch
Braces   +100 added lines, -57 removed lines patch added patch discarded remove patch
@@ -44,13 +44,13 @@  discard block
 block discarded – undo
44 44
     public function __construct($method = 'post'){
45 45
         try{
46 46
             $method = strtolower(trim($method));
47
-            if(!in_array($method,['get','post'],true))
48
-                $method = $_SERVER['REQUEST_METHOD'];
47
+            if(!in_array($method,['get','post'],true)) {
48
+                            $method = $_SERVER['REQUEST_METHOD'];
49
+            }
49 50
             $this->RequestMethod = $method;
50 51
             $this->OptionResolver= new PhpOptionsResolver();
51 52
             return $this;
52
-        }
53
-        catch(\Exception $e){echo $e->getMessage();}
53
+        } catch(\Exception $e){echo $e->getMessage();}
54 54
         return false;
55 55
     }
56 56
     /* COMMON METHODS */
@@ -74,7 +74,9 @@  discard block
 block discarded – undo
74 74
         $this->OptionResolver->addAllowedTypes('action','string');
75 75
         $this->OptionResolver->addAllowedTypes('name','string');
76 76
         $attributes=$this->OptionResolver->resolve($this->FilterAttributes($attributes,'form'));
77
-        if(strcasecmp($attributes['method'],'get') == 0) unset($attributes['enctype']);
77
+        if(strcasecmp($attributes['method'],'get') == 0) {
78
+          unset($attributes['enctype']);
79
+        }
78 80
         $html  = '<form';
79 81
         $html .= $this->BuildAttributes($attributes);
80 82
         $html .= '>';
@@ -100,8 +102,9 @@  discard block
 block discarded – undo
100 102
         ]);
101 103
         $attributes = $this->OptionResolver->resolve($this->FilterAttributes($attributes,'fieldset'));
102 104
         $html ='<fieldset';
103
-        if($attributes)
104
-            $html.= $this->BuildAttributes($attributes,array('legend','label','id','legend-attributes'));
105
+        if($attributes) {
106
+                    $html.= $this->BuildAttributes($attributes,array('legend','label','id','legend-attributes'));
107
+        }
105 108
         $html .= ' >';
106 109
         if(isset($attributes['legend']))
107 110
         {
@@ -227,8 +230,9 @@  discard block
 block discarded – undo
227 230
         $this->OptionResolver->addAllowedValues('type',['radio','checkbox']);
228 231
         $type = $attributes['type']?:'checkbox';
229 232
         $attributes = $this->OptionResolver->resolve($this->FilterAttributes($attributes,$type));
230
-        if(strpos($attributes['id'],'input')!==0)
231
-            $attributes['id'] = $this->normalize($attributes['id']);
233
+        if(strpos($attributes['id'],'input')!==0) {
234
+                    $attributes['id'] = $this->normalize($attributes['id']);
235
+        }
232 236
 
233 237
         if(sizeof($options)<2)
234 238
         {
@@ -237,17 +241,19 @@  discard block
 block discarded – undo
237 241
             $attributes['type'] = 'checkbox';
238 242
             $this->addHidden($label,array('value'=>'0'));
239 243
             $html = $this->CreateInput($label,$attributes);
240
-        }
241
-        else
244
+        } else
242 245
         {
243 246
             if(isset($attributes['value']))
244 247
             {
245
-                if(!is_array($attributes['value'])) $attributes['value'] = array($attributes['value']);
248
+                if(!is_array($attributes['value'])) {
249
+                  $attributes['value'] = array($attributes['value']);
250
+                }
251
+            } else {
252
+                            $attributes['value'] = [];
253
+            }
254
+            if(substr($attributes['name'], -2) !== '[]') {
255
+                            $attributes['name'] .= '[]';
246 256
             }
247
-            else
248
-                $attributes['value'] = [];
249
-            if(substr($attributes['name'], -2) !== '[]')
250
-                $attributes['name'] .= '[]';
251 257
             $count = 0;
252 258
             $html = $this->InsertLabel($label,array('for'=>$attributes['id']));
253 259
             foreach($options as $value => $text)
@@ -256,7 +262,9 @@  discard block
 block discarded – undo
256 262
                 $html .= '<input id="'.$attributes['id'].'-'.$count.'"';
257 263
                 $html .= ' value="'.$value.'"';
258 264
                 $html .= $this->BuildAttributes($attributes,array('id', 'value', 'checked', 'required'));
259
-                if(in_array($value, $attributes['value'])) $html .= ' checked="checked"';
265
+                if(in_array($value, $attributes['value'])) {
266
+                  $html .= ' checked="checked"';
267
+                }
260 268
                 $html .= ' >'.$text.'</label>';
261 269
                 ++$count;
262 270
             }
@@ -474,21 +482,25 @@  discard block
 block discarded – undo
474 482
         $this->OptionResolver->addAllowedValues('type','select');
475 483
         $attributes = $this->OptionResolver->resolve($this->FilterAttributes($attributes,'select'));
476 484
         $multiple=false;
477
-        if(strpos($attributes['id'],'input')!==0)
478
-            $attributes['id'] = $this->normalize($attributes['id']);
485
+        if(strpos($attributes['id'],'input')!==0) {
486
+                    $attributes['id'] = $this->normalize($attributes['id']);
487
+        }
479 488
         if($attributes['value'])
480 489
         {
481
-            if(!is_array($attributes['value']))
482
-                $attributes['value'] = array($attributes['value']);
490
+            if(!is_array($attributes['value'])) {
491
+                            $attributes['value'] = array($attributes['value']);
492
+            }
493
+        } else {
494
+                    $attributes['value'] = [];
495
+        }
496
+        if(isset($attributes['multiple']) and ($attributes['multiple']==='multiple')) {
497
+                    $multiple=true;
483 498
         }
484
-        else
485
-            $attributes['value'] = [];
486
-        if(isset($attributes['multiple']) and ($attributes['multiple']==='multiple'))
487
-            $multiple=true;
488 499
         if($multiple)
489 500
         {   $attributes['size']= $attributes['size']?:3;
490
-            if(substr($attributes['name'], -2) !== '[]')
491
-                $attributes['name'] .= '[]';
501
+            if(substr($attributes['name'], -2) !== '[]') {
502
+                            $attributes['name'] .= '[]';
503
+            }
492 504
         }
493 505
 
494 506
         $html = $this->InsertLabel($label,array('for'=>$attributes['id']));
@@ -508,8 +520,9 @@  discard block
 block discarded – undo
508 520
                     $output='<optgroup label="'.$groupName.'">';
509 521
                     foreach($groupOptions as $key=>$optLabel)
510 522
                     {
511
-                        if(is_array($optLabel))$output .= $OptGroupHtml($key,$optLabel);
512
-                        else
523
+                        if(is_array($optLabel)) {
524
+                          $output .= $OptGroupHtml($key,$optLabel);
525
+                        } else
513 526
                         {
514 527
                             $output .= '<option value="'.$key.'"';
515 528
                             if(!$selected and in_array($key,$attributes['value']))
@@ -524,8 +537,7 @@  discard block
 block discarded – undo
524 537
                     $output.='</optgroup>';
525 538
                     return$output;
526 539
                 },[$OptionValue,$OptionLabel]);
527
-            }
528
-            else
540
+            } else
529 541
             {
530 542
                 $html .= '<option value="'.$OptionValue.'"';
531 543
                 if(!$selected and in_array($OptionValue,$attributes['value']))
@@ -581,8 +593,9 @@  discard block
 block discarded – undo
581 593
             'name' => $this->normalize($label)
582 594
         ]);
583 595
         $attributes = $this->OptionResolver->resolve($this->FilterAttributes($attributes,'textarea'));
584
-        if(strpos($attributes['id'],'input')!==0)
585
-            $attributes['id'] = $this->normalize($attributes['id']);
596
+        if(strpos($attributes['id'],'input')!==0) {
597
+                    $attributes['id'] = $this->normalize($attributes['id']);
598
+        }
586 599
         $html  = $this->InsertLabel($label,array('for'=>$attributes['id']));
587 600
         $html .= '<textarea'. $this->BuildAttributes($attributes,array('value')).'>';
588 601
         $html .= $attributes['value'];
@@ -665,25 +678,35 @@  discard block
 block discarded – undo
665 678
      * @return bool|null|string
666 679
      */
667 680
     private function BuildAttributes($attributes,$ignore = null){
668
-        if(!$attributes) return false;
681
+        if(!$attributes) {
682
+          return false;
683
+        }
669 684
         is_array($attributes) or $attributes = array($attributes);
670 685
         $filtered = array_keys($attributes);
671 686
         if($ignore)
672 687
         {
673
-            if (!is_array($ignore)) $ignore = array($ignore);
688
+            if (!is_array($ignore)) {
689
+              $ignore = array($ignore);
690
+            }
674 691
             $filtered = array_diff($filtered, $ignore);
675 692
         }
676 693
         $output = null;
677 694
         foreach($attributes as $key=>$value)
678 695
         {
679
-            if(!in_array($key,$filtered)) continue;
696
+            if(!in_array($key,$filtered)) {
697
+              continue;
698
+            }
680 699
             $output .= ' ';
681 700
             $key = $this->normalize($key);
682 701
             if(is_array($value)):
683 702
                 $output .= $key.'="'.implode(' ',array_values($value)).'"';
684
-            else:
703
+            else {
704
+              :
685 705
                 if(is_numeric($key)) $output .= $value;
686
-                else $output .= $key.'="'.$value.'"';
706
+            }
707
+                else {
708
+                  $output .= $key.'="'.$value.'"';
709
+                }
687 710
             endif;
688 711
         }
689 712
         return $output;
@@ -730,8 +753,9 @@  discard block
 block discarded – undo
730 753
         $type = $attributes['type']?:'text';
731 754
         $attributes = $this->OptionResolver->resolve($this->FilterAttributes($attributes,$type));
732 755
         $notLabeled = array('hidden','submit','reset','button');
733
-        if(strpos($attributes['id'],'input')!==0)
734
-            $attributes['id'] = $this->normalize($attributes['id']);
756
+        if(strpos($attributes['id'],'input')!==0) {
757
+                    $attributes['id'] = $this->normalize($attributes['id']);
758
+        }
735 759
         $html  = !in_array($attributes['type'],$notLabeled)?$this->InsertLabel($label,array('for'=>$attributes['id'])):'';
736 760
         $html .= '<input';
737 761
         $html .= $this->BuildAttributes($attributes);
@@ -745,11 +769,15 @@  discard block
 block discarded – undo
745 769
      */
746 770
     private function DisplayError($label)
747 771
     {
748
-        if (!$label) return false;
772
+        if (!$label) {
773
+          return false;
774
+        }
749 775
         $label = $this->normalize($label);
750 776
         $errorMsg = null;
751 777
         !isset($this->ValidationErrors[$label]) OR $errorMsg = $this->ValidationErrors[$label];
752
-        if (!$errorMsg) return '';
778
+        if (!$errorMsg) {
779
+          return '';
780
+        }
753 781
         if (is_array($errorMsg))
754 782
         {
755 783
             $html='';
@@ -758,8 +786,7 @@  discard block
 block discarded – undo
758 786
                 $html .= $error;
759 787
                 $html .= '</span></li>';
760 788
             }
761
-        }
762
-        else
789
+        } else
763 790
         {
764 791
             $html = '<li class="hint"><span class="error">';
765 792
             $html .= $errorMsg;
@@ -786,7 +813,9 @@  discard block
 block discarded – undo
786 813
             case 'number':
787 814
             case 'time':
788 815
             case 'week':
789
-                if($element === 'number') $AcceptedAttributes[]='placeholder';
816
+                if($element === 'number') {
817
+                  $AcceptedAttributes[]='placeholder';
818
+                }
790 819
                 $AcceptedAttributes = array_merge($AcceptedAttributes,$this->AllowedAttributes['date']);
791 820
                 break;
792 821
             case 'color':
@@ -805,7 +834,9 @@  discard block
 block discarded – undo
805 834
                 if($element === 'radio')
806 835
                 {
807 836
                     $bannedIndex=array_search('indeterminate', $tmpAttributesList,true);
808
-                    if(isset($tmpAttributesList[$bannedIndex]))unset($tmpAttributesList[$bannedIndex]);
837
+                    if(isset($tmpAttributesList[$bannedIndex])) {
838
+                      unset($tmpAttributesList[$bannedIndex]);
839
+                    }
809 840
                 }
810 841
                 $AcceptedAttributes = array_merge($AcceptedAttributes,array_values($tmpAttributesList));
811 842
                 break;
@@ -844,7 +875,9 @@  discard block
 block discarded – undo
844 875
                 $AcceptedAttributes = array_merge($AcceptedAttributes,$this->AllowedAttributes['hidden']);
845 876
                 break;
846 877
             default:
847
-                if($element === 'email') $AcceptedAttributes[]='multiple';
878
+                if($element === 'email') {
879
+                  $AcceptedAttributes[]='multiple';
880
+                }
848 881
                 $AcceptedAttributes = array_merge($AcceptedAttributes,$this->AllowedAttributes['text']);
849 882
                 break;
850 883
         endswitch;
@@ -853,10 +886,11 @@  discard block
 block discarded – undo
853 886
         foreach ($attributesKeys as $attribute):
854 887
             !is_numeric($attribute) or $attribute=$attributes[$attribute];
855 888
             if(!in_array($attribute,$AcceptedAttributes)){
856
-                if(substr($attribute,0,5)!=='data-')
857
-                    $AcceptedAttributes[] +=$attribute;
858
-                else
859
-                    unset($attributes[$attribute]);
889
+                if(substr($attribute,0,5)!=='data-') {
890
+                                    $AcceptedAttributes[] +=$attribute;
891
+                } else {
892
+                                    unset($attributes[$attribute]);
893
+                }
860 894
             }
861 895
 
862 896
         endforeach;
@@ -868,10 +902,14 @@  discard block
 block discarded – undo
868 902
      * @return bool|string
869 903
      */
870 904
     private function getDefaultValue($label){
871
-        if(!$label) return false;
905
+        if(!$label) {
906
+          return false;
907
+        }
872 908
         $label = $this->normalize($label);
873 909
         $request = strcasecmp($this->RequestMethod,'get')==0 ? $_GET : $_POST;
874
-        if(!$value = $request[$label]) return '';
910
+        if(!$value = $request[$label]) {
911
+          return '';
912
+        }
875 913
         return $value;
876 914
     }
877 915
 
@@ -881,7 +919,9 @@  discard block
 block discarded – undo
881 919
      * @return bool|string
882 920
      */
883 921
     private function InsertLabel($label,$attributes=[]){
884
-        if(!$label or is_array($label)) return false;
922
+        if(!$label or is_array($label)) {
923
+          return false;
924
+        }
885 925
         $labelAttributes = $this->FilterAttributes($attributes,'label');
886 926
         $html = '<label'.$this->BuildAttributes($labelAttributes).'>';
887 927
         $html .= $label;
@@ -895,9 +935,12 @@  discard block
 block discarded – undo
895 935
      */
896 936
     private function normalize($keys)
897 937
     {
898
-        if(is_array($keys)) return false;
899
-        foreach (array(' ', '&nbsp;', '\n', '\t', '\r', '"','\'','_') as $strip)
900
-            $keys = str_replace($strip, '', (string) $keys);
938
+        if(is_array($keys)) {
939
+          return false;
940
+        }
941
+        foreach (array(' ', '&nbsp;', '\n', '\t', '\r', '"','\'','_') as $strip) {
942
+                    $keys = str_replace($strip, '', (string) $keys);
943
+        }
901 944
         $keys = trim(preg_replace('/\W+/', '-', $keys), '-');
902 945
         $keys = strtolower(str_ireplace('input','',$keys));
903 946
         return $keys;
Please login to merge, or discard this patch.