Try to convert value on required type automatically

@param mixed $value

@return mixed

@throws Exception\InvalidArgumentException
763264
public function convertValue($value)
    {
        if ($value === null || ($value === "" && $this->type !== self::TYPE_STRING)) {
            return;
        }

        if (self::isScalarType($this->type) || $this->type === self::TYPE_ARRAY) {
            // Scalar and array

            if ($this->type === self::TYPE_BOOLEAN && strtolower($value) === "false") {
                return false;
            }

            if (is_scalar($value) || $this->type === self::TYPE_ARRAY) {
                if (settype($value, $this->type)) {
                    return $value;
                }
            }
        } elseif ($this->type === self::TYPE_DATETIME
            || $this->type === self::TYPE_DATE
        ) {
            // DateTime

            if ($value instanceof \DateTime) {
                return $value;
            } elseif (is_array($value) && isset($value["date"])) {
                $date = $value["date"];
            } elseif (is_object($value) && isset($value->date)) {
                $date = $value->date;
            } else {
                $date = $value;
            }

            if (isset($date)) {
                try {
                    return new \DateTime($date);
                } catch (\Exception $e) {

                }
            }
        } elseif ($this->type === self::TYPE_COLLECTION
            && Validator::isTraversable($value)
        ) {
            // Collection

            return new Entity\Collection($this->typeOption, $value);
        } elseif ($this->type === self::TYPE_ENTITY
            && Validator::isTraversable($value)
        ) {
            // Entity

            return Entity\Reflection::load($this->typeOption)->createEntity($value);
        }

        throw new Exception\InvalidArgumentException(
            "Can not convert value on property '" . $this->name
            . "' automatically!",
            $value
        );
    }
216017
protected function normalizeValue($value)
    {
        if (false === $value) {
            return $value;
        }

        $value = $this->remapXml($value);

        $normalized = array();
        foreach ($value as $name => $val) {
            if (isset($this->children[$name])) {
                $normalized[$name] = $this->children[$name]->normalize($val);
                unset($value[$name]);
            } elseif (!$this->removeExtraKeys) {
                $normalized[$name] = $val;
            }
        }

        // if extra fields are present, throw exception
        if (count($value) && !$this->ignoreExtraKeys) {
            $msg = sprintf('Unrecognized option%s "%s" under "%s"', 1 === count($value) ? '' : 's', implode(', ', array_keys($value)), $this->getPath());
            $ex = new InvalidConfigurationException($msg);
            $ex->setPath($this->getPath());

            throw $ex;
        }

        return $normalized;
    }
37492
protected function convertType(string $value)
    {
        $val = \mb_strtolower($value);
        if ($val === 'true') {
            return true;
        } elseif ($val === 'false') {
            return false;
        } elseif ($val === 'null') {
            return null;
        } elseif (\mb_strpos($val, '.') !== false && \is_numeric($val)) {
            return (float) $val;
        } elseif (\is_numeric($val)) {
            return (int) $val;
        }

        return $this->replaceVariables($value);
    }
318967
public function getOptionForValue($value) : IFieldOption
    {
        if ($this->specificFieldOptionLoader) {
            $fieldOption = call_user_func($this->specificFieldOptionLoader, $value);

            if (!$fieldOption) {
                throw InvalidArgumentException::format('Invalid option supplied to %s: %s', __METHOD__, $value);
            }

            return $fieldOption;
        }

        return (new ArrayFieldOptions(call_user_func($this->fieldOptionsLoader)))->getOptionForValue($value);
    }
423761
protected function setValue($value)
    {
        if (is_string($value)) {
            $this->value = $value;
        } else {
            throw new \InvalidArgumentException(
                "Invalid type '" . gettype($value). "' for argument 'value' given."
            );
        }

        return $this;
    }
562448
public function transform($value)
    {
        if ($value instanceof JsonSerializable || $value instanceof StdClass) {
            return json_encode($value);
        }

        // If a toJson() method exists, the object can cast itself automatically.
        if (method_exists($value, 'toJson')) {
            return $value;
        }

        // Otherwise, if the object doesn't even have a __toString() method, we can't proceed.
        if (! method_exists($value, '__toString')) {
            throw new \Exception('Cannot transform this object to JavaScript.');
        }

        return "'{$value}'";
    }
46405
protected function validateType($value)
    {
        if (!$value instanceof Closure && $value !== null) {
            $exception = new InvalidTypeException(sprintf(
                'Invalid type for path "%s". Expected closure, but got %s.',
                $this->getPath(),
                gettype($value)
            ));

            if ($hint = $this->getInfo()) {
                $exception->addHint($hint);
            }

            $exception->setPath($this->getPath());

            throw $exception;
        }
    }
616902
public function filter($value)
    {
        try {
            $result = $this->normalizeDateTime($value);
        } catch (\Exception $e) {
            // DateTime threw an exception, an invalid date string was provided
            throw new Exception\InvalidArgumentException('Invalid date string provided', $e->getCode(), $e);
        }

        if ($result === false) {
            return $value;
        }

        return $result;
    }
1231065
public static function validate($value)
    {
        $reflector = new ReflectionClass(static::class);

        foreach ($reflector->getConstants() as $allowedValue) {
            if ($value === $allowedValue) {
                return $allowedValue;
            }
        }

        throw new ArgumentException(
            sprintf(
                'Invalid value `%s` on enum `%s`',
                $value,
                static::class
            )
        );
    }
103205
protected function verifyElement($value)
    {
        if (!$this->elementType->isOfType($value)) {
            throw Exception\InvalidArgumentException::format(
                'Invalid element supplied to collection: expecting type of %s, %s given',
                $this->elementType->asTypeString(), Debug::getType($value)
            );
        }
    }
1058386
public static function getCharacterSetEciByValue(int $value) : ?self
    {
        if ($value < 0 || $value >= 900) {
            throw new InvalidArgumentException('Value must be between 0 and 900');
        }

        $valueToEci = self::valueToEci();

        if (! array_key_exists($value, $valueToEci)) {
            return null;
        }

        return $valueToEci[$value];
    }
951143
public function normalizeValue($value)
	{
		$className  = $this->getFilterClassName();
		if ($value instanceof $className)
		{
			return $value;
		}

		if (!is_callable($value))
		{
			throw new InvalidArgumentException('Filter ' . var_export($value, true) . ' is neither callable nor an instance of ' . $className);
		}

		return new $className($value);
	}
1056266
public function setWhen($value)
    {
        if (!is_callable($value) && $value !== null) {
            throw new InvalidArgumentException('When parameter must be callable type');
        }
        $this->when = $value;

        return $this;
    }
618040
public function hydrate($value)
    {
        if ($value instanceof MongoId) {
            return (string)$value;
        }
        
        if ($this->nullable && $value === null) {
            return null;
        }
        
        throw new Exception\InvalidArgumentException(sprintf(
            'Invalid value: must be an instance of MongoId, "%s" given.',
            is_object($value) ? get_class($value) : gettype($value)
        ));
    }
894822
private function convertType($value)
    {
        $type = gettype($value);

        if ($this->type === $type || !is_scalar($value)) {
            return $value;
        }

        switch ($this->type) {
            case self::TYPE_BOOL:
                return $this->convertBool($value);
            case self::TYPE_INT:
                return $this->convertInt($value);
            case self::TYPE_FLOAT:
                return $this->convertFloat($value);
            default:
                return $this->convertString($value);
        }
    }
598741
protected function checkType($value)
    {
        if (is_string($value) !== true) {
            $msg = "The Identifier Datatype only accepts to store identifier values.";
            throw new InvalidArgumentException($msg);
        } elseif ($value === '') {
            $msg = "The Identifier Datatype do not accept empty strings as valid identifiers.";
            throw new InvalidArgumentException($msg);
        }
    }
1276940
public function addValue($value)
    {
        if ($this->datatype == 'string') {
            if (is_string($value) == false) {
                throw new InvalidArgumentException('The value(' . $value . ') is not string but the restriction demands it');
            }
        } elseif ($this->datatype == 'integer') {
            // The value comes as string from the wsdl
            if (is_string($value)) {
                $value = intval($value);
            }

            if (is_int($value) == false) {
                throw new InvalidArgumentException('The value(' . $value . ') is not int but the restriction demands it');
            }
        } else {
            if ($value == null) {
                throw new InvalidArgumentException('Value(' . $value . ') is null');
            }
        }

        $this->values[] = $value;
    }