10BC0 [HttpFoundation] Adds getAcceptableFormats() method for Request by AndreiIgna · Pull Request #26486 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpFoundation] Adds getAcceptableFormats() method for Request
  • Loading branch information
Andrei Igna authored and nicolas-grekas committed Jun 19, 2018
commit 8a127ea34a8ca53d25de242dabe2e1e02745529e
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.2.0
-----

* added `getAcceptableFormats()` for reading acceptable formats based on Accept header

4.1.0
-----

Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ class Request
*/
protected $format;

/**
* @var array
*/
private $acceptableFormats;

/**
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
*/
Expand Down Expand Up @@ -263,6 +268,7 @@ public function initialize(array $query = array(), array $request = array(), arr
$this->charsets = null;
$this->encodings = null;
$this->acceptableContentTypes = null;
$this->acceptableFormats = null;
$this->pathInfo = null;
$this->requestUri = null;
$this->baseUrl = null;
Expand Down Expand Up @@ -450,6 +456,7 @@ public function duplicate(array $query = null, array $request = null, array $att
$dup->charsets = null;
$dup->encodings = null;
$dup->acceptableContentTypes = null;
$dup->acceptableFormats = null;
$dup->pathInfo = null;
$dup->requestUri = null;
$dup->baseUrl = null;
Expand Down Expand Up @@ -1355,6 +1362,18 @@ public function getContentType()
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()))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is array_values actually required?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is added because the mix of array_filter and array_unqiue sometimes returns [0 => 'html', 2 => 'xml', 5 => 'json'].
This create problems with tests or confusion when checking the array, so it's best to reset the keys

}

/**
* Sets the default locale.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,16 @@ public function testGetAcceptableContentTypes()
$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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would it only return html and xml if everything is acceptable according to */*? I think this shows that the method is not clear and generic enough to be added to the core IMO.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see where I would possibly use this method. What would be useful is a similar method that accepts formats as argument and would return the ones that are acceptable according to the accept header. This would then allow to make use of */* and this is what people usually need in REST APIs etc.

}

public function testGetLanguages()
{
$request = new Request();
Expand Down
0