You are here

public function WSClientServiceDescription::invoke in Web service client 7

Invoke a service via its endpoint.

Parameters

string $operation: The name of the operation to execute.

array $arguments: Arguments to pass to the service with this operation. If the array keys do not match the parameter information, the array values are assigned in sequential order according to the order of parameters.

1 call to WSClientServiceDescription::invoke()
WSClientServiceDescription::__call in ./wsclient.inc
Magic method to catch service invocations.

File

./wsclient.inc, line 46
Web service client - include file.

Class

WSClientServiceDescription
Class representing web service descriptions.

Code

public function invoke($operation, array $arguments) {
  if (!isset($this->operations[$operation])) {
    throw new WSClientException('Operation %operation does not exist for web service %service.', array(
      '%operation' => $operation,
      '%service' => $this->name,
    ));
  }
  $named_arguments = array();
  if (isset($this->operations[$operation]['parameter'])) {
    $remaining_params = $this->operations[$operation]['parameter'];

    // Assign named arguments and hidden parameters.
    foreach ($this->operations[$operation]['parameter'] as $param => $info) {
      if (isset($arguments[$param])) {
        $named_arguments[$param] = $arguments[$param];
        unset($arguments[$param]);
        unset($remaining_params[$param]);
      }
      elseif ($info['type'] == 'hidden') {
        $named_arguments[$param] = $info['default value'];
        unset($remaining_params[$param]);
      }
    }

    // Assign the rest in sequential order.
    foreach ($remaining_params as $param => $info) {
      $named_arguments[$param] = array_shift($arguments);
    }
  }

  // Fill in global parameters.
  foreach ($this->global_parameters as $param => $info) {
    if (!isset($named_arguments[$param])) {
      $named_arguments[$param] = $info['default value'];
    }
  }

  // Unless explicitly permitted by 'allow null', unset optional parameters
  // with NULL or empty ('') values.
  if (isset($this->operations[$operation]['parameter'])) {
    foreach ($this->operations[$operation]['parameter'] as $param => $info) {
      if (isset($info['optional']) && $info['optional'] && (!isset($info['allow null']) || !$info['allow null']) && ($named_arguments[$param] === '' || !isset($named_arguments[$param]))) {
        unset($named_arguments[$param]);
      }
    }
  }
  drupal_alter('wsclient_invoke_arguments', $named_arguments, $operation, $this);
  $response = $this
    ->endpoint()
    ->call($operation, $named_arguments);
  drupal_alter('wsclient_invoke_response', $response, $operation, $this);
  return $response;
}