8000 [HttpFoundation] Adds getAcceptableFormats() method for Request · symfony/symfony@8a127ea · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a127ea

Browse files
Andrei Ignanicolas-grekas
Andrei Igna
authored andcommitted
[HttpFoundation] Adds getAcceptableFormats() method for Request
1 parent d4f5d46 commit 8a127ea

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.2.0
5+
-----
6+
7+
* added `getAcceptableFormats()` for reading acceptable formats based on Accept header
8+
49
4.1.0
510
-----
611

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ class Request
170170
*/
171171
protected $format;
172172

173+
/**
174+
* @var array
175+
*/
176+
private $acceptableFormats;
177+
173178
/**
174179
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
175180
*/
@@ -263,6 +268,7 @@ public function initialize(array $query = array(), array $request = array(), arr
263268
$this->charsets = null;
264269
$this->encodings = null;
265270
$this->acceptableContentTypes = null;
271+
$this->acceptableFormats = null;
266272
$this->pathInfo = null;
267273
$this->requestUri = null;
268274
$this->baseUrl = null;
@@ -450,6 +456,7 @@ public function duplicate(array $query = null, array $request = null, array $att
450456
$dup->charsets = null;
451457
$dup->encodings = null;
452458
$dup->acceptableContentTypes = null;
459+
$dup->acceptableFormats = null;
453460
$dup->pathInfo = null;
454461
$dup->requestUri = null;
455462
$dup->baseUrl = null;
@@ -1355,6 +1362,18 @@ public function getContentType()
13551362
return $this->getFormat($this->headers->get('CONTENT_TYPE'));
13561363
}
13571364

1365+
/**
1366+
* Gets the acceptable client formats associated with the request.
1367+
*/
1368+
public function getAcceptableFormats(): array
1369+
{
1370+
if (null !== $this->acceptableFormats) {
1371+
return $this->acceptableFormats;
1372+
}
1373+
1374+
return $this->acceptableFormats = array_values(array_unique(array_filter(array_map(array($this, 'getFormat'), $this->getAcceptableContentTypes()))));
1375+
}
1376+
13581377
/**
13591378
* Sets the default locale.
13601379
*

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,16 @@ public function testGetAcceptableContentTypes()
14271427
$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());
14281428
}
14291429

1430+
public function testGetAcceptableFormats()
1431+
{
1432+
$request = new Request();
1433+
$this->assertEquals(array(), $request->getAcceptableFormats());
1434+
1435+
$request = new Request();
1436+
$request->headers->set('Accept', 'text/html, application/xhtml+xml, application/xml;q=0.9, */*');
1437+
$this->assertEquals(array('html', 'xml'), $request->getAcceptableFormats());
1438+
}
1439+
14301440
public function testGetLanguages()
14311441
{
14321442
$request = new Request();

0 commit comments

Comments
 (0)
0