accepts() public method

This method may work different then you might think. This is a _convenience_ method working exclusively with short type names it knows about. Only those types will be matched. You can tell this method about more types via Media::type(). Note: In case negotiation fails, 'html' is used as a fallback type.
See also: lithium\net\http\Media::negotiate()
public accepts ( boolean | string $type = null ) : string | boolean | array
$type boolean | string Optionally a type name i.e. `'json'` or `true`. 1. If not specified, returns the media type name that the client prefers, using a potentially set `type` param, then content negotiation and that fails, ultimately falling back and returning the string `'html'`. 2. If a media type name (string) is passed, returns `true` or `false`, indicating whether or not that type is accepted by the client at all. 3. If `true`, returns the raw content types from the `Accept` header, parsed into an array and sorted by client preference.
return string | boolean | array Returns a type name (i.e. 'json'`) or a boolean or an array, depending on the value of `$type`.
Example #1
0
 /**
  * Tests that `Accept` headers with only one listed content type are parsed property, and tests
  * that `'* /*'` is still parsed as `'text/html'`.
  */
 public function testAcceptSingleContentType()
 {
     $request = new Request(array('env' => array('HTTP_ACCEPT' => 'application/json,text/xml')));
     $this->assertEqual(array('application/json', 'text/xml'), $request->accepts(true));
     $this->assertEqual('json', $request->accepts());
     $request = new Request(array('env' => array('HTTP_ACCEPT' => 'application/json')));
     $this->assertEqual(array('application/json'), $request->accepts(true));
     $this->assertEqual('json', $request->accepts());
     $request = new Request(array('env' => array('HTTP_ACCEPT' => '*/*')));
     $this->assertEqual(array('text/html'), $request->accepts(true));
     $this->assertEqual('html', $request->accepts());
 }
Example #2
0
 public function testParsingAcceptHeader()
 {
     $chrome = array('application/xml', 'application/xhtml+xml', 'text/html;q=0.9', 'text/plain;q=0.8', 'image/png', '*/*;q=0.5');
     $firefox = array('text/html', 'application/xhtml+xml', 'application/xml;q=0.9', '*/*;q=0.8');
     $safari = array('application/xml', 'application/xhtml+xml', 'text/html;q=0.9', 'text/plain;q=0.8', 'image/png', '*/*;q=0.5');
     $opera = array('text/html', 'application/xml;q=0.9', 'application/xhtml+xml', 'image/png', 'image/jpeg', 'image/gif', 'image/x-xbitmap', '*/*;q=0.1');
     $request = new Request(array('env' => array('HTTP_ACCEPT' => join(',', $chrome))));
     $this->assertEqual('html', $request->accepts());
     $this->assertTrue(array_search('text/plain', $request->accepts(true)), 4);
     $request = new Request(array('env' => array('HTTP_ACCEPT' => join(',', $safari))));
     $this->assertEqual('html', $request->accepts());
     $request = new Request(array('env' => array('HTTP_ACCEPT' => join(',', $firefox))));
     $this->assertEqual('html', $request->accepts());
     $request = new Request(array('env' => array('HTTP_ACCEPT' => join(',', $opera))));
     $this->assertEqual('html', $request->accepts());
     $request = new Request(array('env' => array('HTTP_ACCEPT' => join(',', $chrome))));
     $request->params['type'] = 'txt';
     $result = $request->accepts(true);
     $this->assertEqual('text/plain', $result[0]);
 }