You are here

class HttpClientBaseFormatter in Http Client 6.2

Same name and namespace in other branches
  1. 7.2 includes/HttpClient.inc \HttpClientBaseFormatter

A base formatter to format php and json.

Hierarchy

Expanded class hierarchy of HttpClientBaseFormatter

File

includes/HttpClient.inc, line 296

View source
class HttpClientBaseFormatter implements HttpClientFormatter {
  const FORMAT_PHP = 'php';
  const FORMAT_JSON = 'json';
  const FORMAT_FORM = 'form';
  protected $mimeTypes = array(
    self::FORMAT_PHP => 'application/vnd.php.serialized',
    self::FORMAT_JSON => 'application/json',
    self::FORMAT_FORM => 'application/x-www-form-urlencoded',
  );
  protected $format;
  public function __construct($format = self::FORMAT_PHP) {
    $this->format = $format;
  }

  /**
   * Serializes arbitrary data.
   *
   * @param mixed $data
   *  The data that should be serialized.
   * @return string
   *  The serialized data as a string.
   */
  public function serialize($data) {
    switch ($this->format) {
      case self::FORMAT_PHP:
        return serialize($data);
        break;
      case self::FORMAT_JSON:
        return json_encode($data);
        break;
      case self::FORMAT_FORM:
        return http_build_query($data, NULL, '&');
        break;
    }
  }

  /**
   * Unserializes data.
   *
   * @param string $data
   *  The data that should be unserialized.
   * @return mixed
   *  The unserialized data.
   */
  public function unserialize($data) {
    switch ($this->format) {
      case self::FORMAT_PHP:
        if (($response = @unserialize($data)) !== FALSE || $data === serialize(FALSE)) {
          return $response;
        }
        else {
          throw new Exception(t('Unserialization of response body failed.'), 1);
        }
        break;
      case self::FORMAT_JSON:
        $response = json_decode($data);
        if ($response === NULL) {
          throw new Exception(t('Unserialization of response body failed.'), 1);
        }
        return $response;
        break;
      case self::FORMAT_FORM:
        $response = array();
        parse_str($data, $response);
        return $response;
        break;
    }
  }

  /**
   * Returns the mime type to use.
   */
  public function mimeType() {
    return $this->mimeTypes[$this->format];
  }

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

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

}

Members

Namesort descending Modifiers Type Description Overrides
HttpClientBaseFormatter::$format protected property
HttpClientBaseFormatter::$mimeTypes protected property
HttpClientBaseFormatter::accepts public function Return the mime type that the formatter can parse. Overrides HttpClientFormatter::accepts
HttpClientBaseFormatter::contentType public function Return the content type form the data the formatter generates. Overrides HttpClientFormatter::contentType
HttpClientBaseFormatter::FORMAT_FORM constant
HttpClientBaseFormatter::FORMAT_JSON constant
HttpClientBaseFormatter::FORMAT_PHP constant
HttpClientBaseFormatter::mimeType public function Returns the mime type to use.
HttpClientBaseFormatter::serialize public function Serializes arbitrary data. Overrides HttpClientFormatter::serialize
HttpClientBaseFormatter::unserialize public function Unserializes data. Overrides HttpClientFormatter::unserialize
HttpClientBaseFormatter::__construct public function