• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

system/classes/restresponse.php

00001 <?php
00012 namespace Habari;
00013 
00014 class RestResponse
00015 {
00016   protected $response;
00017 
00022   public function __construct($response) {
00023     $this->response = $response;
00024   }
00025 
00029   public function out() {
00030     $accept = $this->get_accept();
00031     header('content-type: ' . $accept);
00032     echo $this->get();
00033   }
00034 
00040   public function get_best_mime($mime_types = null) {
00041     // Values will be stored in this array
00042     $accept_types = array();
00043 
00044     // Accept header is case insensitive, and whitespace isn’t important
00045     $accept = strtolower(str_replace(' ', '', $_SERVER['HTTP_ACCEPT']));
00046     // divide it into parts in the place of a ","
00047     $accept = explode(',', $accept);
00048     foreach ($accept as $a) {
00049       // the default quality is 1.
00050       $q = 1;
00051       // check if there is a different quality
00052       if (strpos($a, ';q=')) {
00053         // divide "mime/type;q=X" into two parts: "mime/type" and "X"
00054         list($a, $q) = explode(';q=', $a);
00055       }
00056       // mime-type $a is accepted with the quality $q
00057       // WARNING: $q == 0 means, that mime-type isn’t supported!
00058       $accept_types[$a] = $q;
00059     }
00060     arsort($accept_types);
00061 
00062     // if no parameter was passed, just return parsed data
00063     if (!$mime_types) {
00064       return $accept_types;
00065     }
00066 
00067     $mime_types = array_map('strtolower', (array)$mime_types);
00068 
00069     // let's check our supported types:
00070     foreach ($accept_types as $mime => $q) {
00071       if ($q && in_array($mime, $mime_types)) {
00072         return $mime;
00073       }
00074     }
00075     // no mime-type found
00076     return null;
00077   }
00078 
00079   public function get_mime_list() {
00080     static $mimelist = null;
00081     if(is_null($mimelist)) {
00082       $mimelist = array(
00083         'text/plain' => array($this, 'convert_text_plain'),
00084         'text/html' => array($this, 'convert_text_html'),
00085         'application/json' => array($this, 'convert_applicaton_json'),
00086         'application/xml' => array($this, 'convert_application_xml'),
00087       );
00088       $mimelist = Plugins::filter('rest_mime_list', $mimelist);
00089     }
00090     return $mimelist;
00091   }
00092 
00093   public function get_accept()
00094   {
00095     $mimelist = $this->get_mime_list();
00096     $accept = $this->get_best_mime(array_keys($mimelist));
00097     return $accept;
00098   }
00099 
00100   public function get() {
00101     $mimelist = $this->get_mime_list();
00102     $accept = $this->get_accept();
00103     if(is_string($this->response)) {
00104       $response = $this->response;
00105     }
00106     elseif(is_array($this->response)) {
00107       $response = $mimelist[$accept]($this->response);
00108     }
00109     elseif($this->response instanceof DOMElement) {
00110       $response = $mimelist[$accept]($this->response);
00111     }
00112 
00113     $response = Plugins::filter('rest_response', $response, $accept, $this->response);
00114     return $response;
00115   }
00116 }

Generated on Sun Aug 4 2013 12:51:43 for Habari by  doxygen 1.7.1