Skip to content

Commit b418f22

Browse files
committed
Fix comments, add PHPDoc
1 parent fe05ece commit b418f22

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/Symfony/Component/HttpFoundation/Request.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,8 @@ public function setFormat($format, $mimeTypes)
13471347
* * _format request attribute
13481348
* * $default
13491349
*
1350+
* @see getPreferredFormat
1351+
*
13501352
* @param string|null $default The default format
13511353
*
13521354
* @return string|null The request format
@@ -1563,19 +1565,26 @@ public function isNoCache()
15631565
return $this->headers->hasCacheControlDirective('no-cache') || 'no-cache' == $this->headers->get('Pragma');
15641566
}
15651567

1568+
/**
1569+
* Gets the preferred format for the response by inspecting, in the following order:
1570+
* * the request format set using setRequestFormat
1571+
* * the values of the Accept HTTP header
1572+
* * the content type of the body of the request
1573+
*/
15661574
public function getPreferredFormat(?string $default = 'html'): ?string
15671575
{
15681576
if (null !== $this->preferredFormat) {
15691577
return $this->preferredFormat;
15701578
}
15711579

1580+
$preferredFormat = null;
15721581
foreach ($this->getAcceptableContentTypes() as $contentType) {
1573-
if ($this->preferredFormat = $this->getFormat($contentType)) {
1574-
return $this->preferredFormat;
1582+
if ($preferredFormat = $this->getFormat($contentType)) {
1583+
break;
15751584
}
15761585
}
15771586

1578-
$this->preferredFormat = $this->getRequestFormat($this->getContentType());
1587+
$this->preferredFormat = $this->getRequestFormat($preferredFormat ?: $this->getContentType());
15791588

15801589
return $this->preferredFormat ?: $default;
15811590
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,13 @@ public function testGetPreferredFormat()
407407
$this->assertSame('json', $request->getPreferredFormat('json'));
408408

409409
$request->setRequestFormat('atom');
410-
$request->headers->set('Content-Type', 'application/json');
411-
$request->headers->set('Accept', 'application/xml');
410+
$request->headers->set('Accept', 'application/ld+json');
411+
$request->headers->set('Content-Type', 'application/merge-patch+json');
412412
$this->assertSame('atom', $request->getPreferredFormat());
413413

414414
$request = new Request();
415-
$request->headers->set('Content-Type', 'application/json');
416415
$request->headers->set('Accept', 'application/xml');
416+
$request->headers->set('Content-Type', 'application/json');
417417
$this->assertSame('xml', $request->getPreferredFormat());
418418

419419
$request = new Request();

0 commit comments

Comments
 (0)