/**
  * Fills the {@link &$model} with values from the {@link $fields}
  *
  * @param array       $fields    An array with field names as keys and the values to store.
  * @param array       $rawFields An array with field names as keys and the raw value as the value.
  *                                  Used for 'html' typed fields, for raw access to the field value.
  * @param ModelObject &$model    The ModelObject we're filling
  * @param Errors      $errors    The Errors object to update if there are any field errors
  *
  * @return ModelObject Returns &$model after modifications
  */
 public function inputArrayToModel(array $fields, array $rawFields, ModelObject &$model, Errors $errors)
 {
     $fields = array_intersect_key($fields, array_flip($model->getPersistentFields()));
     foreach ($fields as $key => $value) {
         $datatype = $model->getDatatype($key);
         // Clean HTML
         if ($datatype === 'html') {
             $validation = $model->getValidation($key);
             $fields[$key] = $this->InputClean->clean($rawFields[$key], array_key_exists('allowedtags', $validation) ? $validation['allowedtags'] : null);
         }
         // Convert INT
         if ($datatype === 'int' && !empty($value)) {
             if (is_numeric($value) === false) {
                 $errors->addFieldError('invalid', get_class($model) . '.' . $key, 'field', $model->getFieldTitle($key), $value, "Cannot convert string [{$value}] to integer.");
             } else {
                 $fields[$key] = intval($value);
             }
         }
         // Convert FLOAT
         if ($datatype === 'float' && !empty($value)) {
             if (is_numeric($value) === false) {
                 $errors->addFieldError('invalid', get_class($model) . '.' . $key, 'field', $model->getFieldTitle($key), $value, "Cannot convert string [{$value}] to float.");
             } else {
                 $fields[$key] = floatval($value);
             }
         }
         // Convert BOOLEAN
         if ($datatype === 'boolean') {
             $fields[$key] = StringUtils::strToBool($value);
         }
         // Convert DATE
         if ($datatype === 'date' && strlen(trim($value)) > 0) {
             try {
                 $fields[$key] = $this->DateFactory->newLocalDate(trim($value));
             } catch (DateException $e) {
                 $errors->addFieldError('invalid', get_class($model) . '.' . $key, 'field', $model->getFieldTitle($key), $value, "Cannot convert string [{$value}] to date.");
             }
         }
     }
     $model->setFromArray($fields);
     return $model;
 }