src/Entity/Reflection/Property.php
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
        );
    }
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
        );
    }
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 ) ; }
Try to convert value on required type automatically

@param mixed $value

@return mixed

@throws Exception\InvalidArgumentException
Try to convert value on required type automatically