1

In our backoffice we have a core-system that generates models from the DB. When returning one object we make an instance of stdClass. All the columns from the query-result are set as proprties and when finished processing the query-result the stdClass-object is converted into a (we call it) Decorator-class. Basically the Decorator-class has one proprty $_oObject where the stdClass is stored into. We do this gain control over dynamically created objects. This works all fine.

However, I'm working on a webserver using SOAP. The webservice returns the whole Decorator-object (could possibly have sub-objects, also being Decorator objects, and sub-sub object.. and so on). This structure works perfectly fine with our internal system because we have control over the Decorator-object but for the outside world I want to revert the Decorator-object-structure into a stdClass instance with sub-classes also being stdClasses. Basically I want to remove all the 'nodes' in the print_r-result containing Decorator.

Any ideas how to achieve what I want (see results below). PHP's get_object_vars doesn't return anything and actually I'm stuck..

My sample data:

Decorator Object
(
    [oClass:Decorator:private] => stdClass Object
        (
            [Id] => 1
            [FAQCategoryId] => 1
            [TitleId] => 1
            [ContentId] => 2
            [Views] => 226
            [DateCreated] => 2011-10-31 11:17:44
            [DateModified] => 
            [Title] => My title..
            [Content] => My content..
            [AttachmentSet] => Array
                (
                    [0] => Decorator Object
                        (
                            [oClass:Decorator:private] => stdClass Object
                                (
                                    [Id] => 1
                                    [LanguageId] => 1
                                    [FAQItemId] => 1
                                    [Attachment] => file1.pdf
                                )

                        )

                    [1] => Decorator Object
                        (
                            [oClass:Decorator:private] => stdClass Object
                                (
                                    [Id] => 2
                                    [LanguageId] => 1
                                    [FAQItemId] => 1
                                    [Attachment] => file2.pdf
                                )

                        )

                )
        )
)

I want to convert it into:

stdClass Object
(
    [Id] => 1
    [FAQCategoryId] => 1
    [TitleId] => 1
    [ContentId] => 2
    [Views] => 226
    [DateCreated] => 2011-10-31 11:17:44
    [DateModified] => 
    [Title] => My title..
    [Content] => My content..
    [AttachmentSet] => Array
        (
            [0] => stdClass Object
                (
                    [Id] => 1
                    [LanguageId] => 1
                    [FAQItemId] => 1
                    [Attachment] => file1.pdf
                )
            [1] => stdClass Object
                (
                    [Id] => 2
                    [LanguageId] => 1
                    [FAQItemId] => 1
                    [Attachment] => file2.pdf
                )
        )
)
Ben Fransen
  • 10,884
  • 18
  • 76
  • 129

1 Answers1

0

I've figured it out. In my case I've created a function that returns the stdObject of a Decoarator object. In my Controller_Core-class I've made function that recursively handles a given object and returns the whole structure as one stdClass.

My code, if it is helpful to someone:

/**
 * Controller_Core::RevertToStdClass
 *
 * @params: Decorator   $oObject
 * @return: stdClass    $oObject
 **/
public function RevertToStdClass(Decorator $oObject)
{   
    if(is_a($oObject, "Decorator"))
    {
        $oObject = $oObject->ReturnStdObject();
    }

    $aProperties = get_object_vars($oObject);

    foreach($aProperties as $sProperty => $mValue)
    {
        if(is_array($mValue))
        {
            foreach($mValue as $mIndex => $mSubValue)
            {
                if(is_a($mSubValue, "Decorator"))
                {
                    $oObject->{$sProperty}[$mIndex] = $this->RevertToStdClass($mSubValue);
                }
            }
        }
        else
        {
            if(is_a($mValue, "Decorator"))
            {
                $oObject->{$sProperty} = $this->RevertToStdClass($oObject->{$sProperty});
            }
        }
    }

    return $oObject;    
}
Ben Fransen
  • 10,884
  • 18
  • 76
  • 129