<?php 

/**
 * FormSmarts Webhook
 */


/**
 *  Normalize recursive data structure to key-value pairs.
 */
class FormSmartsParameterNormalizer {
  
  private $norm_params = NULL;

  function __construct() {
    $this->norm_params = array();
  }

  public function normalize($parameters) {
    foreach ($parameters as $k => $v) $this->serialize($k, $v);
    return $this->norm_params;
  }

  private function serialize($key, $val) {
    if (! is_array($val) && ! is_object($val)) {
      // not an array or object
      $this->norm_params[$key] = $val;
      return;
    }
    if (is_array($val)) { 
      // array but not associative: decorate key with index
      $i = 0;
      foreach ($val as $v) {
	$this->serialize($key . '_' . (string) $i, $v);
	$i++;
      }
      return;
    }
    // associative array: decorate key with sub-key name 
    foreach ($val as $k => $v) $this->serialize($key . '_' . $k, $v);
  }
}



/*

Example input data structure:

{  
   "api_id": "FSEVAPI", 
   "api_version": "0.4",
   "api_date": "2012-04-30T16:11:04",
   "form_id": "lqh",
   "form_name": "File Upload Demo Form",
   "fs_ref_num": "6GZTWKU3HV7DAV1R8YE92L41B", 
   "fields": 
      [
         {  
            "field_value": "Bob", 
            "field_name": "Full Name", 
            "field_id": 1226194,
            "field_datatype": "name"
         }, 
         {
            "field_value": "bob@example.com", 
            "field_name": "Email", 
            "field_id": 1226205, 
            "field_datatype": "email"
         }, 
         {
            "field_id": 1226216, 
            "field_datatype": "attachment", 
            "field_value": "",
            "field_name": "Upload Your Picture",
            "attachment_filename": "bg-v2.0.png", 
            "attachment_url":
"http://formsmarts.com/download/6GZTWKU3HV7DAV1R8YE92L41B-1Q1"
         }, 
         {
            "field_value": "This is a test comment.\r\nThank you\r\n\r\nBye,\r\nBob",
            "field_name": "Any Comments", 
            "field_id": 1237447, 
            "field_datatype": "text"
         }
      ]
}


Example output data structure:


{  
   'api_date': '2012-04-30T16:11:04',
   'api_id': 'FSEVAPI',
   'api_version': '0.4',
   'fields_0_field_datatype': 'name',
   'fields_0_field_id': 1226194,
   'fields_0_field_name': 'Full Name',
   'fields_0_field_value': 'Bob',
   'fields_1_field_datatype': 'email',
   'fields_1_field_id': 1226205,
   'fields_1_field_name': 'Email',
   'fields_1_field_value': 'bob@example.com',
   'fields_2_attachment_filename': 'bg-v2.0.png',
   'fields_2_attachment_url': "http://formsmarts.com/download/6GZTWKU3HV7DAV1R8YE92L41B-1Q1"
   'fields_2_field_datatype': 'attachment',
   'fields_2_field_id': 1226216,
   'fields_2_field_name': 'Upload Your Picture',
   'fields_2_field_value': '',
   'fields_3_field_datatype': 'text',
   'fields_3_field_id': 123744,
   'fields_3_field_name': 'Any Comments',
   'fields_3_field_value': '"This is a test comment.\r\nThank you\r\n\r\nBye,\r\nBob',
   'form_id': 'lqh',
   'form_name': 'File Upload Demo Form',
   'fs_ref_num': '6GZTWKU3HV7DAV1R8YE92L41B'
};  

 */

?>
