Exemplo n.º 1
0
 public function convertFromString(ValidationExpression $validation, $value, $rawValue = null, $fromStorage = false)
 {
     if (is_null($rawValue)) {
         $rawValue = $value;
     }
     $vArray = $validation->getValidationArray();
     $datatype = $validation->getDatatype();
     switch ($datatype) {
         case 'flag':
             if ($value == false || strlen(trim($value)) == 0) {
                 $value = null;
             } else {
                 $value = 1;
             }
             break;
             //Clean HTML
         //Clean HTML
         case 'html':
             $value = $this->InputClean->clean($rawValue, array_key_exists('allowedtags', $vArray) ? $vArray['allowedtags'] : null);
             break;
             // Sanitize URL
         // Sanitize URL
         case 'url':
             $urlCleaned = URLUtils::safeURL($rawValue);
             if (!empty($urlCleaned)) {
                 $value = $urlCleaned;
             }
             break;
             //Convert INT
         //Convert INT
         case 'int':
             if (!empty($value) && is_numeric(str_replace(',', '', $value)) == FALSE) {
                 throw new TypeConversionException("Cannot convert string value [{$value}] to integer");
             } else {
                 if ($value === '') {
                     $value = null;
                 } else {
                     $value = intval(str_replace(',', '', $value));
                 }
             }
             break;
             //Convert FLOAT
         //Convert FLOAT
         case 'float':
             if (!empty($value) && is_numeric(str_replace(',', '', $value)) == FALSE) {
                 throw new TypeConversionException("Cannot convert string value [{$value}] to float");
             } else {
                 if ($value === '') {
                     $value = null;
                 } else {
                     $value = floatval(str_replace(',', '', $value));
                 }
             }
             break;
             //Convert BOOLEAN
         //Convert BOOLEAN
         case 'boolean':
             $value = StringUtils::strToBool($value);
             break;
             //Convert DATE
         //Convert DATE
         case 'date':
             if (!empty($value) && strlen(trim($value)) > 0) {
                 try {
                     if ($fromStorage) {
                         $value = $this->DateFactory->newStorageDate(trim($value));
                     } else {
                         $value = $this->DateFactory->newLocalDate(trim($value));
                     }
                 } catch (DateException $e) {
                     throw new TypeConversionException("Cannot convert string value [{$value}] to date");
                 }
             } else {
                 $value = null;
             }
             break;
     }
     unset($vArray);
     unset($datatype);
     unset($rawValue);
     unset($validation);
     return $value;
 }