function outputResponse(ResponseItem $responseItem, RestRequestItem $requestItem) { $doc = $this->createXmlDoc(); $requestType = $this->getRequestType($requestItem); $data = $responseItem->getResponse(); // Check to see if this is a single entry, or a collection, and construct either an xml // feed (collection) or an entry (single) if ($responseItem->getResponse() instanceof RestfulCollection) { $totalResults = $responseItem->getResponse()->getTotalResults(); $itemsPerPage = $requestItem->getCount(); $startIndex = $requestItem->getStartIndex(); // The root Feed element $entry = $this->addNode($doc, 'response', ''); // Required Xml fields $this->addNode($entry, 'startIndex', $startIndex); $this->addNode($entry, 'itemsPerPage', $itemsPerPage); $this->addNode($entry, 'totalResults', $totalResults); $responses = $responseItem->getResponse()->getEntry(); foreach ($responses as $response) { // recursively add responseItem data to the xml structure $this->addData($entry, $requestType, $response); } } else { // Single entry = Xml:Entry $entry = $this->addNode($doc, 'response', ''); // addData loops through the responseItem data recursively creating a matching XML structure $this->addData($entry, 'entry', $data['entry']); } $xml = $doc->saveXML(); echo $xml; }
/** * * @param ResponseItem $responseItem * @param RestRequestItem $requestItem */ function outputResponse(ResponseItem $responseItem, RestRequestItem $requestItem) { $response = $responseItem->getResponse(); if ($response instanceof RestfulCollection) { $itemsPerPage = $requestItem->getCount(); if ($itemsPerPage > 0) { $response->itemsPerPage = $itemsPerPage; } } // several service calls return a null value if (!is_null($response)) { $this->encodeAndSendResponse($response); } }
function outputResponse(ResponseItem $responseItem, RestRequestItem $requestItem) { $response = $responseItem->getResponse(); if ($response instanceof RestfulCollection) { $itemsPerPage = $requestItem->getCount(); if ($itemsPerPage > 0) { $response->itemsPerPage = $itemsPerPage; } } // several service calls return a null value if (!is_null($response)) { if (Config::get('debug')) { echo self::json_format(json_encode($response)); // TODO: add a query option to pretty-print json output } else { echo json_encode($response); } } }
function outputResponse(ResponseItem $responseItem, RestRequestItem $requestItem) { $doc = $this->createAtomDoc(); $requestType = $this->getRequestType($requestItem); $data = $responseItem->getResponse(); $params = $requestItem->getParameters(); $userId = isset($params['userId']) ? $params['userId'][0] : ''; $guid = 'urn:guid:' . $userId; $authorName = $_SERVER['HTTP_HOST'] . ':' . $userId; $updatedAtom = date(DATE_ATOM); // Check to see if this is a single entry, or a collection, and construct either an atom // feed (collection) or an entry (single) if ($data instanceof RestfulCollection) { $totalResults = $data->getTotalResults(); $itemsPerPage = $requestItem->getCount(); $startIndex = $requestItem->getStartIndex(); // The root Feed element $entry = $this->addNode($doc, 'feed', '', false, self::$nameSpace); // Required Atom fields $endPos = $startIndex + $itemsPerPage > $totalResults ? $totalResults : $startIndex + $itemsPerPage; $this->addNode($entry, 'title', $requestType . ' feed for id ' . $authorName . ' (' . $startIndex . ' - ' . ($endPos - 1) . ' of ' . $totalResults . ')'); $author = $this->addNode($entry, 'author'); $this->addNode($author, 'uri', $guid); $this->addNode($author, 'name', $authorName); $this->addNode($entry, 'updated', $updatedAtom); $this->addNode($entry, 'id', $guid); $this->addNode($entry, 'link', '', array('rel' => 'self', 'href' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])); // Add osearch & next link to the entry $this->addPagingFields($entry, $startIndex, $itemsPerPage, $totalResults); // Add response entries to feed $responses = $data->getEntry(); foreach ($responses as $response) { // Attempt to have a real ID field, otherwise we fall back on the idSpec id $idField = is_object($response) && isset($response->id) ? $response->id : (is_array($response) && isset($response['id']) ? $response['id'] : $requestItem->getUser()->getUserId($requestItem->getToken())); // construct <entry> blocks this record $feedEntry = $this->addNode($entry, 'entry'); $content = $this->addNode($feedEntry, 'content', '', array('type' => 'application/xml')); // Author node $author = $this->addNode($feedEntry, 'author'); $this->addNode($author, 'uri', $guid); $this->addNode($author, 'name', $authorName); // Special hoisting rules for activities if ($response instanceof Shindig_Activity) { $this->addNode($feedEntry, 'category', '', array('term' => 'status')); $this->addNode($feedEntry, 'updated', date(DATE_ATOM, $response->postedTime)); $this->addNode($feedEntry, 'id', 'urn:guid:' . $response->id); //FIXME should add a link field but don't have URL's available yet: // <link rel="self" type="application/atom+xml" href="http://api.example.org/activity/feeds/.../af3778"/> $this->addNode($feedEntry, 'title', strip_tags($response->title)); $this->addNode($feedEntry, 'summary', $response->body); // Unset them so addData doesn't include them again unset($response->postedTime); unset($response->id); unset($response->title); unset($response->body); } else { $this->addNode($feedEntry, 'id', 'urn:guid:' . $idField); $this->addNode($feedEntry, 'title', $requestType . ' feed entry for id ' . $idField); $this->addNode($feedEntry, 'updated', $updatedAtom); } // recursively add responseItem data to the xml structure $this->addData($content, $requestType, $response, self::$osNameSpace); } } else { // Single entry = Atom:Entry $entry = $doc->appendChild($doc->createElementNS(self::$nameSpace, "entry")); // Atom fields $this->addNode($entry, 'title', $requestType . ' entry for ' . $authorName); $author = $this->addNode($entry, 'author'); $this->addNode($author, 'uri', $guid); $this->addNode($author, 'name', $authorName); $this->addNode($entry, 'id', $guid); $this->addNode($entry, 'updated', $updatedAtom); $content = $this->addNode($entry, 'content', '', array('type' => 'application/xml')); // addData loops through the responseItem data recursively creating a matching XML structure $this->addData($content, $requestType, $data['entry'], self::$osNameSpace); } $xml = $doc->saveXML(); if ($responseItem->getResponse() instanceof RestfulCollection) { //FIXME dirty hack until i find a way to add multiple name spaces using DomXML functions $xml = str_replace('<feed xmlns="http://www.w3.org/2005/Atom">', '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:osearch="http://a9.com/-/spec/opensearch/1.1">', $xml); } echo $xml; }
/** * Tests ResponseItem->setResponse() */ public function testSetResponse() { $expected = array('bar' => 2); $this->ResponseItem->setResponse($expected); $this->assertEquals($expected, $this->ResponseItem->getResponse()); }
function outputResponse(ResponseItem $responseItem, RestRequestItem $requestItem) { echo json_encode($responseItem->getResponse()); }