Skip to content
Request.php 2.63 KiB
Newer Older
Sylvain Lecoy's avatar
Sylvain Lecoy committed
<?php

/**
 * @file
Sylvain Lecoy's avatar
Sylvain Lecoy committed
 * Definition of WoW\Request.
Sylvain Lecoy's avatar
Sylvain Lecoy committed
 */

/**
Sylvain Lecoy's avatar
Sylvain Lecoy committed
 * Defines the WoW\Request class.
Sylvain Lecoy's avatar
Sylvain Lecoy committed
 */
Sylvain Lecoy's avatar
Sylvain Lecoy committed
class WoWRequest {
Sylvain Lecoy's avatar
Sylvain Lecoy committed

  /**
   * The WoW Service.
   *
   * @var WoWServiceInterface
   */
  protected $service;

  /**
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   * Resource URL being linked to. It is the responsibility of the caller to
   * url encode the path: http://$host/api/wow/$path.
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   *
   * @var string
   */
Sylvain Lecoy's avatar
Sylvain Lecoy committed
  protected $path;
Sylvain Lecoy's avatar
Sylvain Lecoy committed

  /**
   * An array of query key/value-pairs (without any URL-encoding) to append to
   * the URL.
   *
   * @var array
   */
  protected $query;

  /**
   * An array containing request headers to send as name/value pairs.
   *
   * @var array
   */
  protected $headers;

  /**
   * Constructs an HttpRequest object.
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   *
   * @param WoWServiceInterface
   *   A WoW Service object.
   * @param string $path
   *   Resource URL being linked to. It is the responsibility of the caller to
   *   url encode the path: http://$host/api/wow/$path.
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   */
Sylvain Lecoy's avatar
Sylvain Lecoy committed
  public function __construct(WoWServiceInterface $service, $path) {
Sylvain Lecoy's avatar
Sylvain Lecoy committed
    $this->service = $service;
Sylvain Lecoy's avatar
Sylvain Lecoy committed
    $this->path = $path;
Sylvain Lecoy's avatar
Sylvain Lecoy committed
    $this->query = array();
    $this->headers = array();
  }

  /**
   * Configures the request with an If-Modified-Since header.
   *
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   * @param int $time
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   *   A Unix time stamp.
   *
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   * @return WoWRequest
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   *   The Request.
   */
Sylvain Lecoy's avatar
Sylvain Lecoy committed
  public function setIfModifiedSince($time) {
    $this->headers['If-Modified-Since'] = gmdate("D, d M Y H:i:s T", $time);
Sylvain Lecoy's avatar
Sylvain Lecoy committed
    return $this;
  }

  /**
   * Configures the request with an API compliant locale value.
   *
   * @param string $language
   *   The language used to determine the locale to set.
   *
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   * @return WoWRequest
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   *   The Request.
   */
Sylvain Lecoy's avatar
Sylvain Lecoy committed
  public function setLocale($language) {
Sylvain Lecoy's avatar
Sylvain Lecoy committed
    // Prepares the query by adding a locale string if supported.
Sylvain Lecoy's avatar
Sylvain Lecoy committed
    if ($locale = $this->service->getLocale($language)) {
Sylvain Lecoy's avatar
Sylvain Lecoy committed
      $this->query['locale'] = $locale;
    }
    return $this;
  }

  /**
   * Adds a query parameter to the request.
   *
   * @param string $key
   *   The query key.
   * @param string|array $value
   *   The query value.
   *
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   * @return WoWRequest
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   *   The Request.
   */
Sylvain Lecoy's avatar
Sylvain Lecoy committed
  public function setQuery($key, $value) {
Sylvain Lecoy's avatar
Sylvain Lecoy committed
    $this->query[$key] = is_array($value) ? implode(",", $value) : $value;
    return $this;
  }

  /**
   * Executes the request.
   *
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   * @return WoWResponse
Sylvain Lecoy's avatar
Sylvain Lecoy committed
   *   The response returned by the service.
   */
Sylvain Lecoy's avatar
Sylvain Lecoy committed
  public function execute() {
Sylvain Lecoy's avatar
Sylvain Lecoy committed
    // The date is used to sign the request, in the following format:
    // Fri, 10 Jun 2011 20:59:24 GMT, but also to time stamp the response.
Sylvain Lecoy's avatar
Sylvain Lecoy committed
    $this->headers['Date'] = gmdate("D, d M Y H:i:s T");
    return $this->service->request($this->path, $this->query, $this->headers);
Sylvain Lecoy's avatar
Sylvain Lecoy committed
  }
}