/** * @param MethodResponse $response * @return Response * @throws \Seven\RpcBundle\Exception\UnknownMethodResponse */ public function createHttpResponse(MethodResponse $response) { $data = array('jsonrpc' => '2.0'); if ($response instanceof MethodReturn) { $data['result'] = $response->getReturnValue(); } elseif ($response instanceof MethodFault) { $data['error'] = array('code' => $response->getCode(), 'message' => $response->getMessage()); } else { throw new UnknownMethodResponse("Unknown MethodResponse instance"); } if ($response->getCallId()) { $data['id'] = $response->getCallId(); } return new Response(json_encode($data), 200, array('content-type' => 'text/json')); }
/** * @param MethodResponse $response * @throws \Seven\RpcBundle\Exception\UnknownMethodResponse * @return Response */ public function createHttpResponse(MethodResponse $response) { $document = new \DOMDocument("1.0", "UTF-8"); $document->appendChild($responseEl = $document->createElement("methodResponse")); if ($response instanceof MethodReturn) { $paramsEl = $document->createElement("params"); $paramEl = $document->createElement("param"); $responseEl->appendChild($paramsEl); $paramsEl->appendChild($paramEl); $paramEl->appendChild($this->pack($document, $response->getReturnValue(), $response->getReturnType())); } elseif ($response instanceof MethodFault) { $responseEl->appendChild($faultEl = $document->createElement("fault")); $faultEl->appendChild($this->pack($document, array('faultCode' => $response->getCode(), 'faultString' => $response->getMessage()), ValueType::Object)); } else { throw new UnknownMethodResponse("Unknown MethodResponse instance"); } return new Response($document->saveXML(), 200, array('content-type' => 'text/xml')); }