Ejemplo n.º 1
0
Archivo: Field.php Proyecto: jasny/Q
 /**
  * Cast a value according to the datatype property
  *
  * @param mixed   $value
  * @param boolean $force  Force casting and cast to type instead of datatype
  * @return mixed
  */
 public function castValue($value, $force = null)
 {
     if ($value === null || $value === "") {
         return null;
     }
     if (!isset($this['type'])) {
         return $value;
     }
     $cast = null;
     $type = strtolower($force ? $this['type'] : $this['datatype']);
     switch ($type) {
         case 'children':
             $cast = (array) $value;
             break;
         case 'array':
             $cast = !is_array($value) && !empty($value) ? split_set(';', $value) : $value;
             break;
         case 'bool':
         case 'boolean':
             if ($force || $value == 0 || $value == 1) {
                 $cast = (bool) $value;
             } else {
                 $cast = $value;
             }
             break;
         case 'set':
             if (!is_int($value) && !preg_match('/^-?\\d*$/', $value)) {
                 $value = split_set(';', $value);
             }
             if (is_array($value) && !empty($value) && is_string(reset($value))) {
                 $opts = $this->getProperty('values');
                 if (!empty($opts)) {
                     $intval = 0;
                     if (!is_array($opts)) {
                         $opts = split_set(';', $opts);
                     }
                     foreach ($opts as $val => $opt) {
                         if (in_array($opt, $value)) {
                             $intval += pow(2, $val);
                         }
                     }
                     $value = $intval;
                 }
             }
             if (is_array($value)) {
                 $value = array_sum($value);
             }
         case 'bit':
         case 'tinyint':
         case 'smallint':
         case 'mediumint':
         case 'int':
         case 'integer':
         case 'bigint':
             if (is_string($value)) {
                 $value = trim($value, '"');
             }
             $cast = (int) $value;
             if (is_object($value) || is_array($value) || !is_int($value) && !preg_match('/^-?\\d*$/', $value)) {
                 if ($force) {
                     trigger_error("Field '{$this}': Value {$value} is cast to integer {$cast}", E_USER_NOTICE);
                 } else {
                     $cast = $value;
                 }
             }
             break;
         case 'float':
         case 'double':
         case 'double precision':
         case 'real':
         case 'decimal':
         case 'dec':
         case 'fixed':
             if (is_string($value)) {
                 $value = trim($value, '"');
             }
             $matches = null;
             if (is_object($value) || is_array($value) || !is_float($value) && !preg_match('/^(-?\\d*)([\\.,]?)(\\d*)$/', $value, $matches) && $force) {
                 trigger_error("Field '{$this}': Value {$value} is cast to float " . (double) $value, E_USER_NOTICE);
             }
             $cast = !empty($matches) ? $cast = (double) ($matches[1] . '.' . $matches[3]) : (!$force ? $value : (double) $value);
             break;
         case 'date':
         case 'datetime':
             if (is_int($value) || is_string($value) && ctype_digit($value)) {
                 $date = new \DateTime();
                 $date->setTimestamp($value);
                 $cast = $date->format('c');
             } elseif (is_string($value) && preg_match('/^\\d{1,2}-\\d{1,2}-\\d{4}(\\s+\\d{1,2}:\\d{1,2}:\\d{1,2}\\d{1,2})?$/', $value, $matches)) {
                 $date = \DateTime::createFromFormat('j-n-Y H:i:s', $value . ($matches[1] ? '' : ' 00:00:00'));
                 if ($date) {
                     $cast = $date->format('c');
                 }
             }
             if (empty($cast)) {
                 $cast = $value;
             }
             break;
         case 'time':
             if (is_string($value) && preg_match('/^([0-1]\\d|2[0-3])\\:([0-5]\\d)/', $value, $matches)) {
                 $cast = $matches[0];
             } elseif ($force) {
                 trigger_error("Field '{$this}': Value {$value} is not a valid time: set to NULL", E_USER_NOTICE);
             } else {
                 $cast = $value;
             }
             break;
         default:
             if ($force) {
                 $value = $this['datatype'] == 'array' ? $value = implode_set(';', $value) : (string) $value;
             }
             $cast = is_string($value) ? trim($value) : $value;
     }
     return $cast;
 }
Ejemplo n.º 2
0
Archivo: MySQL.php Proyecto: jasny/Q
 /**
  * Return the connection string (without additional settings)
  * 
  * @return string
  */
 public function getDSN()
 {
     return 'mysql:' . implode_set(',', $this->settings);
 }
Ejemplo n.º 3
0
Archivo: misc.php Proyecto: jasny/Q
/**
 * Alias of join_set.
 *
 * @param string $glue
 * @param array  $array
 * @param string $format        Format for each pair in sprintf format: key, value, full key. Can be array(format num, format assoc).
 * @param string $group_prefix  Use %s for key
 * @param string $group_suffix
 * @param string $quote         Quote values (default is where needed)
 * @param string $key_prefix
 * @return string
 */
function implode_set($glue, $array, $format = array('%2$s', '%3$s=%2$s'), $group_prefix = '', $group_suffix = '', $quote = null, $key_prefix = null)
{
    return implode_set($glue, $array, $format, $group_prefix, $group_suffix, $quote, $key_prefix);
}