src/Kisma/Core/Utility/Convert.php
public static function toObject( $object )
    {
        //	If we can't iterate over the thing, we bail
        if ( !is_object( $object ) && !is_array( $object ) && !( $object instanceof \Traversable ) )
        {
            return null;
        }

        if ( is_array( $object ) )
        {
            //	Convert to an object
            $_properties = new \stdClass();

            foreach ( $object as $_key => $_value )
            {
                $_properties->{$_key} = $_value;
            }
        }
        else
        {
            $_me = new \ReflectionObject( $object );
            $_properties = $_me->getProperties();
        }

        //	We'll return this
        $_obj = new \stdClass();

        if ( !empty( $_properties ) )
        {
            if ( is_object( $object ) )
            {
                $_myClass = get_class( $object );
            }
            else
            {
                $_myClass = '_array_';
            }

            foreach ( $_properties as $_property )
            {
                //	Only want properties of $object hierarchy...
                if ( isset( $_property->class ) )
                {
                    $_class = new \ReflectionClass( $_property->class );

                    if ( !empty( $_class ) && !$_class->isInstance( $object ) && !$_class->isSubclassOf( $_myClass ) )
                    {
                        unset( $_class );
                        continue;
                    }

                    unset( $_class );
                }

                try
                {
                    $_realPropertyName = $_propertyName = ltrim( $_property->name, '_ ' );

                    if ( false !== strpos( $_propertyName, '_' ) )
                    {
                        $_propertyName = Inflector::tag( $_propertyName );
                    }

                    $_getter = 'get' . $_propertyName;

                    if ( method_exists( $object, $_getter ) )
                    {
                        $_propertyValue = $object->{$_getter}();

                        if ( !is_scalar( $_propertyValue ) )
                        {
                            $_propertyValue = self::toObject( $_propertyValue );
                        }

                        $_obj->{$_realPropertyName} = $_propertyValue;
                    }
                }
                catch ( \Exception $_ex )
                {
                    //	Just ignore, not a valid property if we can't read it with a getter
                }
            }
        }

        return $_obj;
    }
public static function toObject( $object )
    {
        //	If we can't iterate over the thing, we bail
        if ( !is_object( $object ) && !is_array( $object ) && !( $object instanceof \Traversable ) )
        {
            return null;
        }

        if ( is_array( $object ) )
        {
            //	Convert to an object
            $_properties = new \stdClass();

            foreach ( $object as $_key => $_value )
            {
                $_properties->{$_key} = $_value;
            }
        }
        else
        {
            $_me = new \ReflectionObject( $object );
            $_properties = $_me->getProperties();
        }

        //	We'll return this
        $_obj = new \stdClass();

        if ( !empty( $_properties ) )
        {
            if ( is_object( $object ) )
            {
                $_myClass = get_class( $object );
            }
            else
            {
                $_myClass = '_array_';
            }

            foreach ( $_properties as $_property )
            {
                //	Only want properties of $object hierarchy...
                if ( isset( $_property->class ) )
                {
                    $_class = new \ReflectionClass( $_property->class );

                    if ( !empty( $_class ) && !$_class->isInstance( $object ) && !$_class->isSubclassOf( $_myClass ) )
                    {
                        unset( $_class );
                        continue;
                    }

                    unset( $_class );
                }

                try
                {
                    $_realPropertyName = $_propertyName = ltrim( $_property->name, '_ ' );

                    if ( false !== strpos( $_propertyName, '_' ) )
                    {
                        $_propertyName = Inflector::tag( $_propertyName );
                    }

                    $_getter = 'get' . $_propertyName;

                    if ( method_exists( $object, $_getter ) )
                    {
                        $_propertyValue = $object->{$_getter}();

                        if ( !is_scalar( $_propertyValue ) )
                        {
                            $_propertyValue = self::toObject( $_propertyValue );
                        }

                        $_obj->{$_realPropertyName} = $_propertyValue;
                    }
                }
                catch ( \Exception $_ex )
                {
                    //	Just ignore, not a valid property if we can't read it with a getter
                }
            }
        }

        return $_obj;
    }
public static function toObject ( $ object ) { //	If we can't iterate over the thing, we bail if ( ! is_object ( $ object ) && ! is_array ( $ object ) && ! ( $ object instanceof \ Traversable ) ) { return null ; } if ( is_array ( $ object ) ) { //	Convert to an object $ _properties = new \ stdClass ( ) ; foreach ( $ object as $ _key => $ _value ) { $ _properties -> { $ _key } = $ _value ; } } else { $ _me = new \ ReflectionObject ( $ object ) ; $ _properties = $ _me -> getProperties ( ) ; } //	We'll return this $ _obj = new \ stdClass ( ) ; if ( ! empty ( $ _properties ) ) { if ( is_object ( $ object ) ) { $ _myClass = get_class ( $ object ) ; } else { $ _myClass = '_array_' ; } foreach ( $ _properties as $ _property ) { //	Only want properties of $object hierarchy... if ( isset ( $ _property -> class ) ) { $ _class = new \ ReflectionClass ( $ _property -> class ) ; if ( ! empty ( $ _class ) && ! $ _class -> isInstance ( $ object ) && ! $ _class -> isSubclassOf ( $ _myClass ) ) { unset ( $ _class ) ; continue ; } unset ( $ _class ) ; } try { $ _realPropertyName = $ _propertyName = ltrim ( $ _property -> name , '_ ' ) ; if ( false !== strpos ( $ _propertyName , '_' ) ) { $ _propertyName = Inflector :: tag ( $ _propertyName ) ; } $ _getter = 'get' . $ _propertyName ; if ( method_exists ( $ object , $ _getter ) ) { $ _propertyValue = $ object -> { $ _getter } ( ) ; if ( ! is_scalar ( $ _propertyValue ) ) { $ _propertyValue = self :: toObject ( $ _propertyValue ) ; } $ _obj -> { $ _realPropertyName } = $ _propertyValue ; } } catch ( \ Exception $ _ex ) { //	Just ignore, not a valid property if we can't read it with a getter } } } return $ _obj ; }
Dynamically generates the object from the declared properties of the given object or array

@param array|object $object

@return \stdClass
Dynamically generates the object from the declared properties of the given object or array