class HttpClientBaseFormatter in Http Client 6.2
Same name and namespace in other branches
- 7.2 includes/HttpClient.inc \HttpClientBaseFormatter
A base formatter to format php and json.
Hierarchy
- class \HttpClientBaseFormatter implements HttpClientFormatter
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
HttpClientBaseFormatter:: |
protected | property | ||
HttpClientBaseFormatter:: |
protected | property | ||
HttpClientBaseFormatter:: |
public | function |
Return the mime type that the formatter can parse. Overrides HttpClientFormatter:: |
|
HttpClientBaseFormatter:: |
public | function |
Return the content type form the data the formatter generates. Overrides HttpClientFormatter:: |
|
HttpClientBaseFormatter:: |
constant | |||
HttpClientBaseFormatter:: |
constant | |||
HttpClientBaseFormatter:: |
constant | |||
HttpClientBaseFormatter:: |
public | function | Returns the mime type to use. | |
HttpClientBaseFormatter:: |
public | function |
Serializes arbitrary data. Overrides HttpClientFormatter:: |
|
HttpClientBaseFormatter:: |
public | function |
Unserializes data. Overrides HttpClientFormatter:: |
|
HttpClientBaseFormatter:: |
public | function |