Example #1
0
 /**
  * Detect and set the output format and mimetype
  *
  * This method bases it's detection on the ACCEPT header passed
  * to the API.
  *
  * @return array of mimetype, outputFormat and params
  */
 public function detectOutputFormat()
 {
     $mimetypes = Frapi_Output::getMimeTypeMap();
     if ($mimetypes) {
         $this->mimeMaps = $mimetypes;
     }
     $types = $this->parseAcceptHeader();
     $return = array('outputFormat' => false, 'mimetype' => false, 'params' => array());
     if (!empty($types)) {
         $mimetypes = $this->parseMimeTypes();
         foreach ($types as $type) {
             if (isset($this->mimeMaps[$type['mimetype']])) {
                 $return['outputFormat'] = $this->mimeMaps[$type['mimetype']];
                 $return['mimetype'] = $type['mimetype'];
                 $return['params'] = isset($type['params']) ? $type['params'] : array();
                 break;
             } else {
                 foreach ($mimetypes as $mimetype) {
                     $matches = array();
                     if (preg_match($mimetype['pattern'], $type['mimetype'], $matches)) {
                         $return['mimetype'] = $type['mimetype'];
                         $return['params'] = isset($type['params']) ? $type['params'] : array();
                         if ($mimetype['output_format'][0] == ':') {
                             $format = substr($mimetype['output_format'], 1);
                             if (!empty($matches[$format]) && Frapi_Rules::validateOutputType($matches[$format])) {
                                 $return['outputFormat'] = $matches[$format];
                             }
                         } else {
                             $return['outputFormat'] = $mimetype['output_format'];
                         }
                         foreach ($mimetype['params'] as $param) {
                             $return['params'][$param] = $matches[$param];
                         }
                         break 2;
                     }
                 }
             }
         }
     }
     // Final catch-all (for cases like Accept: */*)
     if (!$return['outputFormat']) {
         $return['outputFormat'] = $this->getDefaultFormatFromConfiguration();
         $return['mimetype'] = Frapi_Output::getMimeTypeByFormat($return['outputFormat']);
     }
     $this->setOutputFormat($return['outputFormat']);
     return $return;
 }
Example #2
0
File: Main.php Project: helgi/frapi
 /**
  * Set format
  *
  * This method will check if the format is
  * an acceptable one, if so it'll set it to the
  * requested format.
  *
  * In the case where there are no format passed, we
  * default the value to 'xml'
  *
  * @param string $format The format to use.
  * @throws Frapi_Error
  */
 protected function setFormat($format = false)
 {
     if ($format) {
         $typeValid = Frapi_Rules::validateOutputType($format);
         $this->format = $format;
     } else {
         throw new Frapi_Error(Frapi_Error::ERROR_INVALID_URL_PROMPT_FORMAT_NAME, Frapi_Error::ERROR_INVALID_URL_PROMPT_FORMAT_MSG, Frapi_Error::ERROR_INVALID_URL_PROMPT_FORMAT_NO);
     }
 }