add automatic conversion of objects to associative arrays
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 4 Jul 2010 16:50:47 +0000 (16:50 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 4 Jul 2010 16:50:47 +0000 (16:50 +0000)
git-svn-id: https://silmor.de/svn/softmagic/pack/trunk@523 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

phpbase/object.php
woc/phpout.cpp
woc/phpout.h

index 57253e1..28e350c 100644 (file)
@@ -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();
+       }
 };
 
 ?>
index fea93d0..42675c8 100644 (file)
@@ -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<k.size();i++){
+               if(i)code+=",";
+               code+="\n\t\t\""+k[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;
index ee751bd..3c2d9f0 100644 (file)
@@ -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();