You are here

private function RESTServer::getControllerArguments in Services 6.3

Same name and namespace in other branches
  1. 7.3 servers/rest_server/includes/RESTServer.inc \RESTServer::getControllerArguments()

Parses controller arguments from request

Parameters

array $controller: The controller definition

array $path:

string $method: The method used to make the request

array $sources: An array of the sources used for getting the arguments for the call

Return value

void

1 call to RESTServer::getControllerArguments()
RESTServer::handle in servers/rest_server/includes/RESTServer.inc
Handles the call to the REST server

File

servers/rest_server/includes/RESTServer.inc, line 195
Class for handling REST calls.

Class

RESTServer
@file Class for handling REST calls.

Code

private function getControllerArguments($controller, $path, $method) {
  $data = $this
    ->parseRequest($method, $controller);
  $headers = $this
    ->parseRequestHeaders();
  $sources = array(
    'path' => $path,
    'param' => $_GET,
    'data' => $data,
    'headers' => $headers,
  );

  // Map source data to arguments.
  $arguments = array();
  if (isset($controller['args'])) {
    foreach ($controller['args'] as $i => $info) {

      // Fill in argument from source
      if (isset($info['source'])) {
        if (is_array($info['source'])) {
          list($source) = array_keys($info['source']);
          $key = $info['source'][$source];
          if (isset($sources[$source][$key])) {
            $arguments[$i] = $sources[$source][$key];
          }
        }
        else {
          if (isset($sources[$info['source']])) {
            $arguments[$i] = $sources[$info['source']];
          }
        }

        // Convert to array if argument expected to be array.
        if ($info['type'] == 'array' && isset($arguments[$i])) {
          $arguments[$i] = (array) $arguments[$i];
        }
      }

      // When argument isn't set, insert default value if provided or
      // throw a exception if the argument isn't optional.
      if (!isset($arguments[$i])) {
        if (!isset($info['optional']) || !$info['optional']) {
          return services_error(t('Missing required argument @arg', array(
            '@arg' => $info['name'],
          )), 401);
        }

        // Set default value or NULL if default value is not set.
        $arguments[$i] = isset($info['default value']) ? $info['default value'] : NULL;
      }
    }
  }
  return $arguments;
}