| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Encore\Admin\Form\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Encore\Admin\Form\EmbeddedForm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Encore\Admin\Form\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Facades\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Embeds extends Field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var \Closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $builder = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $view = 'admin::form.embeds'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * Create a new HasMany field instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param string $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param array  $arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 23 |  | View Code Duplication |     public function __construct($column, $arguments = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->column = $column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         if (count($arguments) == 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $this->label = $this->formatLabel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             $this->builder = $arguments[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         if (count($arguments) == 2) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             list($this->label, $this->builder) = $arguments; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Prepare input data for insert or update. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param array $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function prepare($input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $form = $this->buildEmbeddedForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return $form->setOriginal($this->original)->prepare($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function getValidator(array $input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if (!array_key_exists($this->column, $input)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $input = array_only($input, $this->column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $rules = $attributes = $messages = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $rel = $this->column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $availInput = $input; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 67 |  | View Code Duplication |         $array_key_attach_str = function (array $a, string $b, string $c = '.') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return call_user_func_array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 'array_merge', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 array_map(function ($u, $v) use ($b, $c) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     return ["{$b}{$c}{$u}" => $v]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 }, array_keys($a), array_values($a)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 76 |  | View Code Duplication |         $array_key_clean = function (array $a) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $a = count($a) ? call_user_func_array('array_merge', array_map(function ($k, $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 return [str_replace(':', '', $k) => $v]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             }, array_keys($a), array_values($a))) : $a; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             return $a; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 84 |  | View Code Duplication |         $array_key_clean_undot = function (array $a) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             if (count($a)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 foreach ($a as $key => $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                     array_set($a, str_replace(':', '', $key), $val); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     if (preg_match('/[\.\:]/', $key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                         unset($a[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             return $a; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         /** @var Field $field */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         foreach ($this->buildEmbeddedForm()->fields() as $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             if (!$fieldRules = $field->getRules()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $column = $field->column(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $columns = is_array($column) ? $column : [$column]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             if ($field instanceof Field\MultipleSelect) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 $availInput[$column] = array_filter($availInput[$column], 'strlen'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 $availInput[$column] = $availInput[$column] ?: null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |              * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |              * For single column field format rules to: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |              * [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |              *     'extra.name' => 'required' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |              *     'extra.email' => 'required' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |              * ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |              * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |              * For multiple column field with rules like 'required': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |              * 'extra' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |              *     'start' => 'start_at' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |              *     'end'   => 'end_at', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |              * ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |              * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |              * format rules to: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |              * [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |              *     'extra.start_atstart' => 'required' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |              *     'extra.end_atend' => 'required' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |              * ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |              */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $newColumn = array_map(function ($k, $v) use ($rel) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 //Fix ResetInput Function! A Headache Implementation! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 return !$k ? "{$rel}.{$v}" : "{$rel}.{$v}:{$k}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             }, array_keys($columns), array_values($columns)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $fieldRules = is_array($fieldRules) ? implode('|', $fieldRules) : $fieldRules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $rules = array_merge($rules, call_user_func_array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 'array_merge', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 array_map(function ($v) use ($fieldRules) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     return [$v => $fieldRules]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 }, $newColumn) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $attributes = array_merge($attributes, call_user_func_array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 'array_merge', | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 142 |  | View Code Duplication |                 array_map(function ($v) use ($field) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                     //Fix ResetInput Function! A Headache Implementation! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                     $u = $field->label(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                     $u .= is_array($field->column()) ? '['.explode(':', explode('.', $v)[1])[0].']' : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                     return [$v => "{$u}"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 }, $newColumn) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             )); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 150 |  | View Code Duplication |             if ($field->validationMessages) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 $newMessages = array_map(function ($v) use ($field, $availInput, $array_key_attach_str) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                     list($rel, $col) = explode('.', $v); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                     //Fix ResetInput Function! A Headache Implementation! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                     $col1 = explode(':', $col)[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                     if (!array_key_exists($col1, $availInput[$rel])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                         return [null => null]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                     $rows = $availInput[$rel][$col1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                     if (!is_array($rows)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                         return $array_key_attach_str($field->validationMessages, $v); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                     $r = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                     foreach (array_keys($rows) as $k) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                         $k = "{$v}{$k}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                         $r = array_merge($r, $array_key_attach_str($field->validationMessages, $k)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                     return $r; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 }, $newColumn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                 $newMessages = call_user_func_array('array_merge', $newMessages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                 $messages = array_merge($messages, $newMessages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         if (empty($rules)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         $newInput = call_user_func_array('array_merge', array_map(function ($idx) use ($availInput) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             list($rel, $col) = explode('.', $idx); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             //Fix ResetInput Function! A Headache Implementation! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             $col1 = explode(':', $col)[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             if (!array_key_exists($col1, $availInput[$rel])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                 return [null => null]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             if (is_array($availInput[$rel][$col1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |                 return call_user_func_array('array_merge', array_map(function ($x, $y) use ($idx) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |                     return ["{$idx}{$x}" => $y]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |                 }, array_keys($availInput[$rel][$col1]), $availInput[$rel][$col1])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             return ["{$idx}" => $availInput[$rel][$col1]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         }, array_keys($rules))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         $newInput = $array_key_clean_undot($newInput); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         $newRules = array_map(function ($idx) use ($availInput, $rules) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |             list($rel, $col) = explode('.', $idx); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |             //Fix ResetInput Function! A Headache Implementation! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |             $col1 = explode(':', $col)[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |             if (!array_key_exists($col1, $availInput[$rel])) return [null => null]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             if (is_array($availInput[$rel][$col1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |                 return call_user_func_array('array_merge', array_map(function ($x) use ($idx, $rules) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |                     return ["{$idx}{$x}" => $rules[$idx]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |                 }, array_keys($availInput[$rel][$col1]))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |             return ["{$idx}" => $rules[$idx]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         }, array_keys($rules)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         $newRules = array_filter(call_user_func_array('array_merge', $newRules), 'strlen', ARRAY_FILTER_USE_KEY); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         $newRules = $array_key_clean($newRules);; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         $newAttributes = array_map(function ($idx) use ($availInput, $attributes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |             list($rel, $col) = explode('.', $idx); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |             //Fix ResetInput Function! A Headache Implementation! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |             $col1 = explode(':', $col)[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |             if (!array_key_exists($col1, $availInput[$rel])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |                 return [null => null]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |             } | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 219 |  | View Code Duplication |             if (is_array($availInput[$rel][$col1])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |                 if (array_keys($availInput[$rel][$col1])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |                     return call_user_func_array('array_merge', array_map(function ($x) use ($idx, $attributes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |                     return ["{$idx}.{$x}" => $attributes[$idx]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |                 }, array_keys($availInput[$rel][$col1]))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |                  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |                 return [null => null]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |             return ["{$idx}" => $attributes[$idx]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |         }, array_keys($attributes)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |         $newAttributes = array_filter(call_user_func_array('array_merge', $newAttributes), 'strlen'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |         $newAttributes = $array_key_clean($newAttributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |         $messages = $array_key_clean($messages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         if (empty($newInput)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |             $newInput = $availInput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |         return Validator::make($newInput, $newRules, $messages, $newAttributes); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |      * Format validation attributes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |      * @param array  $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |      * @param string $label | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |      * @param string $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 252 |  | View Code Duplication |     protected function formatValidationAttribute($input, $label, $column) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         $new = $attributes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         if (is_array($column)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |             foreach ($column as $index => $col) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |                 $new[$col . $index] = $col; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |         foreach (array_keys(array_dot($input)) as $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |             if (is_string($column)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |                 if (Str::endsWith($key, ".$column")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |                     $attributes[$key] = $label; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |                 foreach ($new as $k => $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |                     if (Str::endsWith($key, ".$k")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |                         $attributes[$key] = $label . "[$val]"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |         return $attributes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |      * Reset input key for validation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |      * @param array $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |      * @param array $column $column is the column name array set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |      * @return void. | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |     public function resetInputKey(array &$input, array $column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |         $column = array_flip($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 291 |  | View Code Duplication |         foreach ($input[$this->column] as $key => $value) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |             if (!array_key_exists($key, $column)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |             $newKey = $key . $column[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |             /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |              * set new key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |              */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |             array_set($input, "{$this->column}.$newKey", $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |             /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |              * forget the old key and value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |              */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |             array_forget($input, "{$this->column}.$key"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |      * Get data for Embedded form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |      * Normally, data is obtained from the database. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |      * When the data validation errors, data is obtained from session flash. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |     protected function getEmbeddedData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |         if ($old = old($this->column)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |             return $old; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |         if (empty($this->value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |         if (is_string($this->value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |             return json_decode($this->value, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |         return (array)$this->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |      * Build a Embedded Form and fill data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |      * @return EmbeddedForm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |     protected function buildEmbeddedForm() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |         $form = new EmbeddedForm($this->column); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |         $form->setParent($this->form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |         call_user_func($this->builder, $form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |         $form->fill($this->getEmbeddedData()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |         return $form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |      * Render the form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |      * @return \Illuminate\View\View | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 357 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 358 |  |  |     public function render() | 
            
                                                        
            
                                    
            
            
                | 359 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 360 |  |  |         return parent::render()->with(['form' => $this->buildEmbeddedForm()]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 361 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 362 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 363 |  |  |  | 
            
                        
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.