Пример #1
0
 /**
  * This function outputs the given $data as valid HAL+JSON to the client
  * and sets the HTTP Response Code to the given $statusCode.
  *
  * @param array|SlimBootstrap\DataObject $data       The data to output to
  *                                                   the client
  * @param int                            $statusCode The status code to set
  *                                                   in the reponse
  */
 public function write($data, $statusCode = 200)
 {
     $path = $this->_request->getPath();
     $hal = new hal\Hal($path);
     if (true === is_array($data)) {
         $pathData = explode('/', $path);
         unset($pathData[0]);
         $endpointName = end($pathData);
         $endpointUri = '/' . implode('/', $pathData) . '/';
         foreach ($data as $entry) {
             /** @var SlimBootstrap\DataObject $entry */
             $identifiers = $entry->getIdentifiers();
             $resourceName = $endpointUri . implode('/', array_values($identifiers));
             $resource = new hal\Hal($resourceName, $entry->getData() + $entry->getIdentifiers());
             $this->_addAdditionalLinks($resource, $entry->getLinks());
             $hal->addLink($endpointName, $resourceName);
             $hal->addResource($endpointName, $resource);
         }
     } else {
         $hal->setData($data->getData() + $data->getIdentifiers());
         $this->_addAdditionalLinks($hal, $data->getLinks());
     }
     $body = $this->_jsonEncode($hal);
     if (false === $body) {
         $this->_response->setStatus(500);
         $this->_response->setBody("Error encoding requested data.");
         return;
     }
     $this->_headers->set('Content-Type', 'application/hal+json; charset=UTF-8');
     $this->_response->setStatus($statusCode);
     $this->_response->setBody($hal->asJson());
 }
 public function setResult(RestApi_Result $result)
 {
     foreach ($result->getHeaders() as $name => $value) {
         $this->response->headers()->set($name, $value);
     }
     $this->response->setStatus($result->getCode());
     $this->response->setBody($result->getBody());
 }
Пример #3
0
 /**
  * @param DataObject $entry
  * @param int $statusCode   - default 200, is only set at first method call
  */
 public function writeToStream(SlimBootstrap\DataObject $entry, $statusCode = 200)
 {
     if (true === $this->_firstCall) {
         $this->_headers->set('Content-Type', 'text/csv; charset=UTF-8');
         $this->_response->setStatus($statusCode);
         echo $this->_determineHeadline(array($entry));
         $this->_firstCall = false;
     }
     echo $this->_buildCsvLineFromDataSet($entry->getData(), $this->_multidimensionalFields, $this->_lastFieldName, $this->_encloseAll) . $this->_linebreak;
     $this->_flush();
 }
Пример #4
0
 /**
  * This function outputs the given $data as valid JSON to the client
  * and sets the HTTP Response Code to the given $statusCode.
  *
  * @param array|SlimBootstrap\DataObject $data       The data to output to
  *                                                   the client
  * @param int                            $statusCode The status code to set
  *                                                   in the response
  */
 public function write($data, $statusCode = 200)
 {
     $result = array();
     if (true === is_array($data)) {
         foreach ($data as $entry) {
             /** @var SlimBootstrap\DataObject $entry */
             $identifiers = array_values($entry->getIdentifiers());
             $this->_buildStructure($entry, $identifiers, 0, $result);
         }
     } else {
         $identifiers = array_values($data->getIdentifiers());
         $this->_buildStructure($data, $identifiers, 0, $result);
     }
     $body = $this->_jsonEncode($result);
     if (false === $body) {
         $this->_response->setStatus(500);
         $this->_response->setBody("Error encoding requested data.");
         return;
     }
     $this->_headers->set('Content-Type', 'application/json; charset=UTF-8');
     $this->_response->setStatus($statusCode);
     $this->_response->setBody($body);
 }