Inheritance: implements Themosis\Validation\IValidate
Esempio n. 1
0
 /**
  * Used to run the sanitize callbacks.
  *
  * @param mixed  $value
  * @param string $key
  * @param string $type
  *
  * @return mixed
  */
 public function sanitizeField($value, $key, $type)
 {
     $rules = $this->datas['rules.sanitize'];
     $rule = isset($rules[$key]) ? $rules[$key] : ['html'];
     $vals = [];
     // Check sanitization for infinite fields.
     if (is_array($value)) {
         foreach ($value as $k => $val) {
             if (is_array($val)) {
                 foreach ($val as $subKey => $subVal) {
                     // Check if there is a sanitize method defined for inner fields.
                     if (isset($rule[$subKey]) && !is_numeric($subKey)) {
                         $vals[$k][$subKey] = $this->validator->single($subVal, $rule[$subKey]);
                     } else {
                         // If one inner field has a rule, this one is wrong for the others because $rule is an array of array.
                         if (isset($rules[$key]) && !isset($rule[$subKey])) {
                             $vals[$k][$subKey] = $this->validator->single($subVal, ['html']);
                         } else {
                             $vals[$k][$subKey] = $this->validator->single($subVal, $rule);
                         }
                     }
                 }
             }
         }
     }
     // Return parsed array of the infinite field.
     if (!empty($vals)) {
         return $vals;
     }
     return $this->validator->single($value, $rule);
 }
Esempio n. 2
0
 /**
  * Validate the defined settings.
  *
  * @param mixed $values
  * @return array
  */
 public function validateSettings($values)
 {
     // No validation rules
     if (!isset($this->datas['rules']) || !is_array($this->datas['rules'])) {
         return $values;
     }
     // Null given
     if (is_null($values)) {
         return [];
     }
     $sanitized = [];
     foreach ($values as $setting => $value) {
         $rules = array_keys($this->datas['rules']);
         // 1 - Check if a rule exists
         if (in_array($setting, $rules)) {
             // 1.1 - Check for infinite settings.
             if (is_array($value) && $this->isInfinite($setting)) {
                 foreach ($value as $index => $row) {
                     if ($this->validator->isAssociative($row) && !empty($row)) {
                         foreach ($row as $infiniteSetting => $infiniteValue) {
                             // 1.1.1 - Check if a rule is defined for the infinite sub fields.
                             if (isset($this->datas['rules'][$setting][$infiniteSetting])) {
                                 $rule = $this->datas['rules'][$setting][$infiniteSetting];
                                 $sanitized[$setting][$index][$infiniteSetting] = $this->validator->single($infiniteValue, $rule);
                             } else {
                                 $sanitized[$setting][$index][$infiniteSetting] = $infiniteValue;
                             }
                         }
                     }
                 }
             } else {
                 // 1.2 - Apply rule to other settings.
                 $sanitized[$setting] = $this->validator->single($value, $this->datas['rules'][$setting]);
             }
         } else {
             // 2 - No rule, just set the default value.
             $sanitized[$setting] = $value;
         }
     }
     return $sanitized;
 }