mapType() public method

e.g mapType('application/pdf'); // returns 'pdf'
public mapType ( string | array $ctype ) : string | array | null
$ctype string | array Either a string content type to map, or an array of types.
return string | array | null Aliases for the types provided.
Beispiel #1
0
 /**
  * Tests the mapType method
  *
  * @return void
  */
 public function testMapType()
 {
     $response = new Response();
     $this->assertEquals('wav', $response->mapType('audio/x-wav'));
     $this->assertEquals('pdf', $response->mapType('application/pdf'));
     $this->assertEquals('xml', $response->mapType('text/xml'));
     $this->assertEquals('html', $response->mapType('*/*'));
     $this->assertEquals('csv', $response->mapType('application/vnd.ms-excel'));
     $expected = ['json', 'xhtml', 'css'];
     $result = $response->mapType(['application/json', 'application/xhtml+xml', 'text/css']);
     $this->assertEquals($expected, $result);
 }
 /**
  * Set the extension based on the accept headers.
  * Compares the accepted types and configured extensions.
  * If there is one common type, that is assigned as the ext/content type for the response.
  * The type with the highest weight will be set. If the highest weight has more
  * than one type matching the extensions, the order in which extensions are specified
  * determines which type will be set.
  *
  * If html is one of the preferred types, no content type will be set, this
  * is to avoid issues with browsers that prefer HTML and several other content types.
  *
  * @param \Cake\Network\Request $request The request instance.
  * @param \Cake\Network\Response $response The response instance.
  * @return void
  */
 protected function _setExtension($request, $response)
 {
     $accept = $request->parseAccept();
     if (empty($accept)) {
         return;
     }
     $accepts = $response->mapType($accept);
     $preferedTypes = current($accepts);
     if (array_intersect($preferedTypes, ['html', 'xhtml'])) {
         return;
     }
     $extensions = Router::extensions();
     foreach ($accepts as $types) {
         $ext = array_intersect($extensions, $types);
         if ($ext) {
             $this->ext = current($ext);
             break;
         }
     }
 }
Beispiel #3
0
 /**
  * Maps a content-type back to an alias
  *
  * e.g `mapType('application/pdf'); // returns 'pdf'`
  *
  * @param string|array $ctype Either a string content type to map, or an array of types.
  * @return mixed Aliases for the types provided.
  */
 public function mapType($ctype)
 {
     return parent::mapType($ctype);
 }
 /**
  * Set the extension based on the accept headers.
  * Compares the accepted types and configured extensions.
  * If there is one common type, that is assigned as the ext/content type for the response.
  * The type with the highest weight will be set. If the highest weight has more
  * than one type matching the extensions, the order in which extensions are specified
  * determines which type will be set.
  *
  * If html is one of the preferred types, no content type will be set, this
  * is to avoid issues with browsers that prefer HTML and several other content types.
  *
  * @param \Cake\Network\Request $request The request instance.
  * @param \Cake\Network\Response $response The response instance.
  * @return void
  */
 protected function _setExtension($request, $response)
 {
     $accept = $request->parseAccept();
     if (empty($accept)) {
         return;
     }
     $accepts = $response->mapType($accept);
     $preferredTypes = current($accepts);
     if (array_intersect($preferredTypes, ['html', 'xhtml'])) {
         return;
     }
     $extensions = array_unique(array_merge(Router::extensions(), array_keys($this->config('viewClassMap'))));
     foreach ($accepts as $types) {
         $ext = array_intersect($extensions, $types);
         if (!empty($ext)) {
             $this->ext = current($ext);
             break;
         }
     }
 }
 /**
  * Returns the current response type (Content-type header), or null if not alias exists
  *
  * @return mixed A string content type alias, or raw content type if no alias map exists,
  *	otherwise null
  */
 public function responseType()
 {
     return $this->response->mapType($this->response->type());
 }