Casts the given $value to a byte.
@throws CastingMismatchException
@return mixed
420373
public function castByte()
    {
        $min = self::BYTE_MIN_VALUE;
        $max = self::BYTE_MAX_VALUE;

        $castFunction = function ($value) use ($min, $max) {
            return $value < 0 ? $min : $max;
        };

        if (is_numeric($this->value) && $this->value >= $min && $this->value <= $max) {
            return $this->value;
        }

        return $this->handleMismatch($castFunction, 'byte');
    }
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);
    }
552033
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;
    }
570585
public static function getBytes($value){

		$value = trim($value);
		$unit = $value{strlen($value)-1};

		$value = floatval($value);

		switch(strtolower($unit)){

			case 'g':

				$multiplier = 1073741824;

			break;

			case 'm':

				$multiplier = 1048576;

			break;

			case 'k':

				$multiplier = 1024;

			break;

			default:

				ExceptionHandler::notice('Number::getBytes() expects unit to be either "g", "m" or "k"');
				$multiplier = 1;
		}

		return $multiplier * $value;
	}
1080250
protected function castToInt($value, bool $nullable = true)
    {
        if (null === $value && $nullable) {
            return null;
        }

        return (int) $value;
    }
221781
protected function prepareValue($value)
    {
        if ($this->hasMutator()) {
            return call_user_func($this->getMutator(), $value);
        } elseif ($value && $this->isElementOfDate() && $this->isDateCastable()) {
            $value = $this->castValueAsDateTime($value);
            if ($this instanceof Timestamp) {
                $value = $value->getTimestamp();
            }

            return $value;
        }

        if (! is_null($value)) {
            if ($this->isJsonCastable()) {
                return $this->castValueAsJson($value);
            } elseif ($this->isCommaCastable()) {
                return $this->castValueAsCommaSeparated($value);
            }
        }

        return $value;
    }
158810
public function cast($value)
    {
        $fn = 'cast' . ucfirst(gettype($value));
        
        return method_exists($this, $fn) ? $this->$fn($value) : (boolean)$value;
    }
1289475
public static function convertBytes($value)
    {
        if (is_numeric($value)) {
            return $value;
        }

        $value_length = \strlen($value);
        $qty = (int)substr($value, 0, $value_length - 1);
        $unit = Str::strtolower(substr($value, $value_length - 1));
        switch ($unit) {
            case 'k':
                $qty *= 1024;
                break;
            case 'm':
                $qty *= 1048576;
                break;
            case 'g':
                $qty *= 1073741824;
                break;
        }

        return $qty;
    }
259422
public static function cast($value) {
		$matches = array();
		if(strtolower($value) === 'true') {
			$value = (bool) true;
		} else if(strtolower($value) === 'false') {
			$value = (bool) false;
		} else if(is_numeric($value)) {
			$value = (int) $value;
		} else if(preg_match('/^"(.*)"$/', $value, $matches)) {
			$value = $matches[1];
		}
		return $value;
	}
1246921
protected function unserialize($value)
    {
        if (is_numeric($value)) {
            $int = (int) $value;
            if ((string) $int === $value) {
                return $int;
            }

            $float = (float) $value;
            if ((string) $float === $value) {
                return $float;
            }

            return $value;
        }

        return unserialize($value);
    }
1159646
public function marshal($value)
    {
        if (is_array($value) || $value === null) {
            return $value;
        }

        return json_decode($value, true);
    }
909188
protected function castObject($value)
    {
        if ($value instanceof \DateTime) {
            return $value->format('c');
        }
        
        if (method_exists($value, '__toString')) {
            return (string)$value;
        }
        
        return $this->dontCast($value);
    }
1232005
private function cast($value, $type)
    {
        switch ($type) {
            case 'bool':
                return boolval($value);
                break;
            case 'int':
                return intval($value);
                break;
            case 'float':
                return floatval($value);
                break;
            case 'string':
                return $value;
                break;
            case 'array':
                if (is_array($value)) {
                    return serialize($value);
                } else {
                    return unserialize($value);
                }
                break;
            default:
                return $value;
        }
    }
976433
public function cast($value): array
    {
        $result = [];
        foreach ((array)$value as $key => $itemValue) {
            $result[$key] = $this->type->cast($itemValue);
        }
        return $result;
    }
64259
private function castValue($value)
    {
        foreach ([
            // Integers
            '/^-?\d+$/' => function ($value) {
                return (int) $value;
            },
            // Floats
            '/^-?\d+\.\d+$/' => function ($value) {
                return (float) $value;
            },
            // Booleans
            '/^(?:true|false)$/' => function ($value) {
                return filter_var($value, \FILTER_VALIDATE_BOOLEAN);
            },
        ] as $format => $castFunction) {
            if (preg_match($format, $value)) {
                return $castFunction($value);
            }
        }

        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;
    }
361225
protected function castAttribute($key, $value)
    {
        if ($value === null) {
            return $value;
        }

        if ($this->getCastType($key) === 'serialize') {
            return $this->fromSerialize($value);
        }

        return parent::castAttribute($key, $value);
    }