From a555279e0b3c5e56cb30ede4d5ba28c1294480d2 Mon Sep 17 00:00:00 2001 From: konrad Date: Sun, 4 Jul 2010 16:50:47 +0000 Subject: [PATCH] add automatic conversion of objects to associative arrays git-svn-id: https://silmor.de/svn/softmagic/pack/trunk@523 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- phpbase/object.php | 8 ++++++++ woc/phpout.cpp | 33 +++++++++++++++++++++++++++++++++ woc/phpout.h | 2 ++ 3 files changed, 43 insertions(+), 0 deletions(-) diff --git a/phpbase/object.php b/phpbase/object.php index 57253e1..28e350c 100644 --- a/phpbase/object.php +++ b/phpbase/object.php @@ -23,6 +23,14 @@ class WObject { } return $list; } + + /**helper function for classy objects that calls getPropertyArray if available*/ + protected static function objectToArray($obj) + { + if(!is_subclass_of($obj,"WObject"))return null; + if(!method_exists($obj,"propertyArray"))return null; + return $obj->propertyArray(); + } }; ?> diff --git a/woc/phpout.cpp b/woc/phpout.cpp index fea93d0..42675c8 100644 --- a/woc/phpout.cpp +++ b/woc/phpout.cpp @@ -347,6 +347,9 @@ void WocPHPServerOut::newClass(const WocClass&cls) //de-serializer tf.write(classDeserializers(cls).toAscii()); + //prop to array conversion + tf.write(classPropertiesList(cls).toAscii()); + //end of class code="\n//end of class\n};\n"; tf.write(code.toAscii()); @@ -414,6 +417,36 @@ QString WocPHPServerOut::classProperties(const WocClass&cls) return code; } +QString WocPHPServerOut::classPropertiesList(const WocClass&cls) +{ + QString code; + QStringList k=cls.propertyNames(); + code+="public function propertyArray(){\n\treturn array("; + for(int i=0;i"; + //is it a list? + if(cls.propertyIsList(k[i])){ + //lists... + //object? + if(cls.propertyIsObject(k[i])) + code+="array_map(\"WObject::objectToArray\",$this->prop_"+k[i]+")"; + else + code+="$this->prop_"+k[i]; + }else{ + //non-lists... + //object? + if(cls.propertyIsObject(k[i])) + code+="WObject::objectToArray($this->prop_"+k[i]+")"; + else + code+="$this->prop_"+k[i]; + } + } + code+=");\n}\n"; + + return code; +} + QString WocPHPServerOut::classPropertyValidator(const WocClass&cls,QString prop) { QString code; diff --git a/woc/phpout.h b/woc/phpout.h index ee751bd..3c2d9f0 100644 --- a/woc/phpout.h +++ b/woc/phpout.h @@ -66,6 +66,8 @@ class WocPHPServerOut:public WocOutput QString classPropertyScalarGetters(const WocClass&,QString); /**helper: generate setters for sclar properties*/ QString classPropertyScalarSetters(const WocClass&,QString); + /**helper: generate the property to array converter, eg. for renderers like for Twig*/ + QString classPropertiesList(const WocClass&); /**helper: create info functions (mainly version info) at the start*/ void transInfo(); -- 1.7.2.5