[HttpFoundation] Adds getAcceptableFormats() method for Request

This commit is contained in:
Andrei Igna 2018-03-11 16:37:41 +01:00 committed by Nicolas Grekas
parent d4f5d46b13
commit 8a127ea34a
3 changed files with 34 additions and 0 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG
=========
4.2.0
-----
* added `getAcceptableFormats()` for reading acceptable formats based on Accept header
4.1.0
-----

View File

@ -170,6 +170,11 @@ class Request
*/
protected $format;
/**
* @var array
*/
private $acceptableFormats;
/**
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
*/
@ -263,6 +268,7 @@ class Request
$this->charsets = null;
$this->encodings = null;
$this->acceptableContentTypes = null;
$this->acceptableFormats = null;
$this->pathInfo = null;
$this->requestUri = null;
$this->baseUrl = null;
@ -450,6 +456,7 @@ class Request
$dup->charsets = null;
$dup->encodings = null;
$dup->acceptableContentTypes = null;
$dup->acceptableFormats = null;
$dup->pathInfo = null;
$dup->requestUri = null;
$dup->baseUrl = null;
@ -1355,6 +1362,18 @@ class Request
return $this->getFormat($this->headers->get('CONTENT_TYPE'));
}
/**
* Gets the acceptable client formats associated with the request.
*/
public function getAcceptableFormats(): array
{
if (null !== $this->acceptableFormats) {
return $this->acceptableFormats;
}
return $this->acceptableFormats = array_values(array_unique(array_filter(array_map(array($this, 'getFormat'), $this->getAcceptableContentTypes()))));
}
/**
* Sets the default locale.
*

View File

@ -1427,6 +1427,16 @@ class RequestTest extends TestCase
$this->assertEquals(array('application/vnd.wap.wmlscriptc', 'text/vnd.wap.wml', 'application/vnd.wap.xhtml+xml', 'application/xhtml+xml', 'text/html', 'multipart/mixed', '*/*'), $request->getAcceptableContentTypes());
}
public function testGetAcceptableFormats()
{
$request = new Request();
$this->assertEquals(array(), $request->getAcceptableFormats());
$request = new Request();
$request->headers->set('Accept', 'text/html, application/xhtml+xml, application/xml;q=0.9, */*');
$this->assertEquals(array('html', 'xml'), $request->getAcceptableFormats());
}
public function testGetLanguages()
{
$request = new Request();