Ejemplo n.º 1
0
 /**
  * Returns the accept quality of a submitted mime type based on the
  * request `Accept:` header. If the `$explicit` argument is `TRUE`,
  * only precise matches will be returned, excluding all wildcard (`*`)
  * directives.
  *
  *     // Accept: application/xml; application/json; q=.5; text/html; q=.2, text/*
  *     // Accept quality for application/json
  *
  *     // $quality = 0.5
  *     $quality = $request->headers()->accepts_at_quality('application/json');
  *
  *     // $quality_explicit = FALSE
  *     $quality_explicit = $request->headers()->accepts_at_quality('text/plain', TRUE);
  *
  * @param   string  $type
  * @param   boolean $explicit   explicit check, excludes `*`
  * @return  mixed
  * @since   3.2.0
  */
 public function accepts_at_quality($type, $explicit = FALSE)
 {
     // Parse Accept header if required
     if ($this->_accept_content === NULL) {
         if ($this->offsetExists('Accept')) {
             $accept = $this->offsetGet('Accept');
         } else {
             $accept = '*/*';
         }
         $this->_accept_content = HTTP_Header::parse_accept_header($accept);
     }
     // If not a real mime, try and find it in config
     if (strpos($type, '/') === FALSE) {
         $mime = Kohana::$config->load('mimes.' . $type);
         if ($mime === NULL) {
             return FALSE;
         }
         $quality = FALSE;
         foreach ($mime as $_type) {
             $quality_check = $this->accepts_at_quality($_type, $explicit);
             $quality = $quality_check > $quality ? $quality_check : $quality;
         }
         return $quality;
     }
     $parts = explode('/', $type, 2);
     if (isset($this->_accept_content[$parts[0]][$parts[1]])) {
         return $this->_accept_content[$parts[0]][$parts[1]];
     } elseif ($explicit === TRUE) {
         return FALSE;
     } else {
         if (isset($this->_accept_content[$parts[0]]['*'])) {
             return $this->_accept_content[$parts[0]]['*'];
         } elseif (isset($this->_accept_content['*']['*'])) {
             return $this->_accept_content['*']['*'];
         } else {
             return FALSE;
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Tests the `parse_accept_header` method parses the Accept: header
  * correctly and returns expected output
  * 
  * @dataProvider provider_parse_accept_header
  *
  * @param   string  $accept    accept in
  * @param   array   $expected  expected out
  * @return  void
  */
 public function test_parse_accept_header($accept, array $expected)
 {
     $this->assertSame($expected, HTTP_Header::parse_accept_header($accept));
 }