yii\db\ColumnSchema::typecast PHP Method

typecast() protected method

If the value is null or an [[Expression]], it will not be converted.
Since: 2.0.3
protected typecast ( mixed $value ) : mixed
$value mixed input value
return mixed converted value
    protected function typecast($value)
    {
        if ($value === '' && $this->type !== Schema::TYPE_TEXT && $this->type !== Schema::TYPE_STRING && $this->type !== Schema::TYPE_BINARY && $this->type !== Schema::TYPE_CHAR) {
            return null;
        }
        if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
            return $value;
        }
        switch ($this->phpType) {
            case 'resource':
            case 'string':
                if (is_resource($value)) {
                    return $value;
                }
                if (is_float($value)) {
                    // ensure type cast always has . as decimal separator in all locales
                    return str_replace(',', '.', (string) $value);
                }
                return (string) $value;
            case 'integer':
                return (int) $value;
            case 'boolean':
                // treating a 0 bit value as false too
                // https://github.com/yiisoft/yii2/issues/9006
                return (bool) $value && $value !== "";
            case 'double':
                return (double) $value;
        }
        return $value;
    }

Usage Example

 protected function typecast($value)
 {
     $value = parent::typecast($value);
     if ($this->case !== false && is_string($value)) {
         if ($this->case === CASE_LOWER) {
             $value = strtolower($value);
         } elseif ($this->case === CASE_UPPER) {
             $value = strtoupper($value);
         }
     }
     return utf8_encode($value);
 }