| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Backpack\CRUD\app\Library\Validation\Rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class ValidUploadMultiple extends ValidFileArray | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * Run the validation rule. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @param  string  $attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @param  mixed  $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     public function validate(string $attribute, mixed $value, Closure $fail): void | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         if (! $value = self::ensureValidValue($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |             $fail('Unable to determine the value type.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $entry = CrudPanelFacade::getCurrentEntry() !== false ? CrudPanelFacade::getCurrentEntry() : null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         // `upload_multiple` sends [[0 => null]] when user doesn't upload anything | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         // assume that nothing changed on field so nothing is sent on the request. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         if (count($value) === 1 && empty($value[0])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             if ($entry) { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |                 unset($this->data[$attribute]); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |                 $this->data[$attribute] = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             $value = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $previousValues = $entry?->{$attribute} ?? []; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         if (is_string($previousValues)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             $previousValues = json_decode($previousValues, true) ?? []; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $value = array_merge($previousValues, $value); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         if ($entry) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             $filesDeleted = CrudPanelFacade::getRequest()->input('clear_'.$attribute) ?? []; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             $data = $this->data; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             $data[$attribute] = array_diff($value, $filesDeleted); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             $this->validateArrayData($attribute, $fail, $data); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $this->validateItems($attribute, $value, $fail); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->validateArrayData($attribute, $fail); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $this->validateItems($attribute, $value, $fail); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  |