| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Proengsoft\JsValidation\Remote; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Http\Exceptions\HttpResponseException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Http\JsonResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Validation\ValidationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Validation\ValidationRuleParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Validation\Validator as BaseValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Proengsoft\JsValidation\Support\AccessProtectedTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Proengsoft\JsValidation\Support\RuleListTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class Validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     use AccessProtectedTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     use RuleListTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Validator extension name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     const EXTENSION_NAME = 'jsvalidation'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var \Illuminate\Validation\Validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Whether to escape validation messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected $escape; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * RemoteValidator constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param \Illuminate\Validation\Validator $validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param bool $escape | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 96 |  |     public function __construct(BaseValidator $validator, $escape = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 96 |  |         $this->validator = $validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 96 |  |         $this->escape = $escape; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 96 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Validate request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param $parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @throws \Illuminate\Validation\ValidationException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 96 |  |     public function validate($field, $parameters = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 96 |  |         $attribute = $this->parseAttributeName($field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 96 |  |         $validationParams = $this->parseParameters($parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 96 |  |         $validationResult = $this->validateJsRemoteRequest($attribute, $validationParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 96 |  |         $this->throwValidationException($validationResult, $this->validator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * Throw the failed validation exception. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param mixed $result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param \Illuminate\Validation\Validator  $validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @throws \Illuminate\Validation\ValidationException|\Illuminate\Http\Exceptions\HttpResponseException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 96 |  |     protected function throwValidationException($result, $validator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 96 |  |         $response = new JsonResponse($result, 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 96 |  |         if ($result !== true && class_exists(ValidationException::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 48 |  |             throw new ValidationException($validator, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 48 |  |         throw new HttpResponseException($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      *  Parse Validation input request data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 96 |  |     protected function parseAttributeName($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 96 |  |         parse_str($data, $attrParts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 96 |  |         $attrParts = is_null($attrParts) ? [] : $attrParts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 96 |  |         $newAttr = array_keys(Arr::dot($attrParts)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 96 |  |         return array_pop($newAttr); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      *  Parse Validation parameters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param $parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 96 |  |     protected function parseParameters($parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 96 |  |         $newParams = ['validate_all' => false]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 96 |  |         if (isset($parameters[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 84 |  |             $newParams['validate_all'] = ($parameters[0] === 'true') ? true : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 96 |  |         return $newParams; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Validate remote Javascript Validations. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param $attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param array $parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return array|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 96 |  |     protected function validateJsRemoteRequest($attribute, $parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 96 |  |         $this->setRemoteValidation($attribute, $parameters['validate_all']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 96 |  |         $validator = $this->validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 96 |  |         if ($validator->passes()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 48 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 48 |  |         $messages = $validator->messages()->get($attribute); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 48 |  |         if ($this->escape) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 12 |  |             foreach ($messages as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 12 |  |                 $messages[$key] = e($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 48 |  |         return $messages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * Sets data for validate remote rules. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param $attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param bool $validateAll | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 151 | 96 |  |     protected function setRemoteValidation($attribute, $validateAll = false) | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 153 | 96 |  |         $validator = $this->validator; | 
            
                                                                        
                            
            
                                    
            
            
                | 154 | 96 |  |         $rules = $validator->getRules(); | 
            
                                                                        
                            
            
                                    
            
            
                | 155 | 96 |  |         $rules = isset($rules[$attribute]) ? $rules[$attribute] : []; | 
            
                                                                        
                            
            
                                    
            
            
                | 156 | 96 |  |         if (in_array('no_js_validation', $rules)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 157 | 12 |  |             $validator->setRules([$attribute => []]); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 159 | 12 |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 161 | 84 |  |         if (! $validateAll) { | 
            
                                                                        
                            
            
                                    
            
            
                | 162 | 72 |  |             $rules = $this->purgeNonRemoteRules($rules, $validator); | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 164 | 84 |  |         $validator->setRules([$attribute => $rules]); | 
            
                                                                        
                            
            
                                    
            
            
                | 165 | 84 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * Remove rules that should not be validated remotely. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @param $rules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @param BaseValidator $validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 72 |  |     protected function purgeNonRemoteRules($rules, $validator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 72 |  |         $protectedValidator = $this->createProtectedCaller($validator); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 72 |  |         foreach ($rules as $i => $rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 72 |  |             $parsedRule = ValidationRuleParser::parse($rule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 72 |  |             if (! $this->isRemoteRule($parsedRule[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 54 |  |                 unset($rules[$i]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 72 |  |         return $rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 187 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 188 |  |  |  | 
            
                        
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: