You are here

class HttpClientCompositeFormatter in Http Client 7.2

Same name and namespace in other branches
  1. 6.2 includes/HttpClient.inc \HttpClientCompositeFormatter

A utility formatter to use for creating assymetrical http client formatters.

Hierarchy

Expanded class hierarchy of HttpClientCompositeFormatter

File

includes/HttpClient.inc, line 412

View source
class HttpClientCompositeFormatter implements HttpClientFormatter {
  private $send = null;
  private $accept = null;

  /**
   * Creates an assymetrical formatter.
   *
   * @param string|HttpClientFormatter $send
   *  Optional. The formatter to use when sending requests. Accepts one of
   *  the HttpClientBaseFormatter::FORMAT_ constants or a HttpClientFormatter
   *  object. Defaults to form encoded.
   * @param string|HttpClientFormatter $accept
   *  Optional. The formatter to use when parsing responses. Accepts one of
   *  the HttpClientBaseFormatter::FORMAT_ constants or a HttpClientFormatter
   *  object. Defaults to json.
   */
  public function __construct($send = HttpClientBaseFormatter::FORMAT_FORM, $accept = HttpClientBaseFormatter::FORMAT_JSON) {
    if (is_string($send)) {
      $send = new HttpClientBaseFormatter($send);
    }
    if (is_string($accept)) {
      $accept = new HttpClientBaseFormatter($accept);
    }
    $this->send = $send;
    $this->accept = $accept;
  }

  /**
   * Serializes arbitrary data to the implemented format.
   *
   * @param mixed $data
   *  The data that should be serialized.
   * @return string
   *  The serialized data as a string.
   */
  public function serialize($data) {
    return $this->send
      ->serialize($data);
  }

  /**
   * Unserializes data in the implemented format.
   *
   * @param string $data
   *  The data that should be unserialized.
   * @return mixed
   *  The unserialized data.
   */
  public function unserialize($data) {
    return $this->accept
      ->unserialize($data);
  }

  /**
   * Return the mime type that the formatter can parse.
   */
  public function accepts() {
    return $this->accept
      ->mimeType();
  }

  /**
   * Return the content type form the data the formatter generates.
   */
  public function contentType() {
    return $this->send
      ->mimeType();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HttpClientCompositeFormatter::$accept private property
HttpClientCompositeFormatter::$send private property
HttpClientCompositeFormatter::accepts public function Return the mime type that the formatter can parse. Overrides HttpClientFormatter::accepts
HttpClientCompositeFormatter::contentType public function Return the content type form the data the formatter generates. Overrides HttpClientFormatter::contentType
HttpClientCompositeFormatter::serialize public function Serializes arbitrary data to the implemented format. Overrides HttpClientFormatter::serialize
HttpClientCompositeFormatter::unserialize public function Unserializes data in the implemented format. Overrides HttpClientFormatter::unserialize
HttpClientCompositeFormatter::__construct public function Creates an assymetrical formatter.