Exemple #1
0
 /**
  * Return XML payload
  * 
  * @param  object|array $data
  * @param  string $node
  * @throws \App\Engine\Exception
  * @return string
  */
 public function getXml($data, $node)
 {
     // Error checking
     $exception = null;
     if (empty($data)) {
         $exception = "Was expecting data - none provided.";
     } elseif (empty($node)) {
         $exception = "Was expecting node - none provided.";
     }
     if (null != $exception) {
         throw new \App\Engine\Exception($exception, 500);
     }
     // BEGIN: Build header response
     $headers = new stdClass();
     // 200 OK
     $headers->ResponseCode = 200;
     // Set a Vary response header based on the Accept header
     // Some insight into this can be found at http://www.subbu.org/blog/2007/12/vary-header-for-restful-applications
     $headers->Vary = 'Accept';
     // END: Build header response
     // BEGIN: Set response headers
     /** @var Zend_Controller_Front */
     $fc = Zend_Controller_Front::getInstance();
     $fc->getResponse()->setHttpResponseCode($headers->ResponseCode);
     if (isset($headers->Vary)) {
         $fc->getResponse()->setHeader('Vary', $headers->Vary);
     }
     // END: Set response headers
     // BEGIN: Build XML payload
     // Construct API url
     $protocol = 'http';
     if (!empty($_SERVER['HTTPS'])) {
         $protocol .= 's';
     }
     // Construct 'self' uri
     $link_self = new stdClass();
     $link_self->href = $protocol . '://' . rtrim($fc->getBaseUrl(), '/') . '/' . preg_replace('/^\\/v.+?\\//i', '', $_SERVER['REDIRECT_URL']);
     $link_self->rel = 'self';
     // Construct list of links
     $links[] = $link_self;
     // Put together payload
     $xmlBuilder = new App_Engine_DOMi();
     $xmlBuilder->attachToXml($headers, 'headers');
     // if single entity
     if (is_object($data)) {
         $data->{'resource_link'} = $link_self;
     } else {
         $xmlBuilder->attachToXml($links, 'resource_link');
     }
     $xmlBuilder->attachToXml($data, $node);
     // END: Build XML payload
     return $xmlBuilder->render();
 }
 /**
  * Generate XML DELETE response
  * 
  * @return string
  */
 public function deleteXml()
 {
     $headers = new stdClass();
     $headers->ResponseCode = 204;
     // BEGIN: Set response headers
     /** @var Zend_Controller_Front */
     $fc = Zend_Controller_Front::getInstance();
     $fc->getResponse()->setHttpResponseCode($headers->ResponseCode);
     // END: Set response headers
     // BEGIN: Build XML payload
     $xmlBuilder = new App_Engine_DOMi();
     $xmlBuilder->attachToXml($headers, 'headers');
     // END: Build XML payload
     return $xmlBuilder->render();
 }
Exemple #3
0
 /**
  * Builds response to XML PUT request
  * 
  * $data is the data that should be represented in the response.  In this case, it is the primary key
  * of the updated entity.
  * 
  * @param object $data
  * @param string $node
  * @param object App\System\Service\* $service
  * @param string $uri
  */
 public function putXml($data = null, $node, $service = null, $uri = null)
 {
     // Error checking
     $exception = null;
     if (empty($node)) {
         $exception = "Was expecting node - none provided.";
     }
     if (null != $exception) {
         throw new \App\Engine\Exception($exception, 500);
     }
     // Header response container
     $headers = new stdClass();
     /** @var Zend_Controller_Front */
     $fc = Zend_Controller_Front::getInstance();
     // 204 No Content
     if (null == $data && null == $service && null == $uri) {
         $headers->ResponseCode = 204;
     } else {
         // Error checking
         $exception = null;
         if (empty($data)) {
             $exception = "Was expecting data - none provided.";
         } elseif (empty($service)) {
             $exception = "Was expecting node - none provided.";
         } elseif (empty($uri)) {
             $exception = "Was expecting uri - none provided.";
         }
         if (null != $exception) {
             throw new \App\Engine\Exception($exception, 500);
         }
         // Construct API url
         $protocol = 'http';
         if (!empty($_SERVER['HTTPS'])) {
             $protocol .= 's';
         }
         $_303url = $protocol . '://' . rtrim($fc->getBaseUrl(), '/') . '/' . $uri . '/' . $data;
         $headers->ResponseCode = 303;
         $headers->{'Content-Location'} = $_303url;
         $headers->Location = $_303url;
     }
     // BEGIN: Set response headers
     $fc->getResponse()->setHttpResponseCode($headers->ResponseCode);
     // 303 See Other
     if (303 == $headers->ResponseCode) {
         $fc->getResponse()->setHeader('Content-Location', $_303url);
         $fc->getResponse()->setHeader('Location', $_303url);
     }
     // END: Set response headers
     if (204 != $headers->ResponseCode) {
         // BEGIN: Build XML payload
         // Get representation of newly created resource
         $createdResource = $service->distillAsObject($service->getById($data));
         // Construct 'self' uri
         $link_self = new stdClass();
         $link_self->href = $_303url;
         $link_self->rel = 'self';
         $createdResource->{'resource_link'} = $link_self;
         $xmlBuilder = new App_Engine_DOMi();
         $xmlBuilder->attachToXml($headers, 'headers');
         $xmlBuilder->attachToXml($createdResource, $node);
         // END: Build XML payload
         return $xmlBuilder->render();
     }
 }