function encode($response)
{
$cls = $this->responseFactory;
$encode_as = $response->whichEncoding();
if ($encode_as == Auth_OpenID_ENCODE_KVFORM) {
$wr = new $cls(null, null, $response->encodeToKVForm());
if (is_a($response, 'Auth_OpenID_ServerError')) {
$wr->code = AUTH_OPENID_HTTP_ERROR;
}
} else {
if ($encode_as == Auth_OpenID_ENCODE_URL) {
$location = $response->encodeToURL();
$wr = new $cls(AUTH_OPENID_HTTP_REDIRECT, array('location' => $location));
} else {
if ($encode_as == Auth_OpenID_ENCODE_HTML_FORM) {
$wr = new $cls(AUTH_OPENID_HTTP_OK, array(), $response->toHTML());
} else {
return new Auth_OpenID_EncodingError($response);
}
}
}
/* Allow the response to carry a custom error code (ex: for Association errors) */
if (isset($response->code)) {
$wr->code = $response->code;
}
return $wr;
}