From edc8701384c4c712e9930ee0105a71d2df2bdb32 Mon Sep 17 00:00:00 2001
From: Yuriy Gerasimov <yuri.gerasimov@gmail.com>
Date: Sat, 2 Apr 2011 22:59:59 +0300
Subject: [PATCH] Issue #954964 by ygerasimov,marcingy: Remove struct type of variable in favour of array.

---
 resources/comment_resource.inc              |   20 +++++------
 resources/file_resource.inc                 |    4 +-
 resources/node_resource.inc                 |   41 +++++++++---------------
 resources/taxonomy_resource.inc             |   18 ++++------
 resources/user_resource.inc                 |   45 ++++++++++++--------------
 servers/rest_server/includes/RESTServer.inc |   17 ++--------
 services.services.api.php                   |    4 +-
 7 files changed, 60 insertions(+), 89 deletions(-)

diff --git a/resources/comment_resource.inc b/resources/comment_resource.inc
index e93c1cc..448989b 100644
--- a/resources/comment_resource.inc
+++ b/resources/comment_resource.inc
@@ -17,7 +17,7 @@ function _comment_resource_definition() {
         'args' => array(
           array(
             'name' => 'comment',
-            'type' => 'struct',
+            'type' => 'array',
             'description' => 'The comment object',
             'source' => 'data',
             'optional' => FALSE,
@@ -60,8 +60,8 @@ function _comment_resource_definition() {
           ),
           array(
             'name' => 'data',
-            'type' => 'struct',
-            'description' => 'The comment object with updated information',
+            'type' => 'array',
+            'description' => 'The comment array with updated information',
             'source' => 'data',
             'optional' => FALSE,
           ),
@@ -174,15 +174,14 @@ function _comment_resource_definition() {
  */
 
 function _comment_resource_create($comment) {
-  $comment = (object) $comment;
 
   // Setup form_state
   $form_state = array();
-  $form_state['values'] = (array)$comment;
+  $form_state['values'] = $comment;
   $form_state['values']['op'] = t('Save');
 
   $comment_empty = array(
-    'nid' => $comment->nid,
+    'nid' => $comment['nid'],
     'cid' => NULL,
   );
 
@@ -225,17 +224,16 @@ function _comment_resource_retrieve($cid) {
  *   Unique identifier for the comment (cid) or FALSE if there was a problem.
  */
 function _comment_resource_update($cid, $comment) {
-  $comment = (object) $comment;
-  $comment->cid = $cid;
+  $comment['cid'] = $cid;
 
   $old_comment = (array) _comment_load($cid);
   if (empty($old_comment)) {
-    return services_error(t('Comment @cid not found', array('@cid' => $comment->cid)), 404);
+    return services_error(t('Comment @cid not found', array('@cid' => $comment['cid'])), 404);
   }
 
   // Setup form_state.
   $form_state = array();
-  $form_state['values'] = (array) $comment;
+  $form_state['values'] = $comment;
   $form_state['values']['op'] = t('Save');
   $form_state['comment'] = $old_comment;
 
@@ -245,7 +243,7 @@ function _comment_resource_update($cid, $comment) {
     return services_error(implode("\n", $errors), 406);
   }
 
-  return $comment->cid;
+  return $cid;
 }
 
 /**
diff --git a/resources/file_resource.inc b/resources/file_resource.inc
index 443ea33..f188f10 100644
--- a/resources/file_resource.inc
+++ b/resources/file_resource.inc
@@ -24,8 +24,8 @@ function _file_resource_definition() {
         'args' => array(
           array(
             'name' => 'file',
-            'type' => 'struct',
-            'description'    => t('An object representing a file.'),
+            'type' => 'array',
+            'description'    => t('An array representing a file.'),
             'source' => 'data',
             'optional' => FALSE,
           ),
diff --git a/resources/node_resource.inc b/resources/node_resource.inc
index ca621ae..bdf5cab 100644
--- a/resources/node_resource.inc
+++ b/resources/node_resource.inc
@@ -27,8 +27,8 @@ function _node_resource_definition() {
             'name' => 'node',
             'optional' => FALSE,
             'source' => 'data',
-            'description' => 'The node object to create',
-            'type' => 'struct',
+            'description' => 'The node array to create',
+            'type' => 'array',
           ),
         ),
         'access callback' => '_node_resource_access',
@@ -96,9 +96,9 @@ function _node_resource_definition() {
           array(
             'name' => 'parameters',
             'optional' => TRUE,
-            'type' => 'struct',
+            'type' => 'array',
             'description' => 'Parameters',
-            'default value' => NULL,
+            'default value' => array(),
             'source' => array('param' => 'parameters'),
           ),
         ),
@@ -141,9 +141,7 @@ function _node_resource_retrieve($nid) {
     $node->uri = services_resource_uri(array('node', $node->nid));
     return $node;
   }
-  else {
-    return services_error('Node nid '. $nid .' not found', 404);
-  }
+  return services_error('Node nid '. $nid .' not found', 404);
 }
 
 /**
@@ -162,9 +160,7 @@ function _node_resource_retrieve($nid) {
  * @see drupal_execute()
  */
 function _node_resource_create($node) {
-  $node = (object)$node;
-
-  if (!isset($node->type)) {
+  if (!isset($node['type'])) {
     return services_error('Missing node type', 406);
   }
 
@@ -172,22 +168,21 @@ function _node_resource_create($node) {
   module_load_include('inc', 'node', 'node.pages');
   $nid = NULL;
   if (module_exists('content') && variable_get('services_use_content_permissions', TRUE)) {
-    $fields = content_fields(NULL, $node->type);
+    $fields = content_fields(NULL, $node['type']);
     foreach ($fields as $field_name => $field_info) {
-      if (isset($node->$field_name)) {
-        $access = module_invoke_all('field_access', 'view', $field_info, $user, $node);
+      if (isset($node[$field_name])) {
+        $access = module_invoke_all('field_access', 'view', $field_info, $user, (object)$node);
         if (in_array(FALSE, $access)) {
-          unset($node->$field_name);
+          unset($node[$field_name]);
         }
       }
     }
   }
   // Setup form_state
-  $values = (array)$node;
   $form_state = array();
-  $form_state['values'] = $values;
+  $form_state['values'] = $node;
   $form_state['values']['op'] = t('Save');
-  $ret = drupal_execute($node->type . '_node_form', $form_state, $node);
+  $ret = drupal_execute($node['type'] . '_node_form', $form_state, (object)$node);
 
   if ($errors = form_get_errors()) {
     return services_error(implode(" ", $errors), 406);
@@ -219,18 +214,16 @@ function _node_resource_create($node) {
  * @see drupal_execute()
  */
 function _node_resource_update($nid, $node) {
-  $node = (object)$node;
-  $node->nid = $nid;
+  $node['nid'] = $nid;
 
-  $old_node = node_load($node->nid);
+  $old_node = node_load($nid);
   if ($old_node->nid) {
     // Load the required includes for drupal_execute
     module_load_include('inc', 'node', 'node.pages');
 
     // Setup form_state.
-    $values = (array)$node;
     $form_state = array();
-    $form_state['values'] = $values;
+    $form_state['values'] = $node;
     $form_state['values']['op'] = t('Save');
     $form_state['node'] = (array)$old_node;
 
@@ -244,7 +237,6 @@ function _node_resource_update($nid, $node) {
     return services_error(t('Node not found'), 404);
   }
 
-  $nid = $node->nid;
   $node = array('nid' => $nid);
   if ($uri = services_resource_uri(array('node', $nid))) {
     $node['uri'] = $uri;
@@ -291,8 +283,7 @@ function _node_resource_delete($nid) {
  *     - Is there an easier syntax we can define which can make the urls
  *       for index requests more straightforward?
  */
-function _node_resource_index($page = 0, $fields = '*', $parameters = array()) {
-  $parameters = (array) $parameters;
+function _node_resource_index($page, $fields, $parameters) {
   $schema = drupal_get_schema('node');
   $where = array();
   $fields = db_escape_string($fields);
diff --git a/resources/taxonomy_resource.inc b/resources/taxonomy_resource.inc
index da47604..70e82b7 100644
--- a/resources/taxonomy_resource.inc
+++ b/resources/taxonomy_resource.inc
@@ -34,8 +34,8 @@ function _taxonomy_resource_definition() {
             'name' => 'term',
             'optional' => FALSE,
             'source' => 'data',
-            'description' => 'The taxonomy term object to create',
-            'type' => 'struct',
+            'description' => 'The taxonomy term array to create',
+            'type' => 'array',
           ),
         ),
         'access callback' => '_taxonomy_resource_access',
@@ -58,7 +58,7 @@ function _taxonomy_resource_definition() {
             'optional' => FALSE,
             'source' => 'data',
             'description' => 'The taxonomy term data to update',
-            'type' => 'struct',
+            'type' => 'array',
           ),
         ),
         'access callback' => '_taxonomy_resource_access',
@@ -130,7 +130,7 @@ function _taxonomy_resource_definition() {
             ),
             array(
               'name'          => 'order',
-              'type'          => 'int',
+              'type'          => 'string',
               'description'   => t('The order clause for the query that retrieve the nodes.'),
               'source'        => 'data',
               'optional'      => TRUE,
@@ -165,8 +165,8 @@ function _taxonomy_resource_definition() {
             'name' => 'vocabulary',
             'optional' => FALSE,
             'source' => 'data',
-            'description' => 'The taxonomy vocabulary object to create',
-            'type' => 'struct',
+            'description' => 'The taxonomy vocabulary array to create',
+            'type' => 'array',
           ),
         ),
         'access callback' => '_taxonomy_resource_access',
@@ -189,7 +189,7 @@ function _taxonomy_resource_definition() {
             'optional' => FALSE,
             'source' => 'data',
             'description' => 'The taxonomy vocabulary data to update',
-            'type' => 'struct',
+            'type' => 'array',
           ),
         ),
         'access callback' => '_taxonomy_resource_access',
@@ -275,7 +275,6 @@ function _taxonomy_term_resource_retrieve($tid) {
  * @see taxonomy_save_term()
  */
 function _taxonomy_term_resource_create($term) {
-  $term = (array) $term;
   return taxonomy_save_term($term);
 }
 
@@ -292,7 +291,6 @@ function _taxonomy_term_resource_create($term) {
  * @see taxonomy_save_term()
  */
 function _taxonomy_term_resource_update($tid, $term) {
-  $term = (array) $term;
   $term['tid'] = $tid;
   return _taxonomy_term_resource_create($term);
 }
@@ -356,7 +354,6 @@ function _taxonomy_vocabulary_resource_retrieve($vid) {
  * @see taxonomy_save_vocabulary()
  */
 function _taxonomy_vocabulary_resource_create($vocabulary) {
-  $vocabulary = (array) $vocabulary;
   return taxonomy_save_vocabulary($vocabulary);
 }
 
@@ -373,7 +370,6 @@ function _taxonomy_vocabulary_resource_create($vocabulary) {
  * @see taxonomy_save_vocabulary()
  */
 function _taxonomy_vocabulary_resource_update($vid, $vocabulary) {
-  $vocabulary = (array) $vocabulary;
   $vocabulary['vid'] = $vid;
   return _taxonomy_vocabulary_resource_create($vocabulary);
 }
diff --git a/resources/user_resource.inc b/resources/user_resource.inc
index 37763d7..12bf49e 100644
--- a/resources/user_resource.inc
+++ b/resources/user_resource.inc
@@ -31,8 +31,8 @@ function _user_resource_definition() {
         'args' => array(
           array(
             'name' => 'account',
-            'type' => 'struct',
-            'description' => 'The user object',
+            'type' => 'array',
+            'description' => 'The user data',
             'source' => 'data',
             'optional' => FALSE,
           ),
@@ -56,8 +56,8 @@ function _user_resource_definition() {
           ),
           array(
             'name' => 'data',
-            'type' => 'struct',
-            'description' => 'The user object with updated information',
+            'type' => 'array',
+            'description' => 'The user data with updated information',
             'source' => 'data',
             'optional' => FALSE,
           ),
@@ -73,9 +73,9 @@ function _user_resource_definition() {
         'access arguments append' => TRUE,
         'args' => array(
           array(
-            'name' => 'nid',
+            'name' => 'uid',
             'type' => 'int',
-            'description' => 'The id of the note to delete',
+            'description' => 'The id of the user to delete',
             'source' => array('path' => '0'),
             'optional' => FALSE,
           ),
@@ -104,7 +104,7 @@ function _user_resource_definition() {
           array(
             'name' => 'parameters',
             'optional' => TRUE,
-            'type' => 'struct',
+            'type' => 'array',
             'description' => 'Parameters',
             'default value' => NULL,
             'source' => array('param' => 'parameters'),
@@ -199,10 +199,10 @@ function _user_resource_create($account) {
   module_load_include('inc', 'user', 'user.pages');
 
   // register a new user
-  $form_state['values'] = (array) $account;
+  $form_state['values'] = $account;
   $form_state['values']['pass'] = array(
-    'pass1' => $account->pass,
-    'pass2' => $account->pass,
+    'pass1' => $account['pass'],
+    'pass2' => $account['pass'],
   );
   $form_state['values']['op'] = t('Create new account');
 
@@ -212,9 +212,7 @@ function _user_resource_create($account) {
   if ($errors = form_get_errors()) {
     return services_error(implode(" ", $errors), 406);
   }
-  else {
-    return $form_state['user'];
-  }
+  return $form_state['user'];
 }
 
 /**
@@ -232,7 +230,7 @@ function _user_resource_create($account) {
  *   The modified user object.
  */
 function _user_resource_update($uid, $account) {
-  $account->uid = $uid;
+  $account['uid'] = $uid;
 
   // Load the required includes for saving profile information
   // with drupal_execute().
@@ -244,16 +242,16 @@ function _user_resource_update($uid, $account) {
   // this checkbox on the user create and edit form (and thus
   // causes problems with drupal_execute()). Therefore we just
   // force it to 0 here.
-  if (isset($account->roles[2])) {
-    $account->roles[2] = 0;
+  if (isset($account['roles'][2])) {
+    $account['roles'][2] = 0;
   }
 
   // If a profile category was passed in, use it. Otherwise default
   // to 'account' (for saving core user data.)
   $category = 'account';
-  if (isset($account->category)) {
-    $category = $account->category;
-    unset($account->category);
+  if (isset($account['category'])) {
+    $category = $account['category'];
+    unset($account['category']);
   }
 
   // Drop any passed in values into the $account var. Anything
@@ -264,17 +262,16 @@ function _user_resource_update($uid, $account) {
 
   $form_state['values']['op'] = 'Save';
   $form_state['values']['_category'] = $category;
-  $form_state['values']['_account'] = $account;
+  $form_state['values']['_account'] = (object)$account;
 
-  $ret = drupal_execute('user_profile_form', $form_state, $account, $category);
+  $ret = drupal_execute('user_profile_form', $form_state, (object)$account, $category);
 
   // Error if needed.
   if ($errors = form_get_errors()) {
     return services_error(implode(" ", $errors), 406);
   }
-  else {
-    return $account;
-  }
+
+  return $account;
 }
 
 /**
diff --git a/servers/rest_server/includes/RESTServer.inc b/servers/rest_server/includes/RESTServer.inc
index 9d1da80..075ff80 100755
--- a/servers/rest_server/includes/RESTServer.inc
+++ b/servers/rest_server/includes/RESTServer.inc
@@ -213,20 +213,9 @@ class RESTServer {
                 $arguments[$i] = $sources[$info['source']][$info['name']];
               }
           }
-          // Convert arrays to objects and objects to arrays to be more tolerant
-          // towards client. Not all formats and languages handle arrays &
-          // objects as php does.
-          switch ($info['type']) {
-            case 'struct':
-              if (isset($arguments[$i]) && is_array($arguments[$i])) {
-                $arguments[$i] = (object)$arguments[$i];
-              }
-              break;
-            case 'array':
-              if (is_object($arguments[$i])) {
-                $arguments[$i] = get_object_vars($arguments[$i]);
-              }
-              break;
+          // Convert to array if argument expected to be array.
+          if ($info['type'] == 'array' && isset($arguments[$i])) {
+            $arguments[$i] = (array)$arguments[$i];
           }
         }
 
diff --git a/services.services.api.php b/services.services.api.php
index 2cc8891..784b93c 100644
--- a/services.services.api.php
+++ b/services.services.api.php
@@ -52,7 +52,7 @@
   *     array containing the following keys:
   *
   *     - name: The name of this argument.
-  *     - type: The data type of this argument (int, string, array, struct)
+  *     - type: The data type of this argument (int, string, array)
   *     - description: Text describing this argument's usage.
   *     - optional: A boolean indicating whether or not this argument is optional.
   *     - source: Where this argument should be retrieved from. This can be
@@ -115,4 +115,4 @@ function hook_services_resources() {
       ),
     ),
   );
-}
\ No newline at end of file
+}
-- 
1.7.0.4

