protected function deriveMetaDatatype(ValidationExpression $ve)
 {
     $v = $ve->getValidationArray();
     $datatype = $v['datatype'];
     switch ($datatype) {
         case 'flag':
             return 'flag';
         case 'boolean':
             return 'tiny';
         case 'date':
             if (isset($v['unix']) && !StringUtils::strToBool($v['unix'])) {
                 return 'datetime';
             }
             // If unix attribute is not specified, assume they want date (timestamp) for backward-compatibility
             return 'date';
         case 'int':
             $min = isset($v['min']) ? (int) $v['min'] : 0;
             $max = isset($v['max']) ? (int) $v['max'] : PHP_INT_MAX;
             if ($min < 0) {
                 if ($min >= -128 && $max <= 127) {
                     return 'tiny-signed';
                 } else {
                     if ($min >= -2147483648 && $max <= 2147483647) {
                         return 'int-signed';
                     }
                 }
                 return 'long-signed';
             }
             if ($max < 255) {
                 return 'tiny';
             } else {
                 if ($max <= 4294967295) {
                     return 'int';
                 }
             }
             return 'long';
         case 'float':
             return 'float';
         case 'slug':
         case 'slugwithslash':
         case 'email':
             $ve->setMax(255);
             // set max
             return 'varchar';
         case 'json':
             $ve->setMax(262144);
             //set max to 256K
             return 'mediumtext';
         case 'string':
         case 'html':
         case 'url':
             // NOTE: MySQL columns are bytes, string lengths are characters
             $max = 255;
             // default is 255
             if (isset($v['max'])) {
                 $max = (int) FileSystemUtils::iniValueInBytes($v['max']);
             }
             if ($max == 65536) {
                 $max = 65535;
             }
             if ($max > 262144) {
                 // max is 256K
                 $max = 262144;
             }
             $ve->setMax($max);
             // set max
             if ($max <= 255) {
                 // 255 or less is VARCHAR
                 return 'varchar';
             }
             if ($max <= 65535) {
                 // 64K or less is TEXT
                 return 'text';
             }
             if ($max <= 262144) {
                 // 256K or less is MEDIUMTEXT
                 return 'mediumtext';
             }
         case 'binary':
             $max = 262144;
             // default is 256K
             if (isset($v['max'])) {
                 $max = (int) FileSystemUtils::iniValueInBytes($v['max']);
             }
             if ($max == 65536) {
                 $max = 65535;
             }
             if ($max > 262144) {
                 // max is 256K
                 $max = 262144;
             }
             $ve->setMax($max);
             // set max
             if ($max <= 65535) {
                 // 64K or less, store as BLOB
                 return 'blob';
             }
             if ($max <= 262144) {
                 // 256K or less, store as MEDIUMBLOB
                 return 'mediumblob';
             }
     }
 }
Пример #2
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;
 }