Skip to content

Instantly share code, notes, and snippets.

@phimage
Last active April 15, 2018 07:13
Show Gist options
  • Save phimage/80de171bcaca172879902e8ebffafbd4 to your computer and use it in GitHub Desktop.
Save phimage/80de171bcaca172879902e8ebffafbd4 to your computer and use it in GitHub Desktop.
Transform `WEB GET VARIABLES` result into `C_OBJECT`
// Transform `WEB GET VARIABLES` result into `C_OBJECT`
//
// ex: `key=value&flag&multiplekey=value1&multiplekey=value2`
// -> `{ "key": "value", "flag": "", "multiplekey": [ "value1", "value2" ] }`
//
// - parameter objectify: If `True` variable key containing `.`
// will create sub objects.
// ex: `object.first=value&object.second=1`
// -> `{ "object": { "first": "value", "second": "1" } }`
//
C_TEXT($Txt_key;$Txt_buffer)
C_BOOLEAN($Boo_objectify)
C_COLLECTION($Col_buffer)
C_OBJECT($Obj_buffer)
C_LONGINT($Lon_i)
ARRAY TEXT($tTxt_nameArray;0)
ARRAY TEXT($tTxt_valueArray;0)
// --------------------------------------------------
// If objectify
$Boo_objectify:=Bool($1)
// --------------------------------------------------
WEB GET VARIABLES($tTxt_nameArray;$tTxt_valueArray)
$Obj_result:=New object
For ($Lon_i;1;Size of array($tTxt_nameArray);1)
$Txt_key:=$tTxt_nameArray{$Lon_i}
$Obj_buffer:=$Obj_result
If ($Boo_objectify & (Position(".";$Txt_key)>0))
$Col_buffer:=Split string($Txt_key;".")
$Txt_key:=$Col_buffer.pop()
For each ($Txt_buffer;$Col_buffer)
If ($Obj_buffer[$Txt_buffer]=Null)
$Obj_buffer[$Txt_buffer]:=New object
End if
$Obj_buffer:=$Obj_buffer[$Txt_buffer]
End for each
End if
// Register the value.
// If multiple value for same key, create a collection
Case of
//________________________________________
: ($Obj_buffer[$Txt_key]=Null)
$Obj_buffer[$Txt_key]:=$tTxt_valueArray{$Lon_i}
//________________________________________
: (Value type($Obj_buffer[$Txt_key])=Is collection)
$Obj_buffer[$Txt_key].push($tTxt_valueArray{$Lon_i})
//________________________________________
: (Value type($Obj_buffer[$Txt_key])=Is text)
$Txt_buffer:=$Obj_buffer[$Txt_key]
$Obj_buffer[$Txt_key]:=New collection($Txt_buffer;$tTxt_valueArray{$Lon_i})
//________________________________________
Else
$Obj_buffer[$Txt_key]:=$tTxt_valueArray{$Lon_i}
//________________________________________
End case
End for
// --------------------------------------------------
$0:=$Obj_result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment