yii\sphinx\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_STRING) {
            return null;
        }
        if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
            return $value;
        }
        switch ($this->phpType) {
            case 'resource':
            case 'string':
                return is_resource($value) ? $value : (string) $value;
            case 'int':
            case 'integer':
                return (int) $value;
            case 'bool':
            case 'boolean':
                return (bool) $value;
            case 'double':
                return (double) $value;
        }
        return $value;
    }

Usage Example

コード例 #1
0
 /**
  * @dataProvider dataProviderTypeCast
  *
  * @param $type
  * @param $phpType
  * @param $value
  * @param $expectedResult
  */
 public function testTypeCast($type, $phpType, $value, $expectedResult)
 {
     $columnSchema = new ColumnSchema();
     $columnSchema->type = $type;
     $columnSchema->phpType = $phpType;
     $this->assertEquals($expectedResult, $columnSchema->typecast($value));
 }