/**
  * {@inheritdoc}
  *
  * @param StreamInterface $content
  *   (Optional) The content to override the input stream with. This is mainly
  *   here for testing purposes.
  *
  */
 public static function fromGlobals(array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null, StreamInterface $content = null)
 {
     $server = static::normalizeServer($server ?: $_SERVER);
     $files = static::normalizeFiles($files ?: $_FILES);
     $headers = static::marshalHeaders($server);
     // static::get() has a default parameter, however, if the header is set but
     // the value is NULL, e.g. during a drush operation, the NULL result is
     // returned, instead of the default.
     $method = strtoupper(static::get('REQUEST_METHOD', $server) ?: 'GET');
     $request = new JsonRequest($server, $files, static::marshalUriFromServer($server, $headers), $method, $content ?: 'php://input', $headers);
     $is_json = strpos(static::get('CONTENT_TYPE', $server), 'application/json') !== FALSE;
     $vars_in_body = in_array($method, ['PUT', 'POST', 'PATCH', 'DELETE']);
     if ($vars_in_body) {
         $data = $content ? $content->getContents() : file_get_contents('php://input');
         $body = $body ?: [];
         $new = [];
         if ($is_json) {
             $new = json_decode($data, TRUE);
         } else {
             parse_str($data, $new);
         }
         // Merge in data to $body.
         $body = array_merge($body, $new);
     }
     return $request->withCookieParams($cookies ?: $_COOKIE)->withQueryParams($query ?: $_GET)->withParsedBody($body ?: $_POST);
 }
Example #2
0
 public function testSetDataWorksAsAdvertised()
 {
     $request = new JsonRequest();
     $request->initialize(['foo' => 'fooget', 'bar' => 'barget'], ['foo' => 'foopost', 'bar' => 'barpost'], [], [], [], $this->server, $this->content);
     $data = ['new' => 'value', 'foo' => 'newfoo'];
     $request->setData($data);
     $this->assertEquals($request->query->get('new'), 'value');
     $this->assertEquals($request->query->get('foo'), 'newfoo');
     $request->setMethod('PATCH');
     $request->setData($data);
     $this->assertEquals($request->request->get('new'), 'value');
     $this->assertEquals($request->request->get('foo'), 'newfoo');
 }
Example #3
0
 /**
  * Handles required parameters.
  *
  * @param ResourceInterface $resource
  *   The resource being called.
  * @param JsonRequest $request
  *   The HTTP reqeust.
  *
  * @throws MissingParametersException
  * @throws InvalidParametersException
  *
  */
 protected function handleRequiredParameters(ResourceInterface $resource, JsonRequest $request)
 {
     $required = $resource->getRequiredParameters();
     $params = strtoupper($request->getMethod()) == 'GET' ? $request->getQueryParams() : $request->getParsedBody();
     $invalid = [];
     $missing = [];
     foreach ($required as $param => $callable) {
         if (empty($params[$param])) {
             $missing[] = $param;
             continue;
         }
         if (is_callable($callable) && !$callable($params[$param])) {
             $invalid[] = $param;
         }
     }
     if ($missing) {
         $message = sprintf('Missing required parameter(s): %s', implode(', ', $missing));
         throw new MissingParametersException($message, 400);
     }
     if ($invalid) {
         $message = sprintf('Invalid values for parameter(s): %s', implode(', ', $invalid));
         throw new InvalidParametersException($message, 400);
     }
 }
Example #4
0
 /**
  * Constructor
  *
  * @param \StdClass $user
  *   The Drupal user to call this resource as. Defaults to the current user.
  * @param JsonRequest $request
  *   The request to use to set the context for the resource. Defaults to the
  *   current page request.
  *
  */
 public function __construct(\StdClass $user = NULL, JsonRequest $request = NULL)
 {
     $this->user = $user ?: $GLOBALS['user'];
     $this->request = $request ?: JsonRequest::createFromGlobals();
 }