Cast a generic value from the source to a typed value, if null return null

@param mixed $value
@param string $commonType
@return mixed
761640
protected function castValueWithCommonType($value, string $commonType)
    {
        // these are sorted by the most common data types to avoid extra comparisons
        if (null === $value) {
            return null;
        }
        if (CommonTypes::TTEXT === $commonType) {
            return strval($value);
        }
        if (CommonTypes::TINT === $commonType) {
            return intval($value);
        }
        if (CommonTypes::TNUMBER === $commonType) {
            return floatval($value);
        }
        if (CommonTypes::TBOOL === $commonType) {
            return (bool) $value;
        }
        if (in_array($commonType, [CommonTypes::TDATE, CommonTypes::TTIME, CommonTypes::TDATETIME], true)) {
            return strtotime($value);
        }
        return strval($value);
    }
1183444
public function normalizeValue($type, $value)
    {
        if ($value === null) {
            return null;
        }

        if ($type === 'string') {
            return is_string($value) ? $value : (string)$value;
        } elseif ($type === 'int') {
            return is_int($value) ? $value : (int)$value;
        } elseif ($type === 'float') {
            return is_float($value) ? $value : (float)$value;
        } elseif ($type === 'objectid') {
            return is_scalar($type) ? new ObjectID($value) : $value;
        } elseif ($type === 'bool') {
            return is_bool($value) ? $value : (bool)$value;
        } elseif ($type === 'array') {
            return (array)$value;
        } else {
            throw new InvalidValueException(['`:model` model is not supported `:type` type', 'model' => static::class, 'type' => $type]);
        }
    }
904844
public static function guessAndCastValue($value, &$type = '')
    {
        // matched values array
        $matchesTrue = ['true','yes','ja'];
        $matchesFalse = ['false','no','nein'];
        $matchesNull = ['null'];

        if (in_array(strtolower($value), $matchesTrue)) {
            $type = 'boolean';

            return true;
        } elseif (in_array(strtolower($value), $matchesFalse)) {
            $type = 'boolean';

            return false;
        } elseif (in_array(strtolower($value), $matchesNull)) {
            $type = 'null';

            return null;
        } else {
            // check for numerical string and cast if necessary
            if (strstr($value, ',') !== false) {
                $checkStr = str_replace(',', '.', $value);
                if (is_numeric($checkStr)) {
                    $value = $checkStr;
                }
            }
            //numbers
            if (is_numeric($value) && strstr($value, '.') !== false && substr_count($value, '.') == 1) { //float
                $type = 'float';

                return (float) $value;
            } elseif (is_numeric($value)) { //int
                $type = 'integer';

                return (int) $value;
            } else {
                $type = 'string';

                return $value;
            }
        }
    }
1373038
public function dbTypecastValue($value)
    {
        if ($value === null) {
            return null;
        }

        switch ($this->type) {
            case Schema::TYPE_BIT:
                return decbin($value);
            case Schema::TYPE_BINARY:
                return is_string($value) ? new PdoValue($value, \PDO::PARAM_LOB) : $value;
            case Schema::TYPE_TIMESTAMP:
            case Schema::TYPE_DATETIME:
                return \Yii::$app->formatter->asDatetime($value, 'yyyy-MM-dd HH:mm:ss');
            case Schema::TYPE_DATE:
                return \Yii::$app->formatter->asDate($value, 'yyyy-MM-dd');
            case Schema::TYPE_TIME:
                return \Yii::$app->formatter->asTime($value, 'HH:mm:ss');
            case Schema::TYPE_JSON:
                return new JsonExpression($value, $this->dbType);
            case Schema::TYPE_COMPOSITE:
                return new CompositeExpression($value, $this->dbType, $this);
        }

        return $this->typecast($value);
    }
970197
public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        if (empty($value)) {
            return null;
        }

        if ($value instanceof Uuid || Uuid::isValid($value)) {
            return (string) $value;
        }

        throw ConversionException::conversionFailed($value, self::NAME);
    }
219881
final public static function resolveValue($value, AbstractPlatform $platform)
    {
        if ($value instanceof DomainId) {
            $class = \get_class($value);
            $type = Type::INTEGER;

            foreach (self::$mapping as $mapping) {
                if ($class === $mapping['class']) {
                    $type = $mapping['data_type'] ?? $type;
                    break;
                }
            }

            return self::getType($type)->convertToPHPValue($value->isEmpty() ? null : $value->toString(), $platform);
        }

        return $value;
    }
1313453
public function toPg($value, $type = null)
    {
        return $this->converter->toPg(
            $value,
            $type === null ? $this->getClientIdentifier() : $type,
            $this->getSession()
        );
    }
1287130
public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        if (empty($value)) {
            return null;
        }

        if ($value instanceof Money) {
            return (string) $value->getCurrency().' '.$value->getAmount();
        }

        throw ConversionException::conversionFailed($value, self::NAME);
    }
733138
public function getCastedValue($value, $castingDefinition)
    {
        $castingParams = array();

        if (is_array($castingDefinition)) {
            $castingParams = $castingDefinition;
            array_shift($castingParams);
            $castingDefinition = array_shift($castingDefinition);
        }

        if (strpos($castingDefinition, '->') === false) {
            $castingFieldType = $castingDefinition;
            $castingField = DBField::create_field($castingFieldType, $value);

            return call_user_func_array(array($castingField, 'XML'), $castingParams);
        }

        list($castingFieldType, $castingMethod) = explode('->', $castingDefinition);

        $castingField = DBField::create_field($castingFieldType, $value);

        return call_user_func_array(array($castingField, $castingMethod), $castingParams);
    }
92562
protected function castValueForUpdate($value)
    {
        switch ($this->type) {
            case 'string':
                $value = [(string)$value];
                break;
            case 'list':
            case 'set':
                $value = (array)$value;
                break;
            case 'zset':
                $casted = [];
                foreach ($value as $k => $v) {
                    $casted[] = $v;
                    $casted[] = $k;
                }
                $value = $casted;
                break;
            case 'hash':
                $casted = [];
                foreach ($value as $k => $v) {
                    $casted[] = $k;
                    $casted[] = $v;
                }
                $value = $casted;
                break;
            default:
                break;
        }

        return $value;
    }
201991
public function castValue($field, $value, $cast = true)
    {
        if (! array_key_exists($field, $this->casts) || ! $cast)
            // no cast configured for said field. Nothing done.
            return $value;

        $types = [
            "boolean", "bool", "integer", "int", "float", "double",
            "string", "array", "object", "null"
        ];

        // validate type cast is valid
        $type = $this->casts[$field];
        if (! in_array($type, $types)) {
            throw new RuntimeException("Cast of field '" . $field . "' to type '" . $type . "' not possible. Please define only scalar type casts.");
        }

        $output = $value;
        $result = settype($output, $type);

        if ($result === true)
            return $output;

        return $value;
    }
1041353
public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        $result = null;

        if ($value !== null && ($dateTimeOrNull = parent::convertToPHPValue($value, $platform)) !== null) {
            assert($dateTimeOrNull instanceof DateTimeInterface);
            // despite the name it's not null already
            $result = DateTime::createFromDateTime($dateTimeOrNull);
        }

        return $result;
    }
243132
public function convertToPHPValue($value, PlatformInterface $platform)
    {
        if ($value === null) {
            return;
        }

        $val = @unserialize($value);

        if ($val === false) {
            throw TypeException::conversionToPHPFailed($value, $this->getName());
        }

        return $val;
    }
163524
public static function getValueWithinItsType($value, $knownType = null)
    {
        if (is_int($value) || (!is_null($value) && in_array($knownType, array(
            'time',
            'positiveInteger',
            'unsignedLong',
            'unsignedInt',
            'short',
            'long',
            'int',
            'integer',
        ), true))) {
            return intval($value);
        } elseif (is_float($value) || (!is_null($value) && in_array($knownType, array(
            'float',
            'double',
            'decimal',
        ), true))) {
            return floatval($value);
        } elseif (is_bool($value) || (!is_null($value) && in_array($knownType, array(
            'bool',
            'boolean',
        ), true))) {
            return ($value === 'true' || $value === true || $value === 1 || $value === '1');
        }
        return $value;
    }
1167737
protected function castValue($cast, $value)
    {
        switch ($cast) {
            case 'integer';
                $value = (integer) $value;
                break;
            case 'boolean';
                $value = in_array($value, array('true', 't', 'yes', 'y'));
                break;
            case 'string':
            default:
                $value = (string) $value;
        }

        return $value;
    }
215201
public function cast($value)
    {
        if (isset($value) && !is_resource($value)) {
            return $this->dontCast($value);
        }
        
        return $value;
    }
510605
public function getTypeFromValue($value = null)
    {
        $value = ($value == null ? $this->value : $value);
        return array_search($this->value, self::$sourceList);
    }